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

Commit

Permalink
Fix selected chip value being duplicated and make chip selection more…
Browse files Browse the repository at this point in the history
… robust.

#13 #14
  • Loading branch information
leMaik committed Sep 29, 2016
1 parent 05ec05d commit bb730e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/ChipInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@ class ChipInput extends React.Component {
this.uniqueId = uniqueId.replace(/[^A-Za-z0-9-]/gi, '');
}

componentDidMount() {
const handleKeyDown = this.autoComplete.handleKeyDown
this.autoComplete.handleKeyDown = (event) => {
if (event.keyCode === 13) {
this.handleAddChip(event.target.value)
this.autoComplete.setState({ searchText: '' })
this.autoComplete.forceUpdate()
} else {
handleKeyDown(event)
}
}

this.autoComplete.handleItemTouchTap = (event, child) => {
const dataSource = this.autoComplete.props.dataSource;

const index = parseInt(child.key, 10);
const chosenRequest = dataSource[index];
const searchText = this.autoComplete.chosenRequestText(chosenRequest);


this.autoComplete.setState({
searchText: '',
});
this.autoComplete.forceUpdate()
this.autoComplete.close()

setTimeout(() => this.focus(), 1)
}
}

componentWillReceiveProps (nextProps) {
if (nextProps.disabled) {
this.setState({ focusedChip: null })
Expand All @@ -148,7 +178,7 @@ class ChipInput extends React.Component {
}

focus() {
if (this.input) this.getInputNode().focus();
if (this.autoComplete) this.getInputNode().focus();
if (this.state.focusedChip) {
this.setState({ focusedChip: null })
}
Expand All @@ -163,7 +193,7 @@ class ChipInput extends React.Component {
}

getInputNode() {
return this.input;
return this.autoComplete.refs.searchTextField.getInputNode();
}

handleInputBlur = (event) => {
Expand Down Expand Up @@ -232,8 +262,6 @@ class ChipInput extends React.Component {
this.props.onChange([ ...this.state.chips, chip ])
}
}

this.setState({ inputValue: '' })
}
}

Expand Down Expand Up @@ -371,17 +399,12 @@ class ChipInput extends React.Component {
style={inputStyleMerged}
dataSource={autoCompleteData}
menuProps={{
onChange: (event, input) => {
setTimeout(() => this.focus())
setTimeout(() => {
this.setState({ inputValue: ' ' }) // fix input value not being removed when using some filters
this.handleAddChip(input)
}, (other.menuCloseDelay || 300) + 10) // menuCloseDelay + 10
}
onChange: (event, input) => this.handleAddChip(input)
}}
searchText={this.state.inputValue}
underlineShow={false}
onKeyUp={(event) => this.setState({ inputValue: event.target.value })}
ref={(ref) => this.autoComplete = ref}
/>
<TextFieldUnderline
disabled={disabled}
Expand Down
1 change: 1 addition & 0 deletions stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ storiesOf('ChipInput', module)
<ChipInput
defaultValue={['foo', 'bar']}
fullWidth
onChange={action('onChange')}
/>
))
.add('with many chips', () => themed(
Expand Down

0 comments on commit bb730e9

Please sign in to comment.