Skip to content

Commit

Permalink
Merge pull request #569 from lyytioy/next
Browse files Browse the repository at this point in the history
v2.3.1
  • Loading branch information
mikael-jarvinen authored Aug 23, 2023
2 parents 6073d8d + a4fac08 commit bd14cfc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@lyyti/design-system",
"description": "Lyyti Design System",
"homepage": "https://lyytioy.github.io/lyyti-design-system",
"version": "2.3.0",
"version": "2.3.1",
"engines": {
"node": "^18",
"npm": "^8"
Expand Down
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

0 comments on commit bd14cfc

Please sign in to comment.