diff --git a/packages/ra-core/src/form/Form.spec.tsx b/packages/ra-core/src/form/Form.spec.tsx index 41ca74d265a..fb6fd9e8929 100644 --- a/packages/ra-core/src/form/Form.spec.tsx +++ b/packages/ra-core/src/form/Form.spec.tsx @@ -809,18 +809,54 @@ describe('Form', () => { expectedValue: 'from-search', }, ])( - 'should support overriding the record values from the location $from', + 'should support prefilling the from values from the location $from', async ({ url, expectedValue }) => { render(); - await screen.findByDisplayValue('lorem'); + expect( + (await screen.findByLabelText('title')).value + ).toEqual(''); expect( (screen.getByText('Submit') as HTMLInputElement).disabled ).toEqual(false); fireEvent.click(screen.getByText('Settings')); await screen.findByDisplayValue(expectedValue); + expect( + screen.getByText('Submit').disabled + ).toEqual(false); + } + ); + it.each([ + { + from: 'state', + url: { + pathname: '/form/general', + state: { record: { body: 'from-state' } }, + }, + expectedValue: 'from-state', + }, + { + from: 'search query', + url: `/form/general?source=${encodeURIComponent(JSON.stringify({ body: 'from-search' }))}` as To, + expectedValue: 'from-search', + }, + ])( + 'should support overriding the record values from the location $from', + async ({ url, expectedValue }) => { + render( + + ); + await screen.findByDisplayValue('lorem'); expect( (screen.getByText('Submit') as HTMLInputElement).disabled ).toEqual(false); + fireEvent.click(screen.getByText('Settings')); + await screen.findByDisplayValue(expectedValue); + expect( + screen.getByText('Submit').disabled + ).toEqual(false); } ); }); diff --git a/packages/ra-core/src/form/Form.stories.tsx b/packages/ra-core/src/form/Form.stories.tsx index 69c4a165d30..62386fe9578 100644 --- a/packages/ra-core/src/form/Form.stories.tsx +++ b/packages/ra-core/src/form/Form.stories.tsx @@ -22,8 +22,9 @@ import { Form } from './Form'; import { useInput } from './useInput'; import { required, ValidationError } from './validation'; import { mergeTranslations } from '../i18n'; -import { I18nProvider } from '../types'; +import { I18nProvider, RaRecord } from '../types'; import { + RecordContextProvider, SaveContextProvider, TestMemoryRouter, useNotificationContext, @@ -415,11 +416,24 @@ export const ServerSideValidation = () => { ); }; -export const MultiRoutesForm = ({ url }: { url?: any }) => ( +export const MultiRoutesForm = ({ + url, + initialRecord, +}: { + url?: any; + initialRecord?: Partial; +}) => ( - } /> + + + + } + /> @@ -427,6 +441,7 @@ export const MultiRoutesForm = ({ url }: { url?: any }) => ( MultiRoutesForm.args = { url: 'unmodified', + initialRecord: 'none', }; MultiRoutesForm.argTypes = { @@ -446,17 +461,22 @@ MultiRoutesForm.argTypes = { }, control: { type: 'select' }, }, + initialRecord: { + options: ['none', 'provided'], + mapping: { + none: undefined, + provided: { title: 'lorem', body: 'unmodified' }, + }, + control: { type: 'select' }, + }, }; -const record = { title: 'lorem', body: 'unmodified' }; const FormWithSubRoutes = () => { return ( - <> -
- - - - +
+ + + ); };