marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
25.02k stars 5.26k forks source link

useFilterState does not update permanentFilter value if changed #4473

Closed fancyaction closed 4 years ago

fancyaction commented 4 years ago

What you were expecting: If I use ReferenceInput with AutocompleteInput and set filter prop (aka permanentFilter) to a variable, data filter should update alongside the variable.

Example: I have user select input for author_id, in the following ReferenceInput->AutoComplete Input for books I want to filter for whatever author_id was selected (pulling value either from formState.values or FormDataConsumer's formData) and filterToQuery on book's title: filter={ formData && formData.author_id ? {author_id: formData.author_id} : {} } filterToQuery={(searchText: string) => ({ title: searchText })}

What happened instead: useFilterState is always setting permanentFilter to whatever it originally loaded in with. In the above case, it will always be undefined because formData.author_id doesn't update until user selects author. Once they select author_id=23 it will still be read from the original undefined value.

Steps to reproduce: Set a RefeneceInput's filter prop to another input on form (see above example or CodePen below)

Related code: Please go to posts -> PostCreate.js for code example: https://codesandbox.io/s/focused-snyder-ereg7

It's hard to demonstrate fully with data set there but if you open console you'll see on tag_id select an initial call is made with "filter":{"tag_id":16,"body":""}} but every following call looks like "filter":{"body":"test"}} because tag_id (taken from dataForm.tag_id) is read as undefined in useFilterState.

       <ReferenceInput label="Tag" source="tag_id" reference="tags">
          <AutocompleteInput />
        </ReferenceInput>
        <FormDataConsumer>
          {({ formData, ...rest }) => (
            <ReferenceInput
              label="Comment"
              source="comment_id"
              reference="comments"
              filter={{ tag_id: formData.tag_id }}
              // filter={formData.tag_id ? { tag_id: formData.tag_id } : {}}
              filterToQuery={searchText => ({ body: searchText })}
              {...rest}
            >
              <AutocompleteInput
                shouldRenderSuggestions={val => 2 < val.trim().length}
                optionText="body"
              />
            </ReferenceInput>
          )}
        </FormDataConsumer>

Other information: In useFilterState, replacing permanentFilter with permanentFilterProp.current (e.g. latest filter prop value) in setFilterValue seems to fix but unsure if that could cause issues elsewhere.

This may very well relate to issue #3921

This is my first issue here so please let me know if I can clear anything up. React-Admin is an invaluable part of my team's app and we very much value all the hard work you do here!

Environment

djhi commented 4 years ago

Thanks for reporting. Would you sending a pull request to fix it ?

fzaninotto commented 4 years ago

Probably fixed by #4650

Kmaschta commented 4 years ago

@fancyaction You'll be able to check if your issue is fixed with the 3.4.1, feel free to reopen if it's not the case