Skip to content

Commit c25839e

Browse files
authored
[material-ui][Select] Fix variant type (#41405)
1 parent 65408b1 commit c25839e

File tree

2 files changed

+129
-21
lines changed

2 files changed

+129
-21
lines changed

packages/mui-material/src/Select/Select.d.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export interface BaseSelectProps<Value = unknown>
148148
* The variant to use.
149149
* @default 'outlined'
150150
*/
151-
variant?: 'filled' | 'standard' | 'outlined';
151+
variant?: SelectVariants;
152152
}
153153

154154
export interface FilledSelectProps extends Omit<FilledInputProps, 'value' | 'onChange'> {
@@ -172,20 +172,15 @@ export interface OutlinedSelectProps extends Omit<OutlinedInputProps, 'value' |
172172
* The variant to use.
173173
* @default 'outlined'
174174
*/
175-
variant: 'outlined';
175+
variant?: 'outlined';
176176
}
177177

178178
export type SelectVariants = 'outlined' | 'standard' | 'filled';
179179

180-
export type SelectProps<
181-
Value = unknown,
182-
Variant extends SelectVariants = SelectVariants,
183-
> = BaseSelectProps<Value> &
184-
(Variant extends 'filled'
185-
? FilledSelectProps
186-
: Variant extends 'standard'
187-
? StandardSelectProps
188-
: OutlinedSelectProps);
180+
export type SelectProps<Value = unknown> =
181+
| (FilledSelectProps & BaseSelectProps<Value>)
182+
| (StandardSelectProps & BaseSelectProps<Value>)
183+
| (OutlinedSelectProps & BaseSelectProps<Value>);
189184

190185
/**
191186
*
@@ -198,15 +193,8 @@ export type SelectProps<
198193
* - [Select API](https://mui.com/material-ui/api/select/)
199194
* - inherits [OutlinedInput API](https://mui.com/material-ui/api/outlined-input/)
200195
*/
201-
202-
export default function Select<Value = unknown, Variant extends SelectVariants = 'outlined'>(
203-
props: {
204-
/**
205-
* The variant to use.
206-
* @default 'outlined'
207-
*/
208-
variant?: Variant;
209-
} & Omit<SelectProps<Value, Variant>, 'variant'>,
196+
export default function Select<Value = unknown>(
197+
props: SelectProps<Value>,
210198
): JSX.Element & {
211199
muiName: string;
212200
};

packages/mui-material/src/Select/Select.spec.tsx

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import Select, { SelectChangeEvent } from '@mui/material/Select';
2+
import Select, { SelectChangeEvent, SelectProps } from '@mui/material/Select';
33
import MenuItem from '@mui/material/MenuItem';
44
import { createTheme } from '@mui/material/styles';
55

@@ -75,4 +75,124 @@ function genericValueTest() {
7575
},
7676
}}
7777
/>;
78+
79+
<Select variant="filled" />;
80+
<Select variant="standard" />;
81+
<Select variant="outlined" />;
82+
<Select />;
83+
84+
<Select variant="filled" hiddenLabel />;
85+
// @ts-expect-error hiddenLabel is not present in standard variant
86+
<Select variant="standard" hiddenLabel />;
87+
// @ts-expect-error hiddenLabel is not present in outlined variant
88+
<Select variant="outlined" hiddenLabel />;
89+
// @ts-expect-error hiddenLabel is not present in outlined variant
90+
<Select hiddenLabel />;
91+
92+
const defaultProps: SelectProps<number> = {};
93+
const outlinedProps: SelectProps<number> = {
94+
variant: 'outlined',
95+
};
96+
const filledProps: SelectProps<number> = {
97+
variant: 'filled',
98+
};
99+
const standardProps: SelectProps<number> = {
100+
variant: 'standard',
101+
};
102+
103+
<Select {...defaultProps} />;
104+
<Select {...outlinedProps} />;
105+
<Select {...filledProps} />;
106+
<Select {...standardProps} />;
107+
<Select<number> {...outlinedProps} />;
108+
<Select<number> {...defaultProps} />;
109+
<Select<number> {...filledProps} />;
110+
111+
const rawDefaultProps: SelectProps = {};
112+
const rawOutlinedProps: SelectProps = {
113+
variant: 'outlined',
114+
};
115+
const rawFilledProps: SelectProps = {
116+
variant: 'filled',
117+
};
118+
119+
<Select {...rawDefaultProps} />;
120+
<Select {...rawOutlinedProps} />;
121+
<Select {...rawFilledProps} />;
122+
123+
// @ts-expect-error hiddenLabel is not present in outlined variant
124+
<Select {...defaultProps} hiddenLabel />;
125+
// @ts-expect-error hiddenLabel is not present in outlined variant
126+
<Select {...outlinedProps} hiddenLabel />;
127+
<Select {...filledProps} hiddenLabel />;
128+
// @ts-expect-error hiddenLabel is not present in standard variant
129+
<Select {...standardProps} hiddenLabel />;
130+
131+
interface OtherProps {
132+
otherProp?: number;
133+
}
134+
135+
const SelectWrapper1 = <Value,>(props: SelectProps<Value> & OtherProps) => {
136+
const { otherProp, ...materialSelectProps } = props;
137+
138+
return <Select<Value> {...materialSelectProps} />;
139+
};
140+
141+
<SelectWrapper1 />;
142+
<SelectWrapper1<number> variant="filled" hiddenLabel />;
143+
<SelectWrapper1 variant="filled" hiddenLabel />;
78144
}
145+
146+
type Options<T> = { text: string; value: T } | T;
147+
148+
type Props<T> = (
149+
| {
150+
value: T;
151+
multiple?: false;
152+
onChange: (value: T) => void;
153+
}
154+
| {
155+
value: T[];
156+
multiple: true;
157+
onChange: (value: T[]) => void;
158+
}
159+
) & {
160+
options: Options<T>[];
161+
};
162+
163+
// test for https://github.com/mui/material-ui/issues/41375
164+
const AppSelect = <T extends string>(props: Props<T>) => {
165+
const getOptionText = (option: Options<T>) => {
166+
if (typeof option === 'object') {
167+
return option.text;
168+
}
169+
return option;
170+
};
171+
172+
const getOptionValue = (option: Options<T>) => {
173+
if (typeof option === 'object') {
174+
return option.value;
175+
}
176+
return option;
177+
};
178+
179+
return (
180+
<Select
181+
value={props.value}
182+
multiple={props.multiple}
183+
onChange={(event) => {
184+
if (props.multiple) {
185+
props.onChange(event.target.value as T[]);
186+
} else {
187+
props.onChange(event.target.value as T);
188+
}
189+
}}
190+
>
191+
{props.options.map((option, index) => (
192+
<MenuItem key={index} value={getOptionValue(option)}>
193+
{getOptionText(option)}
194+
</MenuItem>
195+
))}
196+
</Select>
197+
);
198+
};

0 commit comments

Comments
 (0)