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
Original file line number Diff line number Diff line change
Expand Up @@ -170,42 +170,41 @@ describe('useReferenceInputController', () => {
);

await waitFor(() => {
expect(children.mock.calls.length).toBeGreaterThanOrEqual(3);
expect(children).toHaveBeenCalledWith(
expect.objectContaining({
allChoices: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
availableChoices: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
selectedChoices: [{ id: 1, title: 'foo' }],
displayedFilters: {},
error: null,
filter: {},
filterValues: {},
isFetching: false,
isLoading: false,
page: 1,
perPage: 25,
hasPreviousPage: false,
hasNextPage: false,
hideFilter: expect.any(Function),
setFilters: expect.any(Function),
setPage: expect.any(Function),
setPerPage: expect.any(Function),
setSort: expect.any(Function),
showFilter: expect.any(Function),
sort: { field: 'title', order: 'ASC' },
refetch: expect.any(Function),
resource: 'posts',
source: 'post_id',
total: 2,
})
);
});
expect(children).toHaveBeenCalledWith(
expect.objectContaining({
allChoices: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
availableChoices: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
selectedChoices: [{ id: 1, title: 'foo' }],
displayedFilters: {},
error: null,
filter: {},
filterValues: {},
isFetching: false,
isLoading: false,
page: 1,
perPage: 25,
hasPreviousPage: false,
hasNextPage: false,
hideFilter: expect.any(Function),
setFilters: expect.any(Function),
setPage: expect.any(Function),
setPerPage: expect.any(Function),
setSort: expect.any(Function),
showFilter: expect.any(Function),
sort: { field: 'title', order: 'ASC' },
refetch: expect.any(Function),
resource: 'posts',
source: 'post_id',
total: 2,
})
);
});

