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

Introduce <FilterLiveForm> #10344

Merged
merged 33 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
55555fd
wip: first working version
slax57 Nov 12, 2024
8c3bec7
wip: own story and defaultValues
slax57 Nov 12, 2024
8acaf9e
wip: more stories and fix per input validation
slax57 Nov 12, 2024
612ebc1
wip: story with ReferenceInput
slax57 Nov 12, 2024
c68e047
wip: add resettable everywhere
slax57 Nov 12, 2024
8c8d011
wip: add AsListActions story
slax57 Nov 12, 2024
ae9faa0
introduce FilterListWrapper and make stories more beautiful
slax57 Nov 12, 2024
af34890
move AutoSubmitFilterForm to ra-core
slax57 Nov 12, 2024
41780ed
trigger debounce bug in stories
slax57 Nov 14, 2024
e1342d4
fix and debug code
slax57 Nov 14, 2024
159cfd8
fix fix
slax57 Nov 14, 2024
3b0cb94
remove useless useListParams stories
slax57 Nov 14, 2024
8164dfb
apply fix to AutoSubmitFilterForm and remove tech debt from FilterForm
slax57 Nov 14, 2024
1512a1d
fix unit test
slax57 Nov 14, 2024
0c136e3
add missing FormGroupsProvider
slax57 Nov 14, 2024
719fc75
refactor FilterLiveSearch to use AutoSubmitFilterForm + fix AutoSubmi…
slax57 Nov 14, 2024
762b687
add FilterLiveSearch FullApp story
slax57 Nov 15, 2024
5362261
add filterlivesearch reapply old value bug unit test
slax57 Nov 15, 2024
47384c8
use AutoSubmitFilterForm in FilterForm
slax57 Nov 15, 2024
5b58e7e
improve AutoSubmitFilterForm component prop default value
slax57 Nov 15, 2024
b6b8eaf
add tests for AutoSubmitFilterForm
slax57 Nov 15, 2024
01c5429
add test for race condition bug
slax57 Nov 15, 2024
e8005b1
remove debug code
slax57 Nov 15, 2024
fbb3837
add docs for AutoSubmitFilterForm
slax57 Nov 15, 2024
482e83d
improve discoverability
slax57 Nov 15, 2024
0946f6e
fix Liquid syntax error
slax57 Nov 15, 2024
e34fa24
code review
slax57 Nov 20, 2024
36d5a45
move AutoSubmitFilterForm in form folder
slax57 Nov 20, 2024
eeee44f
reintroduce unset
slax57 Nov 20, 2024
fc30c5a
rename component to formComponent
slax57 Nov 20, 2024
9f0ef08
rename FilterListWrapper to FilterListSection
slax57 Nov 20, 2024
05e926f
rename AutoSubmitFilterForm to FilterLiveForm
slax57 Nov 20, 2024
7fd23c6
rename assets
slax57 Nov 20, 2024
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
@@ -0,0 +1,35 @@
import { getFilterFormValues } from './AutoSubmitFilterForm';

