|
1 | 1 | import * as React from 'react'; |
2 | | -import Select, { SelectChangeEvent } from '@mui/material/Select'; |
| 2 | +import Select, { SelectChangeEvent, SelectProps } from '@mui/material/Select'; |
3 | 3 | import MenuItem from '@mui/material/MenuItem'; |
4 | 4 | import { createTheme } from '@mui/material/styles'; |
5 | 5 |
|
@@ -75,4 +75,124 @@ function genericValueTest() { |
75 | 75 | }, |
76 | 76 | }} |
77 | 77 | />; |
| 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 />; |
78 | 144 | } |
| 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