it('should refetch reference getList when its props change', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { SimpleForm } from '../../form';
import { ArrayInput } from './ArrayInput';
import { TextInput } from '../TextInput';
import { SimpleFormIterator } from './SimpleFormIterator';
import { Basic } from './SimpleFormIterator.stories';
import { Basic, WithFormDataConsumer } from './SimpleFormIterator.stories';

describe('<SimpleFormIterator />', () => {
// bypass confirm leave form with unsaved changes
Expand Down Expand Up @@ -471,32 +471,7 @@ describe('<SimpleFormIterator />', () => {
});

it('should not reapply default values set at form level after removing and then re-adding one row, even with FormDataConsumer', async () => {
render(
<Wrapper>
<SimpleForm
defaultValues={{
emails: [{ email: 'test@marmelab.com', name: 'test' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator>
<FormDataConsumer>
{() => (
<>
<TextInput
source="email"
label="Email"
defaultValue="default@marmelab.com"
/>
<TextInput source="name" label="Name" />
</>
)}
</FormDataConsumer>
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Wrapper>
);
render(<WithFormDataConsumer />);

const removeFirstButton = getByLabelText(
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Button, Typography } from '@mui/material';
import { Button, Card, Typography } from '@mui/material';

import { Edit } from '../../detail';
import { SimpleForm } from '../../form';
Expand All @@ -9,6 +9,11 @@ import { TextInput } from '../TextInput';
import { AdminContext } from '../../AdminContext';
import { defaultTheme } from '../../theme/defaultTheme';
import { useSimpleFormIteratorItem } from './useSimpleFormIteratorItem';
import {
FormDataConsumer,
ResourceContextProvider,
testDataProvider,
} from 'ra-core';

export default { title: 'ra-ui-materialui/input/SimpleFormIterator' };

Expand Down Expand Up @@ -231,3 +236,34 @@ export const UseSimpleFormIteratorItem = () => (
</SimpleFormIterator>
</Wrapper>
);

export const WithFormDataConsumer = () => (
<AdminContext dataProvider={testDataProvider()}>
<ResourceContextProvider value="posts">
<Card>
<SimpleForm
defaultValues={{
emails: [{ email: 'test@marmelab.com', name: 'test' }],
}}
>
<ArrayInput source="emails">
<SimpleFormIterator>
<FormDataConsumer>
{() => (
<>
<TextInput
source="email"
label="Email"
defaultValue="default@marmelab.com"
/>
<TextInput source="name" label="Name" />
</>
)}
</FormDataConsumer>
</SimpleFormIterator>
</ArrayInput>
</SimpleForm>
</Card>
</ResourceContextProvider>
</AdminContext>
);
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const SimpleFormIterator = (inProps: SimpleFormIteratorProps) => {
!Children.only(children).props.source &&
// Make sure it's not a FormDataConsumer
// @ts-ignore
!Children.only(children).type !== FormDataConsumer
Children.only(children).type !== FormDataConsumer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to find a way to remove the ts-ignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is out of scope for this PR IMO

) {
// ArrayInput used for an array of scalar values
// (e.g. tags: ['foo', 'bar'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export const InsideReferenceArrayInputDefaultValue = ({
id: 1,
title: 'War and Peace',
// trigger default value
author: undefined,
authors: undefined,
summary:
"War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.",
year: 1869,
Expand Down
11 changes: 9 additions & 2 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ describe('<FilterButton />', () => {
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');

// wait for a bit before clicking the checkbox again
await new Promise(resolve => setTimeout(resolve, 510));
fireEvent.click(checkboxes[0]);

await waitFor(
Expand Down Expand Up @@ -103,6 +105,8 @@ describe('<FilterButton />', () => {
name: 'Title',
});

// wait for a bit before removing the filter
await new Promise(resolve => setTimeout(resolve, 510));
fireEvent.click(screen.getByTitle('Remove this filter'));

await waitFor(
Expand Down Expand Up @@ -135,6 +139,8 @@ describe('<FilterButton />', () => {

await screen.findByText('1-1 of 1');

// wait for a bit before changing the filters again
await new Promise(resolve => setTimeout(resolve, 510));
fireEvent.click(await screen.findByLabelText('Add filter'));
fireEvent.click(screen.getAllByRole('menuitemcheckbox')[2]);
fireEvent.change(
Expand All @@ -149,6 +155,8 @@ describe('<FilterButton />', () => {
'No Posts found using the current filters.'
);

// wait for a bit before changing the filters again
await new Promise(resolve => setTimeout(resolve, 510));
fireEvent.click(screen.getAllByTitle('Remove this filter')[1]);
await screen.findByText('1-1 of 1');

Expand All @@ -163,9 +171,8 @@ describe('<FilterButton />', () => {
{ timeout: 2000 }
);

// Wait for a bit
// wait for a bit before changing the filters again
await new Promise(resolve => setTimeout(resolve, 510));

fireEvent.click(screen.getByTitle('Remove this filter'));
await screen.findByText('1-10 of 13');

Expand Down
24 changes: 1 addition & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9947,18 +9947,6 @@ __metadata:
languageName: node
linkType: hard

"fdir@npm:^6.4.3":
version: 6.4.3
resolution: "fdir@npm:6.4.3"
peerDependencies:
picomatch: ^3 || ^4
peerDependenciesMeta:
picomatch:
optional: true
checksum: d13c10120e9625adf21d8d80481586200759928c19405a816b77dd28eaeb80e7c59c5def3e2941508045eb06d34eb47fad865ccc8bf98e6ab988bb0ed160fb6f
languageName: node
linkType: hard

"fdir@npm:^6.4.4":
version: 6.4.4
resolution: "fdir@npm:6.4.4"
Expand Down Expand Up @@ -18685,17 +18673,7 @@ __metadata:
languageName: node
linkType: hard

"tinyglobby@npm:^0.2.12":
version: 0.2.12
resolution: "tinyglobby@npm:0.2.12"
dependencies:
fdir: "npm:^6.4.3"
picomatch: "npm:^4.0.2"
checksum: 7c9be4fd3625630e262dcb19015302aad3b4ba7fc620f269313e688f2161ea8724d6cb4444baab5ef2826eb6bed72647b169a33ec8eea37501832a2526ff540f
languageName: node
linkType: hard

"tinyglobby@npm:^0.2.13":
"tinyglobby@npm:^0.2.12, tinyglobby@npm:^0.2.13":
version: 0.2.13
resolution: "tinyglobby@npm:0.2.13"
dependencies:
Expand Down
Loading