describe('<AutoSubmitFilterForm />', () => {
describe('getFilterFormValues', () => {
it('should correctly get the filter form values from the new filterValues', () => {
const currentFormValues = {
classicToClear: 'abc',
nestedToClear: { nestedValue: 'def' },
classicUpdated: 'ghi',
nestedUpdated: { nestedValue: 'jkl' },
nestedToSet: { nestedValue: undefined },
published_at: new Date('2022-01-01T03:00:00.000Z'),
clearedDateValue: null,
};
const newFilterValues = {
classicUpdated: 'ghi2',
nestedUpdated: { nestedValue: 'jkl2' },
nestedToSet: { nestedValue: 'mno2' },
published_at: '2022-01-01T03:00:00.000Z',
};

expect(
getFilterFormValues(currentFormValues, newFilterValues)
).toEqual({
classicToClear: '',
nestedToClear: { nestedValue: '' },
classicUpdated: 'ghi2',
nestedUpdated: { nestedValue: 'jkl2' },
nestedToSet: { nestedValue: 'mno2' },
published_at: '2022-01-01T03:00:00.000Z',
clearedDateValue: '',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,37 @@ import {
ValidateForm,
} from '../../../form';
import { useDebouncedEvent, useEvent } from '../../../util';
import { useListFilterContext } from '../useListFilterContext';
import { useListContext } from '../useListContext';

/**
* TODO
*/
export const AutoSubmitFilterForm = (props: AutoSubmitFilterFormProps) => {
const { filterValues, setFilters } = useListFilterContext();
const { filterValues, setFilters } = useListContext();
const resource = useResourceContext(props);

const { debounce = 500, resolver, validate, children, ...rest } = props;
const {
debounce = 500,
resolver,
validate,
Copy link
Contributor

Choose a reason for hiding this comment

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

When you have a non validated input you can't submit. But if you submit your FilterList in a same time, it will reset your AutoSubmitForm to the last validated state.
Do we need to consider it as an issue or as a normal way of work @fzaninotto @djhi ?

Capture.video.2024-11-18.11.19.16.mp4

Copy link
Member

Choose a reason for hiding this comment

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

IMO this isn't a bug

children,
component: Component,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why don't you provide a default value here and avoid the ternary at L124?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know how to do it, but now I know! Thanks!

...rest
} = props;

const finalResolver = resolver
? resolver
: validate
? getSimpleValidationResolver(validate)
: undefined;

const form = useForm({
const formContext = useForm({
mode: 'onChange',
defaultValues: filterValues,
resolver: finalResolver,
...rest,
});
const { handleSubmit, getValues, reset, watch, formState } = form;
const { handleSubmit, getValues, reset, watch, formState } = formContext;
const { isValid } = formState;

// Ref tracking if there are internal changes pending, i.e. changes that
Expand Down Expand Up @@ -111,10 +118,18 @@ export const AutoSubmitFilterForm = (props: AutoSubmitFilterFormProps) => {
);

return (
<FormProvider {...form}>
<FormProvider {...formContext}>
<FormGroupsProvider>
<SourceContextProvider value={sourceContext}>
<form onSubmit={handleSubmit(onSubmit)}>{children}</form>
{Component ? (
<Component onSubmit={handleSubmit(onSubmit)}>
{children}
</Component>
) : (
<form onSubmit={handleSubmit(onSubmit)}>
{children}
</form>
)}
</SourceContextProvider>
</FormGroupsProvider>
</FormProvider>
Expand All @@ -127,6 +142,7 @@ export interface AutoSubmitFilterFormProps
validate?: ValidateForm;
debounce?: number | false;
resource?: string;
component?: React.ComponentType<any>;
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -136,7 +152,7 @@ export interface AutoSubmitFilterFormProps
* and due to the dynamic nature of the filter form, we rebuild the filter form values from its current
* values and make sure to pass at least an empty string for each input.
*/
const getFilterFormValues = (
export const getFilterFormValues = (
formValues: Record<string, any>,
filterValues: Record<string, any>
) => {
Expand Down
40 changes: 2 additions & 38 deletions packages/ra-ui-materialui/src/list/filter/FilterForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
WithAutoCompleteArrayInput,
WithComplexValueFilter,
} from './FilterButton.stories';
import { FilterForm, getFilterFormValues } from './FilterForm';
import { FilterForm } from './FilterForm';

describe('<FilterForm />', () => {
const defaultListContext = {
Expand Down Expand Up @@ -125,11 +125,7 @@ describe('<FilterForm />', () => {
target: { value: 'foo' },
});
await waitFor(() => {
expect(setFilters).toHaveBeenCalledWith(
{ title: 'foo' },
{ title: true },
true
);
expect(setFilters).toHaveBeenCalledWith({ title: 'foo' });
});
});

Expand Down Expand Up @@ -295,38 +291,6 @@ describe('<FilterForm />', () => {
await screen.findByText('1-2 of 2');
});

describe('getFilterFormValues', () => {
it('should correctly get the filter form values from the new filterValues', () => {
const currentFormValues = {
classicToClear: 'abc',
nestedToClear: { nestedValue: 'def' },
classicUpdated: 'ghi',
nestedUpdated: { nestedValue: 'jkl' },
nestedToSet: { nestedValue: undefined },
published_at: new Date('2022-01-01T03:00:00.000Z'),
clearedDateValue: null,
};
const newFilterValues = {
classicUpdated: 'ghi2',
nestedUpdated: { nestedValue: 'jkl2' },
nestedToSet: { nestedValue: 'mno2' },
published_at: '2022-01-01T03:00:00.000Z',
};

expect(
getFilterFormValues(currentFormValues, newFilterValues)
).toEqual({
classicToClear: '',
nestedToClear: { nestedValue: '' },
classicUpdated: 'ghi2',
nestedUpdated: { nestedValue: 'jkl2' },
nestedToSet: { nestedValue: 'mno2' },
published_at: '2022-01-01T03:00:00.000Z',
clearedDateValue: '',
});
});
});

it('should not reapply previous filter form values when clearing nested AutocompleteArrayInput', async () => {
render(<WithAutoCompleteArrayInput />);

Expand Down
Loading
Loading