@@ -8,81 +8,80 @@ 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,
35- onChange,
25+ type,
26+ id,
27+ name,
28+ value,
29+ options,
30+ disabled,
31+ style,
32+ tooltip,
33+ label,
34+ multiple,
35+ onChange,
3636} : SelectProps ) {
37- const handleChange = ( event : SelectChangeEvent ) => {
38- if ( id ) {
39- let newValue : string | number = event . target . value ;
40- if ( typeof value == "number" ) {
41- newValue = Number . parseInt ( newValue ) ;
42- }
43- onChange ( {
44- componentType : type ,
45- id : id ,
46- property : "value" ,
47- value : newValue ,
48- } ) ;
49- }
50- } ;
51- return (
52- < Tooltip title = { tooltip } >
53- < MuiFormControl variant = "filled" size = "small" style = { style } >
54- { label && < MuiInputLabel id = { `${ id } -label` } > { label } </ MuiInputLabel > }
55- < MuiSelect
56- labelId = { `${ id } -label` }
57- id = { id }
58- name = { name }
59- value = { `${ value } ` }
60- disabled = { disabled }
61- multiple = { multiple }
62- onChange = { handleChange }
63- >
64- { Array . isArray ( options ) &&
65- options . map ( normalizeSelectOption ) . map ( ( [ value , text ] , index ) => (
66- < MuiMenuItem key = { index } value = { value } >
67- { text }
68- </ MuiMenuItem >
69- ) ) }
70- </ MuiSelect >
71- </ MuiFormControl >
72- </ Tooltip >
73- ) ;
37+ const handleChange = ( event : SelectChangeEvent ) => {
38+ if ( id ) {
39+ let newValue : string | number = event . target . value ;
40+ if ( typeof value == "number" ) {
41+ newValue = Number . parseInt ( newValue ) ;
42+ }
43+ onChange ( {
44+ componentType : type ,
45+ id : id ,
46+ property : "value" ,
47+ value : newValue ,
48+ } ) ;
49+ }
50+ } ;
51+ return (
52+ < Tooltip title = { tooltip } >
53+ < MuiFormControl variant = "filled" size = "small" style = { style } >
54+ { label && < MuiInputLabel id = { `${ id } -label` } > { label } </ MuiInputLabel > }
55+ < MuiSelect
56+ labelId = { `${ id } -label` }
57+ id = { id }
58+ name = { name }
59+ value = { `${ value } ` }
60+ disabled = { disabled }
61+ multiple = { multiple }
62+ onChange = { handleChange } >
63+ { Array . isArray ( options ) &&
64+ options . map ( normalizeSelectOption ) . map ( ( [ value , text ] , index ) => (
65+ < MuiMenuItem key = { index } value = { value } >
66+ { text }
67+ </ MuiMenuItem >
68+ ) ) }
69+ </ MuiSelect >
70+ </ MuiFormControl >
71+ </ Tooltip >
72+ ) ;
7473}
7574
7675function normalizeSelectOption (
77- option : SelectOption ,
76+ option : SelectOption
7877) : [ string | number , string ] {
79- if ( isString ( option ) ) {
80- return [ option , option ] ;
81- } else if ( typeof option === "number" ) {
82- return [ option , option . toString ( ) ] ;
83- } else if ( Array . isArray ( option ) ) {
84- return option ;
85- } else {
86- return [ option . value , option . label || `${ option . value } ` ] ;
87- }
78+ if ( isString ( option ) ) {
79+ return [ option , option ] ;
80+ } else if ( typeof option === "number" ) {
81+ return [ option , option . toString ( ) ] ;
82+ } else if ( Array . isArray ( option ) ) {
83+ return option ;
84+ } else {
85+ return [ option . value , option . label || `${ option . value } ` ] ;
86+ }
8887}
0 commit comments