Skip to content

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

Merged
djhi merged 2 commits intonextfrom
choices-string-array
Jul 23, 2024
Merged

Update <SelectInput> to support an array of strings as choices#10038
djhi merged 2 commits intonextfrom
choices-string-array

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
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

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
@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

Development

Successfully merging this pull request may close these issues.

3 participants