Skip to content

Commit

Permalink
feat: reset filepicker on import.done and reset
Browse files Browse the repository at this point in the history
  • Loading branch information
vsgoulart committed Aug 23, 2024
1 parent 6dd5f00 commit 334d134
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { formFieldClasses } from '../Util';
import { Label } from '../Label';
import { Errors } from '../Errors';
import { useRef, useState } from 'preact/hooks';
import { useSingleLineTemplateEvaluation } from '../../hooks';
import { useEffect, useRef, useState } from 'preact/hooks';
import { useService, useSingleLineTemplateEvaluation } from '../../hooks';

const type = 'filepicker';

Expand All @@ -28,13 +28,31 @@ export function FilePicker(props) {
const fileInputRef = useRef(null);
/** @type {[File[],import("preact/hooks").StateUpdater<File[]>]} */
const [selectedFiles, setSelectedFiles] = useState([]);
const eventBus = useService('eventBus');
const { field, onChange, domId, errors = [], disabled, readonly, required } = props;
const { label, multiple = '', accept = '', id } = field;
const evaluatedAccept = useSingleLineTemplateEvaluation(accept);
const evaluatedMultiple =
useSingleLineTemplateEvaluation(typeof multiple === 'string' ? multiple : multiple.toString()) === 'true';
const errorMessageId = `${domId}-error-message`;

useEffect(() => {
const reset = () => {
setSelectedFiles([]);
onChange({
value: null,
});
};

eventBus.on('import.done', reset);
eventBus.on('reset', reset);

return () => {
eventBus.off('import.done', reset);
eventBus.off('reset', reset);
};
}, [eventBus, onChange]);

return (
<div class={formFieldClasses(type, { errors, disabled, readonly })}>
<Label htmlFor={domId} label={label} required={required} />
Expand Down

0 comments on commit 334d134

Please sign in to comment.