From 5b9b5994266c4d850e4059d152cb00b452be23e3 Mon Sep 17 00:00:00 2001 From: tkow Date: Thu, 16 Dec 2021 21:17:44 +0900 Subject: [PATCH 1/9] feat: add onSubmitRemove prop --- packages/ra-ui-materialui/src/input/FileInput.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/FileInput.tsx b/packages/ra-ui-materialui/src/input/FileInput.tsx index 6824d13e479..8f2c814f41f 100644 --- a/packages/ra-ui-materialui/src/input/FileInput.tsx +++ b/packages/ra-ui-materialui/src/input/FileInput.tsx @@ -33,6 +33,7 @@ export const FileInput = ( maxSize, minSize, multiple = false, + onSubmitRemove, options: { inputProps: inputPropsOptions, ...options @@ -111,7 +112,11 @@ export const FileInput = ( } }; - const onRemove = file => () => { + const onRemove = file => async () => { + if (onSubmitRemove) { + const isSuccess = await onSubmitRemove(file); + if (!isSuccess) return; + } if (multiple) { const filteredFiles = files.filter( stateFile => !shallowEqual(stateFile, file) @@ -216,6 +221,7 @@ FileInput.propTypes = { maxSize: PropTypes.number, minSize: PropTypes.number, multiple: PropTypes.bool, + onSubmitRemove: PropTypes.func, options: PropTypes.object, resource: PropTypes.string, source: PropTypes.string, @@ -250,6 +256,7 @@ export interface FileInputProps DropzoneOptions, 'accept' | 'multiple' | 'maxSize' | 'minSize' > { + onSubmitRemove?(file): boolean | Promise; children?: ReactNode; labelMultiple?: string; labelSingle?: string; From 4587a5a1db832f9fc30b9710b913b8b36bf494d2 Mon Sep 17 00:00:00 2001 From: tkow Date: Thu, 16 Dec 2021 21:18:25 +0900 Subject: [PATCH 2/9] feat: add onSubmitRemove prop test --- .../src/input/FileInput.spec.tsx | 216 +++++++++++++++++- 1 file changed, 214 insertions(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/FileInput.spec.tsx b/packages/ra-ui-materialui/src/input/FileInput.spec.tsx index ca2c9314f2b..6dc6dc185b6 100644 --- a/packages/ra-ui-materialui/src/input/FileInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/FileInput.spec.tsx @@ -1,7 +1,11 @@ import * as React from 'react'; -import { render, fireEvent } from '@testing-library/react'; +import { + render, + fireEvent, + waitForElementToBeRemoved, + waitFor, +} from '@testing-library/react'; import { Form } from 'react-final-form'; - import { FileField, ImageField } from '../field'; import { FileInput } from './FileInput'; @@ -247,6 +251,214 @@ describe('', () => { }); }); + describe('should stop to remove file field onSubmitReview return false', () => { + it('normal function', async () => { + const onSubmit = jest.fn(); + + const { getByLabelText, getByTitle } = render( +
( + + false} + > + + +