Skip to content

Commit

Permalink
Enhance MuiSelectInput to support string options (#677)
Browse files Browse the repository at this point in the history
Signed-off-by: Ayoub LABIDI <[email protected]>
  • Loading branch information
ayolab authored Jan 28, 2025
1 parent fe0fa1a commit 0ff1ca7
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ import { useController } from 'react-hook-form';

export type MuiSelectInputProps = SelectProps & {
name: string;
options: { id: string; label: string }[];
options: { id: string; label: string }[] | string[];
};

function renderMenuItem(option: { id: string; label: string } | string) {
const key = typeof option === 'string' ? option : option.id;
const value = key;
const label = typeof option === 'string' ? option : <FormattedMessage id={option.label} />;

return (
<MenuItem key={key} value={value}>
{label}
</MenuItem>
);
}

// This input use Mui select instead of Autocomplete which can be needed some time (like in FormControl)
export function MuiSelectInput({ name, options, ...props }: MuiSelectInputProps) {
const {
Expand All @@ -24,11 +36,7 @@ export function MuiSelectInput({ name, options, ...props }: MuiSelectInputProps)

return (
<Select value={value} onChange={onChange} {...props}>
{options.map((option) => (
<MenuItem key={option.id ?? option.label} value={option.id ?? option}>
<FormattedMessage id={option.label ?? option} />
</MenuItem>
))}
{options.map(renderMenuItem)}
</Select>
);
}

0 comments on commit 0ff1ca7

Please sign in to comment.