1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
- import SelectItem from './SelectItem' ;
5
- import SelectItemInternal from './internal/SelectItemInternal' ;
6
-
7
4
const ANIMATION_TIMEOUT = 500 ;
8
5
9
6
export default class SelectList extends React . Component {
@@ -19,6 +16,10 @@ export default class SelectList extends React.Component {
19
16
PropTypes . string ,
20
17
PropTypes . number
21
18
] ) ,
19
+ children : PropTypes . oneOfType ( [
20
+ PropTypes . node ,
21
+ PropTypes . arrayOf ( PropTypes . node )
22
+ ] ) ,
22
23
selectFirst : PropTypes . bool , // eslint-disable-line react/no-unused-prop-types
23
24
className : PropTypes . string ,
24
25
} ;
@@ -29,14 +30,14 @@ export default class SelectList extends React.Component {
29
30
value : null ,
30
31
onChange : null ,
31
32
selectFirst : null ,
33
+ children : null ,
32
34
} ;
33
35
34
36
constructor ( props ) {
35
37
super ( props ) ;
36
38
37
39
this . state = {
38
40
selectedId : props . defaultValue || 0 ,
39
- children : [ ]
40
41
} ;
41
42
42
43
if ( props . defaultValue && props . onChange ) {
@@ -48,7 +49,10 @@ export default class SelectList extends React.Component {
48
49
this . selectListId = `cc_selectlist__${ SelectList . maxId } ` ;
49
50
SelectList . maxId += 1 ;
50
51
51
- this . _cleanChildren ( this . props ) ;
52
+ const { children, selectFirst } = this . props ;
53
+ if ( selectFirst ) {
54
+ this . calculateFirst ( children ) ;
55
+ }
52
56
}
53
57
54
58
componentWillReceiveProps ( nextProps ) {
@@ -59,8 +63,6 @@ export default class SelectList extends React.Component {
59
63
selectedId : nextProps . value ,
60
64
} ) ;
61
65
}
62
-
63
- this . _cleanChildren ( nextProps ) ;
64
66
}
65
67
66
68
_changeActiveItem = ( id , value ) => {
@@ -91,83 +93,47 @@ export default class SelectList extends React.Component {
91
93
} ) ;
92
94
} ;
93
95
94
- _cleanChildren ( props ) {
95
- const { selectedId } = this . state ;
96
- const children = [ ] ;
97
-
98
- if ( window . chayns . utils . isArray ( props . children ) ) {
99
- props . children . map ( ( child ) => {
100
- if ( child && child . type && child . type . componentName === SelectItem . componentName ) {
101
- if ( child . props
102
- && ( child . props . id || child . props . id === 0 )
103
- && child . props . name ) {
104
- children . push ( child ) ;
105
- }
106
- }
107
- } ) ;
108
- }
109
-
110
- if ( selectedId === 0 && props . selectFirst && children . length > 0 ) {
111
- this . _selectFirstItem ( children ) ;
96
+ calculateFirst ( children ) {
97
+ if ( ! children ) {
98
+ return ;
112
99
}
113
100
114
- this . setState ( {
115
- children
116
- } ) ;
117
- }
101
+ let firstItemId = 0 ;
118
102
119
- _selectFirstItem ( children ) {
120
- for ( let i = 0 , z = children . length ; i < z ; i += 1 ) {
121
- const { props } = children [ i ] ;
122
-
123
- if ( ! props . disabled ) {
124
- this . _changeActiveItem ( props . id , props . value ) ;
125
- return ;
103
+ for ( let i = 0 , z = children . length ; i < z ; i += 1 ) {
104
+ const child = children [ i ] ;
105
+ if ( React . isValidElement ( child ) ) {
106
+ if ( child && child . props && child . props . id && ! child . props . disabled ) {
107
+ firstItemId = child . props . id ;
108
+ break ;
109
+ }
126
110
}
127
111
}
128
- }
129
-
130
- _renderChildren ( children ) {
131
- if ( children . length === 1 ) return children ;
132
- const { selectedId } = this . state ;
133
-
134
- return children . map ( ( child ) => {
135
- const {
136
- id,
137
- disabled,
138
- className,
139
- name,
140
- value,
141
- } = child . props ;
142
112
143
- return (
144
- < SelectItemInternal
145
- id = { id }
146
- selectListId = { this . selectListId }
147
- onChange = { this . _changeActiveItem }
148
- checked = { id === selectedId }
149
- disabled = { disabled }
150
- key = { id }
151
- name = { name }
152
- className = { className }
153
- value = { value }
154
- >
155
-
156
- { child }
157
- </ SelectItemInternal >
158
- ) ;
113
+ this . setState ( {
114
+ selectedId : firstItemId ,
159
115
} ) ;
160
116
}
161
117
162
-
163
118
render ( ) {
164
- const { className } = this . props ;
165
- const { children } = this . state ;
119
+ const { className, children } = this . props ;
120
+ const { selectedId } = this . state ;
121
+
166
122
167
123
if ( children . length > 0 ) {
168
124
return (
169
125
< div className = { className } >
170
- { this . _renderChildren ( children ) }
126
+ { React . Children . map ( children , ( child ) => {
127
+ if ( ! React . isValidElement ( child ) ) {
128
+ return null ;
129
+ }
130
+
131
+ return React . cloneElement ( child , {
132
+ changeListItem : this . _changeActiveItem ,
133
+ selectListId : this . selectListId ,
134
+ selectListSelectedId : selectedId ,
135
+ } ) ;
136
+ } ) }
171
137
</ div >
172
138
) ;
173
139
}
0 commit comments