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

Fix support for checkbox in select #568

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 44 additions & 28 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import TextField, { TextFieldProps } from './TextField';
import Autocomplete, { AutocompleteProps, OptionsType } from './Autocomplete';
import { forwardRef, Ref } from 'react';
import Typography from './Typography';
import Checkbox from './Checkbox';
import React from 'react';

type CommonProps = {
options?: AutocompleteProps['options'];
Expand Down Expand Up @@ -34,7 +36,7 @@ const Select = (
...props
}: SelectProps,
ref: Ref<HTMLDivElement>
): JSX.Element => {
): React.JSX.Element => {
if (multiple) {
return (
<Autocomplete
Expand All @@ -49,10 +51,17 @@ const Select = (
/>
);
}

const selectProps = 'SelectProps' in props ? props.SelectProps : {};
const isMultiple = 'SelectProps' in props && props.SelectProps?.multiple;

return (
<TextField
select
startAdornment={adornment}
inputProps={{ 'data-testid': testid }}
ref={ref}
{...(props as SingleSelectProps)}
SelectProps={{
MenuProps: {
anchorOrigin: {
Expand All @@ -63,36 +72,43 @@ const Select = (
vertical: 'top',
horizontal: 'left',
},
...selectProps?.MenuProps,
},
renderValue: (value) => options.find((o) => o.id == value)?.value,
renderValue: (value) => {
if (value instanceof Array) return value.map((v) => options.find((o) => o.id == v)?.value).join(', ');

return options.find((o) => o.id == value)?.value;
},
...selectProps,
}}
inputProps={{ 'data-testid': testid }}
{...(props as SingleSelectProps)}
ref={ref as Ref<HTMLDivElement>}
>
{options.map(({ id, value: label, description, disabled }) => (
<MenuItem
key={id}
value={id}
disabled={disabled}
sx={{
...(description && {
flexDirection: 'column',
alignItems: 'flex-start',
}),
...(optionDivider && {
'&:not(:last-child)': { borderBottom: '1px solid rgba(0,0,0,.23)' },
}),
}}
>
{label}
{description && (
<Typography variant="caption" color="grey.400">
{description}
</Typography>
)}
</MenuItem>
))}
{options.map(({ id, value: label, description, disabled }) => {
const isChecked = isMultiple && (props.value as Array<string | number>).map((i) => i.toString()).indexOf(id.toString()) > -1;

return (
<MenuItem
key={id}
value={id}
disabled={disabled}
sx={{
...(description && {
flexDirection: 'column',
alignItems: 'flex-start',
}),
...(optionDivider && {
'&:not(:last-child)': { borderBottom: '1px solid rgba(0,0,0,.23)' },
}),
}}
>
{checkbox && (<Checkbox checked={isChecked} />)}
{label}
{description && (
<Typography variant="caption" color="grey.400">
{description}
</Typography>
)}
</MenuItem>
)})}
</TextField>
);
};
Expand Down
9 changes: 6 additions & 3 deletions stories/Inputs/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default {
} as Meta;

const SelectTemplate: StoryFn<SelectProps> = (args: SelectProps) => {
const [selectValue, setSelectValue] = useState('0');
const isMultiple = Boolean('SelectProps' in args && args.SelectProps?.multiple);
const [selectValue, setSelectValue] = useState<Array<string | number> | string | number>(isMultiple ? [0] : 0);

args.value = selectValue;
args.onChange = (e: ChangeEvent<HTMLInputElement>) => setSelectValue(e.target.value);
Expand Down Expand Up @@ -126,11 +127,13 @@ MultipleSelect.args = {
fullWidth: true,
};

export const MultipleSelectCheckbox = MultiSelectTemplate.bind({});
export const MultipleSelectCheckbox = SelectTemplate.bind({});
MultipleSelectCheckbox.args = {
multiple: true,
placeholder: 'Select',
fullWidth: true,
SelectProps: {
multiple: true,
},
checkbox: true,
};

Expand Down