fix(input): updated file validation

This commit is contained in:
sourabpramanik committed 2024-01-03 06:49:50 +05:30
1 parent 6154a7109f
commit deee62175f
1 file changed
+14 -12
+14 -12
View File
@@ -20,18 +20,6 @@
function setFiles(value: FileList) {
if (!value) return;
const hasInvalidExt = Array.from(value).some((file) => {
const fileExtension = file.name.split('.').pop();
return allowedFileExtensions?.length
? !allowedFileExtensions.includes(fileExtension)
: false;
});
if (hasInvalidExt) {
error = 'Invalid file extension';
input.value = null;
return;
}
files = value;
input.files = value;
}
@@ -79,6 +67,20 @@
const handleChange = (event: Event) => {
const target = event.currentTarget as HTMLInputElement;
const isValidFiles = Array.from(target.files).every((file) => {
const fileExtension = file.name.split('.').pop();
return allowedFileExtensions?.length
? allowedFileExtensions.includes(fileExtension)
: true;
});
if (!isValidFiles) {
error = 'Invalid file extension';
target.value = '';
return;
}
setFiles(target.files);
};
</script>