Created
July 14, 2022 19:35
-
-
Save neysidev/61414f6b139b3b7e6db4b6f1bd0a0de1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import _ from "lodash" | |
| import { formatBytes } from "./format" // https://gist.github.com/neysidev/71d6100023c4359a5cff256f2601c333 | |
| const openBrowseWindow = ({ | |
| accept = [], | |
| multiple = false, | |
| onChange = () => {}, | |
| }) => { | |
| // Initialize the file input | |
| const input = document.createElement("input") | |
| input.type = "file" | |
| input.multiple = multiple | |
| input.accept = accept.join(",") | |
| // Add the event listener | |
| input.addEventListener("change", event => { | |
| // convert file list to array | |
| const filesArray = _.toArray(event.target.files) | |
| const files = filesArray.map(file => ({ | |
| ...file, | |
| size: formatBytes(file.size), | |
| extension: file.name.split(".").at(-1), | |
| url: URL.createObjectURL(file), | |
| })) | |
| onChange(files) | |
| }) | |
| // Open the file input | |
| input.click() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment