Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 275e4c6

Browse files
committed
Use AutoComplete for the text field.
Resolves #1.
1 parent 923b7b7 commit 275e4c6

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

src/ChipInput.js

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* Copyright (c) 2014 Call-Em-All (https://github.com/callemall/material-ui)
44
*/
55
import React from 'react'
6-
import ReactDOM from 'react-dom'
76
import TextFieldUnderline from 'material-ui/TextField/TextFieldUnderline'
87
import TextFieldHint from 'material-ui/TextField/TextFieldHint'
8+
import AutoComplete from 'material-ui/AutoComplete/AutoComplete'
99
import transitions from 'material-ui/styles/transitions'
1010
import Chip from 'material-ui/Chip'
11-
import {blue300, indigo900} from 'material-ui/styles/colors'
11+
import {blue300} from 'material-ui/styles/colors'
1212

1313
const getStyles = (props, context, state) => {
1414
const {
@@ -39,8 +39,8 @@ const getStyles = (props, context, state) => {
3939
},
4040
input: {
4141
padding: 0,
42-
marginTop: 8,
43-
marginBottom: 16,
42+
marginTop: 0,
43+
marginBottom: 24,
4444
lineHeight: '32px',
4545
height: 32,
4646
position: 'relative',
@@ -89,7 +89,7 @@ class ChipInput extends React.Component {
8989
isClean: true,
9090
chips: [],
9191
focusedChip: null,
92-
hasValue: false
92+
inputValue: ''
9393
}
9494

9595
constructor (props) {
@@ -119,19 +119,14 @@ class ChipInput extends React.Component {
119119
}
120120

121121
getInputNode() {
122-
return (this.props.children || this.props.multiLine) ?
123-
this.input.getInputNode() : ReactDOM.findDOMNode(this.input);
122+
return this.input;
124123
}
125124

126125
handleInputBlur = (event) => {
127126
this.setState({isFocused: false});
128127
if (this.props.onBlur) this.props.onBlur(event);
129128
}
130129

131-
handleInputChange = (event) => {
132-
this.setState({ hasValue: event.target.value !== '' });
133-
}
134-
135130
handleInputFocus = (event) => {
136131
if (this.props.disabled) {
137132
return;
@@ -144,24 +139,7 @@ class ChipInput extends React.Component {
144139

145140
handleKeyDown = (event) => {
146141
if (event.keyCode === 13) { // enter
147-
const input = event.target.value
148-
const chips = this.props.value || this.state.chips
149-
if (input.trim().length > 0) {
150-
if (chips.indexOf(input) === -1) {
151-
if (this.props.value) {
152-
if (this.props.onRequestAdd) {
153-
this.props.onRequestAdd(input)
154-
}
155-
} else {
156-
this.setState({ chips: [ ...chips, input ] })
157-
if (this.props.onChange) {
158-
this.props.onChange([ ...chips, input ])
159-
}
160-
}
161-
}
162-
this.setState({ hasValue: false })
163-
event.target.value = ''
164-
}
142+
this.handleAddChip(event.target.value)
165143
} else if (event.keyCode === 8 || event.keyCode === 46) {
166144
if (event.target.value === '') {
167145
const chips = this.props.value || this.state.chips
@@ -196,6 +174,25 @@ class ChipInput extends React.Component {
196174
}
197175
}
198176

177+
handleAddChip (chip) {
178+
const chips = this.props.value || this.state.chips
179+
180+
if (chip.trim().length > 0 && chips.indexOf(chip) === -1) {
181+
if (this.props.value) {
182+
if (this.props.onRequestAdd) {
183+
this.props.onRequestAdd(chip)
184+
}
185+
} else {
186+
this.setState({ chips: [ ...this.state.chips, chip ] })
187+
if (this.props.onChange) {
188+
this.props.onChange([ ...this.state.chips, chip ])
189+
}
190+
}
191+
192+
this.setState({ inputValue: '' })
193+
}
194+
}
195+
199196
handleDeleteChip (chip) {
200197
if (this.props.value) {
201198
if (this.props.onRequestDelete) {
@@ -208,7 +205,9 @@ class ChipInput extends React.Component {
208205
chips,
209206
focusedChip: this.state.focusedChip === chip ? null : this.state.focusedChip
210207
})
211-
this.props.onChange(chips)
208+
if (this.props.onChange) {
209+
this.props.onChange(chips)
210+
}
212211
}
213212
}
214213
}
@@ -234,14 +233,14 @@ class ChipInput extends React.Component {
234233
underlineStyle,
235234
defaultValue = [],
236235
value,
236+
dataSource,
237237
...other,
238238
} = this.props;
239239

240240
const inputProps = {
241241
ref: (elem) => this.input = elem,
242242
disabled: this.props.disabled,
243243
onBlur: this.handleInputBlur,
244-
onChange: this.handleInputChange,
245244
onFocus: this.handleInputFocus,
246245
onKeyDown: this.handleKeyDown
247246
}
@@ -270,17 +269,29 @@ class ChipInput extends React.Component {
270269
{hintText ?
271270
<TextFieldHint
272271
muiTheme={this.context.muiTheme}
273-
show={(this.state.chips || this.props.chips).length === 0 && !this.state.hasValue}
272+
show={(this.props.value || this.state.chips).length === 0 && this.state.inputValue.length === 0}
274273
style={Object.assign({ bottom: 20, pointerEvents: 'none' }, hintStyle)}
275274
text={hintText}
276275
/> :
277276
null
278277
}
279-
<input
278+
<AutoComplete
280279
{...other}
281280
{...inputProps}
282-
style={prepareStyles(inputStyleMerged)}
283-
type="text"
281+
style={inputStyleMerged}
282+
dataSource={dataSource || []}
283+
menuProps={{
284+
onChange: (event, input) => {
285+
setTimeout(() => this.focus())
286+
setTimeout(() => {
287+
this.handleAddChip(input)
288+
this.setState({ inputValue: '' })
289+
}, (other.menuCloseDelay || 300) + 10) // menuCloseDelay + 10
290+
}
291+
}}
292+
searchText={this.state.inputValue}
293+
underlineShow={false}
294+
onKeyUp={(event) => this.setState({ inputValue: event.target.value })}
284295
/>
285296
<TextFieldUnderline
286297
disabled={disabled}

stories/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ storiesOf('ChipInput', module)
3232
/>
3333
</MuiThemeProvider>
3434
))
35+
.add('with auto complete', () => (
36+
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
37+
<ChipInput
38+
defaultValue={['foo', 'bar']}
39+
style={{ width: '100%' }}
40+
dataSource={['alpha', 'beta']}
41+
hintText="Try typing a..."
42+
/>
43+
</MuiThemeProvider>
44+
))
3545
.add('controlled input', () => (
3646
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
3747
<ControlledChipInput />

0 commit comments

Comments
 (0)