Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 11 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 37 additions & 5 deletions packages/utah-design-system/src/components/FileInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const ReactHookFormValidation: Story = {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { control, handleSubmit } = useForm({
defaultValues: {
files: null,
files: null as File[] | null,
},
});

Expand All @@ -108,13 +108,12 @@ export const ReactHookFormValidation: Story = {
control={control}
name="files"
rules={{ required: 'Please select at least one file' }}
render={({ field: { onChange, ...field }, fieldState }) => (
render={({ field: { onChange, value, ...field }, fieldState }) => (
<FileInput
errorMessage={fieldState.error?.message}
isInvalid={fieldState.invalid}
onSelect={(fileList) => {
onChange(fileList);
}}
value={value}
onChange={onChange}
{...field}
{...args}
/>
Expand Down Expand Up @@ -167,3 +166,36 @@ export const HtmlValidation: Story = {
isRequired: true,
},
};

export const Controlled: Story = {
render: (args) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [files, setFiles] = useState<File[] | null>(null);

return (
<div className="flex flex-col items-start gap-4">
<FileInput {...args} value={files} onChange={setFiles} />
<div className="flex gap-2">
<Button
onPress={() => setFiles(null)}
variant="secondary"
isDisabled={!files || files.length === 0}
>
Reset
</Button>
</div>
{files && files.length > 0 && (
<div className="text-sm text-zinc-600 dark:text-zinc-400">
Parent state: {files.map((f) => f.name).join(', ')}
</div>
)}
</div>
);
},
args: {
label: 'Upload document (controlled)',
placeholder: 'Drag a file here or click to upload',
description:
'This input is controlled. Click Reset to clear files from parent state.',
},
};
Loading