KarimMokhtar / react-drag-drop-files

Light and simple Reactjs drag and drop files library to use with very flexible options to change, so you put whatever the design you want for your drop-area. Users can drag and drop or even select the file anywhere in the window.
MIT License
245 stars 86 forks source link

feat: Pass error label to component #111

Open siyavash4812 opened 1 year ago

siyavash4812 commented 1 year ago

Summary

At the moment a hard coded value is being used if there is a type or size error. This PR will add a prop to the library which will allow users to pass in a custom message.

Key Changes

Check List

thanosd commented 8 months ago

@siyavash4812 Are you able to fix the lint issue here so maybe we can get this merged? Was hitting the same issue, and since you have done all the good work to fix this, did not want to create a duplicate. Thanks!

wccbuck commented 5 months ago

While we wait for the ability to add a custom error message to override the not-so-great default one, I thought I'd share the clumsy way I'm editing this component by brute force in my application. I'm also including my fix for the incorrect tooltip (it says the file must be greater than or equal to the upper limit, but it should say less than or equal to).

const fileUploaderRef = useRef<HTMLDivElement | null>(null);
const fixText = () => {
    const spans = fileUploaderRef.current?.querySelectorAll('span');
    spans?.forEach(span => {
        if (span.textContent === "File type/size error, Hovered on types!") {
            span.textContent = "File type/size error.";
        }
        if (span.title.includes(", types:") && !span.title.includes(" MB, types:")) {
            span.title = span.title.replace(", types:", " MB, types:");
            span.title = span.title.replace(">=", "<=");
        }
    })
}
useEffect(() => {
    fixText();
    const observer = new MutationObserver(fixText);
    if (fileUploaderRef.current) {
        observer.observe(fileUploaderRef.current, {
            childList: true,
            subtree: true
        });
    }
    return () => observer.disconnect();
}, [fileUploaderRef, fixText]);

return (
    <div ref={fileUploaderRef}>
        <FileUploader
            handleChange={handleUpload}
            maxSize={2}
        />
    </div>
)