The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/client/hooks/useFileInput.js

25 lines
743 B

import { useState, useEffect } from 'react';
export const useFileInput = (onSetFile, fileType = 'image') => {
const [openInput, setOpenInput] = useState();
useEffect(() => {
const fileInput = document.createElement('input');
const formData = new FormData();
fileInput.setAttribute('type', 'file');
fileInput.setAttribute('style', 'display: none');
document.body.appendChild(fileInput);
const handleFiles = function() {
formData.append(fileType, this.files[0]);
onSetFile(this.files[0], formData);
};
fileInput.addEventListener('change', handleFiles, false);
setOpenInput(() => () => fileInput.click());
return () => {
fileInput.parentNode.removeChild(fileInput);
};
}, [onSetFile]);
return openInput;
};