@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
29 lines • 1.01 kB
JavaScript
import { useRef } from "react";
import { useMergeRefs } from "../../utils/hooks/index.js";
import { validateFiles } from "./utils/validate-files.js";
export const useFileUpload = ({ ref, accept, onSelect, validator, maxSizeInBytes, disabled, }) => {
const inputRef = useRef(null);
const mergedRef = useMergeRefs(inputRef, ref);
const upload = (fileList) => {
const { files, partitionedFiles } = validateFiles(Array.from(fileList), accept, validator, maxSizeInBytes);
onSelect(files, partitionedFiles);
};
const onChange = (event) => {
const fileList = event.target.files;
if (!fileList) {
return;
}
if (!disabled) {
upload(fileList);
}
// Resets the value to make it is possible to upload the same file several consecutive times
event.target.value = "";
};
return {
upload,
onChange,
inputRef,
mergedRef,
};
};
//# sourceMappingURL=useFileUpload.js.map