Skip to content

Commit 8230fff

Browse files
committed
fix(AutoComplete): Fix AutoComplete focus
1 parent 72edae8 commit 8230fff

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

.storybook/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { configure } from '@kadira/storybook';
22

3-
import 'normalize.css'
43
import 'react-mdl/extra/material.js'
54
import 'react-mdl/extra/material.css'
65
import './storybook.scss'

src/AutoComplete/AutoComplete.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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}>

stories/AutoComplete.story.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ storiesOf('AutoComplete', module)
3232
dataIndex={'name'}
3333
/>
3434
))
35-
.add('itemFormatter', () => (
36-
<StatefulAutoComplete
37-
label={'I will complete you'}
38-
items={items2}
39-
valueIndex={'id'}
40-
itemFormatter={(item, props) => `${item.firstName} ${item.lastName}`}
41-
/>
42-
))
4335
.add('change handler', () => (
4436
<StatefulAutoComplete
4537
label={'I will complete you'}

stories/SelectField.story.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ storiesOf('SelectField', module)
3434
<Option value={5}>Five</Option>
3535
</StatefulSelectField>
3636
))
37-
.add('empty', () => (
37+
.add('empty option list', () => (
3838
<StatefulSelectField label={'Select me'}>
3939
</StatefulSelectField>
4040
))

0 commit comments

Comments
 (0)