From 41de8781a2c483f981e706fec01826cc684aa38d Mon Sep 17 00:00:00 2001 From: Joeri Roijenga <16878956+JoeriRoijenga@users.noreply.github.com> Date: Thu, 9 Jan 2025 09:43:53 +0100 Subject: [PATCH] FEAT: Preload files on FileInput (#1092) #962 --- packages/components-react/src/FileInput.tsx | 14 ++++++++++--- .../src/community/file-input.stories.tsx | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/components-react/src/FileInput.tsx b/packages/components-react/src/FileInput.tsx index 1b636909c..473261620 100644 --- a/packages/components-react/src/FileInput.tsx +++ b/packages/components-react/src/FileInput.tsx @@ -1,10 +1,10 @@ import { Paragraph } from '@utrecht/component-library-react'; -import { ChangeEvent, PropsWithChildren, RefObject, useState } from 'react'; +import { ChangeEvent, PropsWithChildren, RefObject, useEffect, useState } from 'react'; import { Button, ButtonProps } from './Button'; import { File } from './File'; export interface FileInputProps extends Omit { - ref: RefObject; + ref?: RefObject; buttonText: string; buttonAppearance?: ButtonProps['appearance']; maxFileSizeInBytes: number; @@ -12,6 +12,7 @@ export interface FileInputProps extends Omit { fileSizeErrorMessage: string; fileTypeErrorMessage: string; onValueChange?: (callbackFiles: File[]) => void; // eslint-disable-line no-unused-vars + defaultFiles?: File[]; } export const FileInput = ({ @@ -24,6 +25,7 @@ export const FileInput = ({ fileSizeErrorMessage, fileTypeErrorMessage, onValueChange, + defaultFiles, }: PropsWithChildren) => { const [files, setFiles] = useState([]); const inputElement = ref; @@ -37,6 +39,12 @@ export const FileInput = ({ } }; + useEffect(() => { + if (defaultFiles) { + setFiles(defaultFiles); + } + }, [defaultFiles]); + return (
{children} @@ -53,7 +61,7 @@ export const FileInput = ({
diff --git a/packages/storybook/src/community/file-input.stories.tsx b/packages/storybook/src/community/file-input.stories.tsx index 3e59b1835..a92e32466 100644 --- a/packages/storybook/src/community/file-input.stories.tsx +++ b/packages/storybook/src/community/file-input.stories.tsx @@ -26,6 +26,9 @@ const meta = { // TODO: add Figma, GitHub and NL DesignSystem links componentOrigin: 'Dit component is volledig ontwikkeld door de Rijkshuisstijl Community.', }, + args: { + ref: undefined, + }, } satisfies Meta; export default meta; @@ -64,3 +67,21 @@ export const MetCustomElementenLijst: Story = { fileTypeErrorMessage: 'Dit bestandstype wordt niet toegestaan.', }, }; + +export const DefaultFiles: Story = { + args: { + buttonText: 'Bestanden kiezen', + maxFileSizeInBytes: 10_485_760, + allowedFileTypes: '.doc,.docx,.xlsx,.pdf,.zip,.jpg,.png,.bmp,.gif', + fileSizeErrorMessage: 'Dit bestand is groter dan 10 MB.', + fileTypeErrorMessage: 'Dit bestandstype wordt niet toegestaan.', + defaultFiles: [ + new File(['Example content for PDF file'], 'example.pdf', { + type: 'application/pdf', + }), + new File(['Example content for a text file'], 'example.txt', { + type: 'text/plain', + }), + ], + }, +};