Skip to content

Commit

Permalink
fix #41375
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Mar 12, 2024
1 parent d7bda2a commit f4ccdb4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 40 deletions.
50 changes: 11 additions & 39 deletions packages/mui-material/src/Select/Select.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,43 +187,6 @@ export type SelectProps<
? StandardSelectProps
: OutlinedSelectProps);

interface SelectType {
<Value, Variant extends 'filled' | 'standard'>(
props: {
/**
* The variant to use.
* @default 'outlined'
*/
variant: Variant;
} & Omit<SelectProps<Value, Variant>, 'variant'>,
): JSX.Element & {
muiName: string;
};
<Value = unknown, Variant extends 'outlined' = 'outlined'>(
props: {
/**
* The variant to use.
* @default 'outlined'
*/
variant?: Variant;
} & Omit<SelectProps<Value, Variant>, 'variant'>,
): JSX.Element & {
muiName: string;
};
}
/**
*
* Demos:
*
* - [Select](https://mui.com/material-ui/react-select/)
*
* API:
*
* - [Select API](https://mui.com/material-ui/api/select/)
* - inherits [OutlinedInput API](https://mui.com/material-ui/api/outlined-input/)
*/
declare const Select: SelectType;

/**
*
* Demos:
Expand All @@ -235,5 +198,14 @@ declare const Select: SelectType;
* - [Select API](https://mui.com/material-ui/api/select/)
* - inherits [OutlinedInput API](https://mui.com/material-ui/api/outlined-input/)
*/

export default Select;
export default function Select<Value = unknown, Variant extends SelectVariants = 'outlined'>(
props: {
/**
* The variant to use.
* @default 'outlined'
*/
variant?: Variant;
} & SelectProps<Value, Variant>,
): JSX.Element & {
muiName: string;
};
6 changes: 5 additions & 1 deletion packages/mui-material/src/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ Select.propTypes /* remove-proptypes */ = {
* The variant to use.
* @default 'outlined'
*/
variant: PropTypes.oneOf(['filled', 'outlined', 'standard']),
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOf([
'filled',
'outlined',
'standard',
]),
};

Select.muiName = 'Select';
Expand Down
54 changes: 54 additions & 0 deletions packages/mui-material/src/Select/Select.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,57 @@ function genericValueTest() {
// @ts-expect-error hiddenLabel is not present in standard variant
<Select {...standardProps} hiddenLabel />;
}

type Options<T> = { text: string; value: T } | T;

type Props<T> = (
| {
value: T;
multiple?: false;
onChange: (value: T) => void;
}
| {
value: T[];
multiple: true;
onChange: (value: T[]) => void;
}
) & {
options: Options<T>[];
};

// test for https://github.com/mui/material-ui/issues/41375
const AppSelect = <T extends string>(props: Props<T>) => {
const getOptionText = (option: Options<T>) => {
if (typeof option === 'object') {
return option.text;
}
return option;
};

const getOptionValue = (option: Options<T>) => {
if (typeof option === 'object') {
return option.value;
}
return option;
};

return (
<Select
value={props.value}
multiple={props.multiple}
onChange={(event) => {
if (props.multiple) {
props.onChange(event.target.value as T[]);
} else {
props.onChange(event.target.value as T);
}
}}
>
{props.options.map((option, index) => (
<MenuItem key={index} value={getOptionValue(option)}>
{getOptionText(option)}
</MenuItem>
))}
</Select>
);
};

0 comments on commit f4ccdb4

Please sign in to comment.