@@ -75,30 +75,35 @@ export default class AutoComplete extends Component {
7575
7676 render ( ) {
7777 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,
8080 } = this . props
8181 const { focused, value : stateValue } = this . state
8282
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+ }
8693
94+ // create options
8795 const children = filtered . map ( item => {
8896 const value = item [ valueIndex ]
8997 const data = item [ dataIndex ]
9098 return < Option key = { value } value = { value } > { data } </ Option >
9199 } )
92100
93- const empty = ! children . length
94-
101+ // calculate input value
95102 const item = items . find ( item => item [ valueIndex ] === value )
96103 const data = item && item [ dataIndex ] || ''
104+ const inputValue = typeof stateValue === 'string' ? stateValue : data
97105
98- const inputValue = typeof stateValue === 'string'
99- ? stateValue
100- : data
101-
106+ // calculate input props
102107 const inputProps = {
103108 disabled,
104109 error,
@@ -115,15 +120,16 @@ export default class AutoComplete extends Component {
115120 inputProps . onBlur = this . onTextfieldBlur
116121 }
117122
123+ // calculate main class
118124 const mainClass = classnames ( {
119125 'mdl-autocomplete' : true ,
120126 'mdl-autocomplete--disabled' : disabled ,
121- 'mdl-autocomplete--empty' : empty ,
122127 'mdl-autocomplete--error' : error ,
123128 'mdl-autocomplete--focused' : focused ,
124129 } , className )
125130
126- if ( disabled || readOnly || empty ) {
131+ // render without dropdown
132+ if ( disabled || readOnly ) {
127133 return (
128134 < div className = { mainClass } >
129135 < Textfield { ...inputProps } />
@@ -132,6 +138,7 @@ export default class AutoComplete extends Component {
132138 )
133139 }
134140
141+ // calculate dropdown props
135142 const dropdownProps = {
136143 align,
137144 offset,
@@ -140,6 +147,7 @@ export default class AutoComplete extends Component {
140147 className : 'mdl-autocomplete-dropdown' ,
141148 }
142149
150+ // render with dropdown
143151 return (
144152 < div className = { mainClass } >
145153 < Dropdown { ...dropdownProps } >
0 commit comments