@@ -34,15 +34,14 @@ export interface ISelect extends ComponentProps<any> {
34
34
const initialValue = {
35
35
isOpen : false ,
36
36
option : {
37
- title : '' ,
37
+ name : '' ,
38
38
value : '' ,
39
39
description : '' ,
40
40
} ,
41
41
} ;
42
42
43
43
type IState = {
44
44
isOpen : boolean ;
45
- lastUpdate ?: number ;
46
45
option : ISelectOption ;
47
46
} ;
48
47
@@ -65,7 +64,7 @@ export class Select extends PureComponent<ISelect, IState> {
65
64
this . contextView = useContextView ( {
66
65
shadowOutline : false ,
67
66
} ) ;
68
- this . state = this . getDefaultState ( ) ;
67
+ this . state = this . getDefaultState ( this . props ) ;
69
68
this . selectElm = React . createRef ( ) ;
70
69
this . selectInput = React . createRef ( ) ;
71
70
}
@@ -80,48 +79,50 @@ export class Select extends PureComponent<ISelect, IState> {
80
79
} ) ;
81
80
}
82
81
83
- public getDefaultState ( ) {
84
- const defaultSelectedOption : ISelectOption = { } ;
85
- const options = Children . toArray ( this . props . children ) ;
82
+ public getDefaultState ( props ) {
83
+ let defaultSelectedOption : ISelectOption = { } ;
84
+ const defaultValue = props . value || props . defaultValue ;
85
+ const options = Children . toArray ( props . children ) ;
86
86
for ( const option of options ) {
87
87
if ( isValidElement ( option ) ) {
88
88
const optionProps = option . props as ISelectOption ;
89
89
if (
90
90
optionProps . value &&
91
- optionProps . value === this . props . defaultValue
91
+ optionProps . value === defaultValue
92
92
) {
93
- defaultSelectedOption . title = optionProps . children as string ;
94
- defaultSelectedOption . value = optionProps . value ;
93
+ defaultSelectedOption = { ...optionProps , name : optionProps . name || optionProps . children as string } ;
95
94
break ;
96
95
}
97
96
}
98
97
}
99
98
return {
100
99
...initialValue ,
101
- option : defaultSelectedOption ,
100
+ option : { ... defaultSelectedOption } ,
102
101
} ;
103
102
}
104
103
105
104
public handleOnClickOption = ( e : React . MouseEvent ) => {
106
105
const option = e . target as HTMLDivElement ;
107
106
const value = getAttr ( option , 'data-value' ) ;
108
- const title = getAttr ( option , 'title ' ) ;
107
+ const name = getAttr ( option , 'data-name ' ) ;
109
108
const desc = getAttr ( option , 'data-desc' ) ;
110
- const optionItem : ISelectOption = {
111
- value : value ,
112
- title : title ,
113
- description : desc ,
114
- } ;
115
-
116
- this . setState (
117
- {
118
- option : optionItem ,
119
- } ,
120
- ( ) => {
121
- this . props . onSelect ?.( e , optionItem ) ;
122
- this . contextView . hide ( ) ;
123
- }
124
- ) ;
109
+ if ( name ) {
110
+ const optionItem : ISelectOption = {
111
+ value : value ,
112
+ name : name ,
113
+ description : desc ,
114
+ } ;
115
+
116
+ this . setState (
117
+ {
118
+ option : optionItem ,
119
+ } ,
120
+ ( ) => {
121
+ this . props . onSelect ?.( e , optionItem ) ;
122
+ this . contextView . hide ( ) ;
123
+ }
124
+ ) ;
125
+ }
125
126
} ;
126
127
127
128
public handleOnHoverOption = ( e : React . MouseEvent ) => {
@@ -173,11 +174,7 @@ export class Select extends PureComponent<ISelect, IState> {
173
174
const { option, isOpen } = this . state ;
174
175
const {
175
176
className,
176
- children,
177
- defaultValue = '' ,
178
177
placeholder,
179
- value,
180
- onSelect,
181
178
...custom
182
179
} = this . props ;
183
180
@@ -192,7 +189,7 @@ export class Select extends PureComponent<ISelect, IState> {
192
189
autoComplete = "off"
193
190
placeholder = { placeholder }
194
191
className = { inputClassName }
195
- value = { option . title }
192
+ value = { option . name }
196
193
readOnly
197
194
/>
198
195
< span className = { selectArrowClassName } >
0 commit comments