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

Update <SelectInput> to support an array of strings as choices #10038

Merged
merged 2 commits into from
Jul 23, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jul 22, 2024

Problem

For SelectInput and other selection inputs, sometimes the list of options is simply an array of strings. Doing the conversion to an array of option objects isn't intuitive.

Solution

Support string choices by default.

<SelectInput source="gender" choices={['Male', 'Female']} />

This concerns the following components:

  • SelectInput
  • SelectArrayInput
  • AutocompleteInput
  • AutocompleteArrayInput
  • RadioButtonGroupInput
  • CheckboxGroupInput

@fzaninotto fzaninotto added the RFR Ready For Review label Jul 22, 2024
@@ -130,6 +131,14 @@ describe('<SelectInput />', () => {
expect(options.length).toEqual(6);
expect(options[1].textContent).toEqual('Created');
});

it('should accept strings as choices', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I only tested SelectInput because the logic is shared by all selection inputs via useChoicesContext.

@djhi djhi self-requested a review July 23, 2024 07:44
@adguernier adguernier self-requested a review July 23, 2024 07:45
@@ -4,14 +4,20 @@ import { useList } from '../../controller';
import { ChoicesContext, ChoicesContextValue } from './ChoicesContext';

export const useChoicesContext = <ChoicesType extends RaRecord = RaRecord>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised this doesn't trigger a TS error as RaRecord can't be a string. I believe this is because ChoicesProps in useChoices accepts any[] for choices. This might be an issue later

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't type the choices as users may pass anything and set the optionText and optionValue. It's never been an issue ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

We should not restrict the ChoicesType then but let's do that in another PR

docs/SelectArrayInput.md Outdated Show resolved Hide resolved
docs/CheckboxGroupInput.md Outdated Show resolved Hide resolved
docs/AutocompleteArrayInput.md Outdated Show resolved Hide resolved
Co-authored-by: adrien guernier <adrien@marmelab.com>
@djhi djhi added this to the 5.1.0 milestone Jul 23, 2024
@djhi djhi merged commit 26c4348 into next Jul 23, 2024
14 checks passed
@djhi djhi deleted the choices-string-array branch July 23, 2024 09:45
@fzaninotto fzaninotto changed the title Update SelectInput to support an array of strings as choices Update <SelectInput> to support an array of strings as choices Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants