@@ -8,86 +8,86 @@ import { isString } from "@/utils/isString";
88import { Tooltip } from "./Tooltip" ;
99
1010export type SelectOption =
11- | string
12- | number
13- | [ string , string ]
14- | [ number , string ]
15- | { value : string | number ; label ?: string } ;
11+ | string
12+ | number
13+ | [ string , string ]
14+ | [ number , string ]
15+ | { value : string | number ; label ?: string } ;
1616
1717interface SelectState extends ComponentState {
18- options ?: SelectOption [ ] ;
19- multiple ?: boolean ;
18+ options ?: SelectOption [ ] ;
19+ multiple ?: boolean ;
2020}
2121
2222interface SelectProps extends ComponentProps , SelectState { }
2323
2424export function Select ( {
25- type,
26- id,
27- name,
28- value,
29- options,
30- disabled,
31- style,
32- tooltip,
33- label,
34- multiple = false ,
35- onChange,
25+ type,
26+ id,
27+ name,
28+ value,
29+ options,
30+ disabled,
31+ style,
32+ tooltip,
33+ label,
34+ multiple = false ,
35+ onChange,
3636} : SelectProps ) {
37- const handleChange = ( event : SelectChangeEvent < unknown > ) => {
38- if ( id ) {
39- let newValue : string | number | ( string | number ) [ ] = multiple
40- ? ( event . target . value as ( string | number ) [ ] )
41- : ( event . target . value as string | number ) ;
37+ const handleChange = ( event : SelectChangeEvent < unknown > ) => {
38+ if ( id ) {
39+ let newValue : string | number | ( string | number ) [ ] = multiple
40+ ? ( event . target . value as ( string | number ) [ ] )
41+ : ( event . target . value as string | number ) ;
4242
43- if ( ! multiple && typeof value === "number" ) {
44- newValue = Number . parseInt ( newValue as string ) ;
45- }
43+ if ( ! multiple && typeof value === "number" ) {
44+ newValue = Number . parseInt ( newValue as string ) ;
45+ }
4646
47- onChange ( {
48- componentType : type ,
49- id : id ,
50- property : "value" ,
51- value : newValue ,
52- } ) ;
53- }
54- } ;
55- return (
56- < Tooltip title = { tooltip } >
57- < MuiFormControl variant = "filled" size = "small" style = { style } >
58- { label && < MuiInputLabel id = { `${ id } -label` } > { label } </ MuiInputLabel > }
59- < MuiSelect
60- labelId = { `${ id } -label` }
61- id = { id }
62- name = { name }
63- value = { value }
64- disabled = { disabled }
65- multiple = { multiple }
66- onChange = { handleChange } >
67- { Array . isArray ( options ) &&
68- options
69- . map ( normalizeSelectOption )
70- . map ( ( [ optionValue , optionLabel ] , index ) => (
71- < MuiMenuItem key = { index } value = { optionValue } >
72- { optionLabel }
73- </ MuiMenuItem >
74- ) ) }
75- </ MuiSelect >
76- </ MuiFormControl >
77- </ Tooltip >
78- ) ;
47+ onChange ( {
48+ componentType : type ,
49+ id : id ,
50+ property : "value" ,
51+ value : newValue ,
52+ } ) ;
53+ }
54+ } ;
55+ return (
56+ < Tooltip title = { tooltip } >
57+ < MuiFormControl variant = "filled" size = "small" style = { style } >
58+ { label && < MuiInputLabel id = { `${ id } -label` } > { label } </ MuiInputLabel > }
59+ < MuiSelect
60+ labelId = { `${ id } -label` }
61+ id = { id }
62+ name = { name }
63+ value = { value }
64+ disabled = { disabled }
65+ multiple = { multiple }
66+ onChange = { handleChange } >
67+ { Array . isArray ( options ) &&
68+ options
69+ . map ( normalizeSelectOption )
70+ . map ( ( [ optionValue , optionLabel ] , index ) => (
71+ < MuiMenuItem key = { index } value = { optionValue } >
72+ { optionLabel }
73+ </ MuiMenuItem >
74+ ) ) }
75+ </ MuiSelect >
76+ </ MuiFormControl >
77+ </ Tooltip >
78+ ) ;
7979}
8080
8181function normalizeSelectOption (
82- option : SelectOption
82+ option : SelectOption
8383) : [ string | number , string ] {
84- if ( isString ( option ) ) {
85- return [ option , option ] ;
86- } else if ( typeof option === "number" ) {
87- return [ option , option . toString ( ) ] ;
88- } else if ( Array . isArray ( option ) ) {
89- return option ;
90- } else {
91- return [ option . value , option . label || `${ option . value } ` ] ;
92- }
84+ if ( isString ( option ) ) {
85+ return [ option , option ] ;
86+ } else if ( typeof option === "number" ) {
87+ return [ option , option . toString ( ) ] ;
88+ } else if ( Array . isArray ( option ) ) {
89+ return option ;
90+ } else {
91+ return [ option . value , option . label || `${ option . value } ` ] ;
92+ }
9393}
0 commit comments