react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

SelectInput / ReferenceInput: raise an error if default-value is out of range

Open ZachSelindh opened this issue 3 years ago • 8 comments

What you were expecting: No MUI errors when using ReferenceInputs

What happened instead: When SelectInput is used as a child of ReferenceInput, and setValue or defaultValues falls outside the possible choices, a MUI warning is triggered. In my thinking, the setValue call or defaultValue prop should be ignored if it does not relate to a current possible choice, which would presumably negate the error.

Steps to reproduce: Add a SelectInput as a child of a ReferenceInput and pass the SelectInput a defaultValue that does not refer to a current choice option.

Related code: https://codesandbox.io/s/blissful-cartwright-gs42vu?file=/src/posts/PostCreate.tsx

Environment

  • React-admin version: 4.0.1
  • Last version that did not exhibit the issue (if applicable): 3.19
  • React version: 17
  • Browser: Chrome
  • Stack trace (in case of a JS error):

ZachSelindh avatar Apr 29 '22 22:04 ZachSelindh

Hi! I'm not sure about this one. Isn't it a good thing to get a warning for this case? It might be an unwanted mistake that the developer would be happy to know about? @djhi and @fzaninotto what do you think?

slax57 avatar May 02 '22 08:05 slax57

I tend to agree, it seems that the warning is legit in that case.

Can you tell us more about your use case? How come you have a defaultValue that isn't a possible reference?

fzaninotto avatar May 02 '22 09:05 fzaninotto

Sure! One common case for us is trying to use local storage-saved user data to populate default form values. So, for example, if a user is on Team Red, we want to automatically enter Team Red for them on all forms, where applicable. But if Team Red isn't authorized to do certain tasks as a team that their individual users can (and that's reflected in the ReferenceInput's filters), then the error will be thrown when we attempt to set that default value, since it doesn't match the choices created using the filters.

I can see why the warning might be valid, and I'm certainly open to suggestions on how to programmatically determine if a value is in-bounds for RA's ReferenceInputs so that invalid values won't be attempted.

ZachSelindh avatar May 03 '22 15:05 ZachSelindh

Thank you for your answer. I haven't tested it, but I'd say you probably can use the ChoicesContext provided by ReferenceInput (with useChoicesContext) to access the available choices and check if your default data is in there. Since it's not really a bug with RA I'll be closing this issue.

slax57 avatar May 04 '22 08:05 slax57

@slax57 Don't mean to resurrect this issue if this isn't intended behavior, but I've noticed that ReferenceInputs with SelectInputs as children who receive "out-of-bounds"/not within choices values for defaultValues will still update the form values in the background, even as the form input remains blank. Would you consider this a bug, that the form state is updating with values from defaultValues that aren't possible references?

ZachSelindh avatar May 11 '22 14:05 ZachSelindh

Indeed this is a good question. I agree it would be nice to enforce the selected value being one of the choices at least at submission time. I'll reopen this as an enhancement request, and will also update the issue title accordingly.

slax57 avatar May 16 '22 09:05 slax57

Don't know if it's related but when using <SelectInput isLoading={!choices} choices={choices} > and refreshing the page, it will render correctly because it will show only once choices are available. But when going from list page to show or edit, it crashes saying that it cannot read map from finalChoices. So I have to do <SelectInput isLoading={!choices} choices={!choices ? [] : choices} > instead.

Again refreshing the page is ok, but then I have the out-of-range warning when coming from list page to show or edit which it's kind of weird, because choices is not null, it's a real array and it's defined, it's not supposed to crash/warn because when loading, it returns a <LinearProgress />

https://github.com/marmelab/react-admin/blob/master/packages/ra-ui-materialui/src/input/SelectInput.tsx#L236

newfylox avatar Jul 06 '22 12:07 newfylox

@newfylox I'm not entirely sure this is related. Could you open a new issue with a reproduction case so that we can investigate on this? Thanks

slax57 avatar Jul 07 '22 07:07 slax57

This feature request has received no support from the community, and is not on the core team roadmap. I'm closing the issue, but feel free to open a PR if you have implemented the feature.

fzaninotto avatar Jan 23 '24 20:01 fzaninotto