React-admin: Unable to reset all the choices in SelectInput on Change of FormData

Created on 15 Oct 2018  路  8Comments  路  Source: marmelab/react-admin

What you were expecting:
I'm rendering a RefrenceInput inside the FormDataConsumer, If there are any changes in FormData , the
ReferenceInput have to load Options to select , and reset the selected values already.

What happened instead:
The ReferenceInput loading options But the previously selected option remain in the list.

Steps to reproduce:
Use the RefrenceInput inside the FormDataConsumer

Related code:

<FormDataConsumer>
     {({ formData, ...rest }) =>
         formData ? formData.school_province &&
             <ReferenceInput label="District" source="school_district" reference="options" filter={{ table: 'District_List', filter: ['province_id,=,' + formData.school_province] }}>
                 <SelectInput optionText="district" {...rest} />
             </ReferenceInput>
             : ''
      }
</FormDataConsumer>

Environment

Most helpful comment

In fact, After thinking a bit more about it, the problem does not lie with the options, but with the value...

In the example of state/city dropdowns, given that you selected state A and city A1, then if you select state B, the value of city should reset to undefined since A1 isn't in state B. In that case, react-admin wouldn't display the previous value in the list.

State  [A] B C D
City [A1] A2 A3 A4

State  A [B] C D
City [ ] B1 B2 B3 B4

Now what's left to decide if it's react-admin's job or not to remove the value if it is not in the list of possible options. By default, it doesn't make sense (because the list of options is limited, and it can be very resource consuming to make sure that the value is a possible option). But we could make that optional.

I think that it would need to be in react-admin if there were no simple way to do it otherwise. I need to study redux-form deeper to decide.

All 8 comments

I have the same problem. It seems like it is an issue with the referenceinput.

Have the same exact problem.
Here's the simple sandbox - https://codesandbox.io/s/p3qq4xrx0j
(I take it from another issue with FormData and updated react-admin version to the 2.4.1).

any solution / workaround for this issue? I think it doesn't make any sense to keep values once the filter is changed

Thanks for the CodeSandbox. Bug reproduced and confirmed.

Sorry, I misunderstood the problem.

What you describe isn't a bug, it's a feature. The currently selected option must always remain in the list of suggested options, even though it doesn't match the filter anymore, because otherwise it can't be selected anymore... That's a choice we made when designing the <ReferenceInput> component.

In fact, After thinking a bit more about it, the problem does not lie with the options, but with the value...

In the example of state/city dropdowns, given that you selected state A and city A1, then if you select state B, the value of city should reset to undefined since A1 isn't in state B. In that case, react-admin wouldn't display the previous value in the list.

State  [A] B C D
City [A1] A2 A3 A4

State  A [B] C D
City [ ] B1 B2 B3 B4

Now what's left to decide if it's react-admin's job or not to remove the value if it is not in the list of possible options. By default, it doesn't make sense (because the list of options is limited, and it can be very resource consuming to make sure that the value is a possible option). But we could make that optional.

I think that it would need to be in react-admin if there were no simple way to do it otherwise. I need to study redux-form deeper to decide.

I solved resetting the value using redux-form

  1. import change from redux-form

    • import {change } from 'redux-form'

  2. give my simple form a name

    • <SimpleForm form="myFrm"

  3. i found that FormDataConsumer passes dispatch func. on rest object so i used it

    • onChange={() => rest.dispatch(change("myFrm", "myField", ""))}

here is my code . hope it can help any one
image

That's the right way to do, thanks. As it's possible without any change in react-admin, I'm closing this bug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phacks picture phacks  路  3Comments

ericwb picture ericwb  路  3Comments

Dragomir-Ivanov picture Dragomir-Ivanov  路  3Comments

yangjiamu picture yangjiamu  路  3Comments

samanmohamadi picture samanmohamadi  路  3Comments