Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#153 - Restricting DragZoneFiled File Upload to a Single File #154

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion libs/formik-elements/src/DragZoneField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export type DragZoneFieldProps<T extends File> = {
* The accessor value for the component (for validation, fetching, etc.).
*/
name: string;

/**
* Controls whether the file upload is restricted to a single file.
* If set to true, only one file can be uploaded at a time.
* @default false
*/
singleFileUpload?: boolean;
} & DragZoneProps<T>;

export default function DragZoneField<T extends File>({ name, ...props }: DragZoneFieldProps<T>) {
Expand All @@ -40,7 +47,9 @@ export default function DragZoneField<T extends File>({ name, ...props }: DragZo
React.useEffect(() => {
props.hook.onUpdateCallback((added, deleted) => {
if (added) {
helpers.setValue([...(fieldValue.current || []), ...(Array.isArray(added) ? added : [added])]);
const addedFiles = Array.isArray(added) ? added : [added];
const updatedFiles = props.singleFileUpload ? addedFiles : [...(fieldValue.current || []), ...addedFiles];
helpers.setValue(updatedFiles);
} else if (deleted) {
helpers.setValue(
(fieldValue.current || []).filter((id) => (Array.isArray(deleted) ? !deleted.includes(id) : id !== deleted))
Expand Down
11 changes: 10 additions & 1 deletion libs/formik-elements/src/UploadButtonField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export type UploadButtonFieldProps<T extends File> = {
* The accessor value for the component (for validation, fetching, etc.).
*/
name: string;

/**
* Controls whether the file upload is restricted to a single file.
* If set to true, only one file can be uploaded at a time.
* @default false
*/
singleFileUpload?: boolean;
} & UploadButtonProps<T>;

export default function UploadButtonField<T extends File>({ name, ...props }: UploadButtonFieldProps<T>) {
Expand All @@ -40,7 +47,9 @@ export default function UploadButtonField<T extends File>({ name, ...props }: Up
React.useEffect(() => {
props.hook.onUpdateCallback((added, deleted) => {
if (added) {
helpers.setValue([...(fieldValue.current || []), ...(Array.isArray(added) ? added : [added])]);
const addedFiles = Array.isArray(added) ? added : [added];
const updatedFiles = props.singleFileUpload ? addedFiles : [...(fieldValue.current || []), ...addedFiles];
helpers.setValue(updatedFiles);
} else if (deleted) {
helpers.setValue(
(fieldValue.current || []).filter((id) => (Array.isArray(deleted) ? deleted.includes(id) : id !== deleted))
Expand Down
15 changes: 15 additions & 0 deletions storybook/src/formik-elements/DragZoneField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ export const AndFileList = () => {
);
};

export const WithSingleFileUpload = () => {
ivam5 marked this conversation as resolved.
Show resolved Hide resolved
const useFileUploadHook = useFileUpload();

return (
<DragZoneField
name={name}
hook={useFileUploadHook}
url={useMockSender.destination.url}
send={useMockSender.send}
title={<Intl name="dragZoneTitle" />}
singleFileUpload={true}
/>
);
};

export const WithLabel = () => {
const useFileUploadHook = useFileUpload();

Expand Down
9 changes: 9 additions & 0 deletions storybook/src/formik-elements/UploadButtonField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ export const Example = () => {
</UploadButtonField>
);
};

export const WithSingleFileUpload = () => {
const file = useFileUpload();
return (
<UploadButtonField hook={file} name={name} url={useMockSender.destination.url} send={useMockSender.send} singleFileUpload={true}>
Upload
</UploadButtonField>
);
};