@@ -75,30 +75,35 @@ export default class AutoComplete extends Component {
75
75
76
76
render ( ) {
77
77
const {
78
- align, className, dataIndex, disabled, error, floatingLabel, items ,
79
- label, offset, readOnly, value, valueIndex,
78
+ align, className, dataIndex, disabled, error, floatingLabel,
79
+ items , label, offset, readOnly, value, valueIndex,
80
80
} = this . props
81
81
const { focused, value : stateValue } = this . state
82
82
83
- const filtered = stateValue
84
- ? items . filter ( i => i [ dataIndex ] . match ( stateValue , 'gi' ) )
85
- : items
83
+ // filter children
84
+ let filtered = items
85
+ if ( stateValue ) {
86
+ try {
87
+ const regex = new RegExp ( stateValue , 'i' )
88
+ filtered = items . filter ( i => i [ dataIndex ] . match ( regex , 'g' ) )
89
+ } catch ( e ) {
90
+ filtered = [ ]
91
+ }
92
+ }
86
93
94
+ // create options
87
95
const children = filtered . map ( item => {
88
96
const value = item [ valueIndex ]
89
97
const data = item [ dataIndex ]
90
98
return < Option key = { value } value = { value } > { data } </ Option >
91
99
} )
92
100
93
- const empty = ! children . length
94
-
101
+ // calculate input value
95
102
const item = items . find ( item => item [ valueIndex ] === value )
96
103
const data = item && item [ dataIndex ] || ''
104
+ const inputValue = typeof stateValue === 'string' ? stateValue : data
97
105
98
- const inputValue = typeof stateValue === 'string'
99
- ? stateValue
100
- : data
101
-
106
+ // calculate input props
102
107
const inputProps = {
103
108
disabled,
104
109
error,
@@ -115,15 +120,16 @@ export default class AutoComplete extends Component {
115
120
inputProps . onBlur = this . onTextfieldBlur
116
121
}
117
122
123
+ // calculate main class
118
124
const mainClass = classnames ( {
119
125
'mdl-autocomplete' : true ,
120
126
'mdl-autocomplete--disabled' : disabled ,
121
- 'mdl-autocomplete--empty' : empty ,
122
127
'mdl-autocomplete--error' : error ,
123
128
'mdl-autocomplete--focused' : focused ,
124
129
} , className )
125
130
126
- if ( disabled || readOnly || empty ) {
131
+ // render without dropdown
132
+ if ( disabled || readOnly ) {
127
133
return (
128
134
< div className = { mainClass } >
129
135
< Textfield { ...inputProps } />
@@ -132,6 +138,7 @@ export default class AutoComplete extends Component {
132
138
)
133
139
}
134
140
141
+ // calculate dropdown props
135
142
const dropdownProps = {
136
143
align,
137
144
offset,
@@ -140,6 +147,7 @@ export default class AutoComplete extends Component {
140
147
className : 'mdl-autocomplete-dropdown' ,
141
148
}
142
149
150
+ // render with dropdown
143
151
return (
144
152
< div className = { mainClass } >
145
153
< Dropdown { ...dropdownProps } >
0 commit comments