Skip to content

Commit

Permalink
resolve #6618
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Maltsev authored and Nick Maltsev committed Apr 18, 2017
1 parent 7b656dd commit 3dad6d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
35 changes: 20 additions & 15 deletions src/AutoComplete/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ class AutoComplete extends Component {
openOnFocus: false,
onUpdateInput: () => {},
onNewRequest: () => {},
searchText: '',
menuCloseDelay: 300,
targetOrigin: {
vertical: 'top',
Expand All @@ -218,7 +217,7 @@ class AutoComplete extends Component {
this.requestsList = [];
this.setState({
open: this.props.open,
searchText: this.props.searchText,
searchText: this.props.searchText || '',
});
this.timerTouchTapCloseId = null;
}
Expand Down Expand Up @@ -262,24 +261,30 @@ class AutoComplete extends Component {

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

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

this.setState({
searchText: searchText,
}, () => {
this.props.onUpdateInput(searchText, this.props.dataSource, {
source: 'touchTap',
});

this.timerTouchTapCloseId = setTimeout(() => {
this.timerTouchTapCloseId = null;
this.close();
this.props.onNewRequest(chosenRequest, index);
}, this.props.menuCloseDelay);
const updateInput = () => this.props.onUpdateInput(searchText, this.props.dataSource, {
source: 'touchTap',
});
this.timerTouchTapCloseId = () => setTimeout(() => {
this.timerTouchTapCloseId = null;
this.close();
this.props.onNewRequest(chosenRequest, index);
}, this.props.menuCloseDelay);

if (typeof this.props.searchText !== 'undefined') {
updateInput();
this.timerTouchTapCloseId();
} else {
this.setState({
searchText: searchText,
}, () => {
updateInput();
this.timerTouchTapCloseId();
});
}
};

chosenRequestText = (chosenRequest) => {
Expand Down
25 changes: 19 additions & 6 deletions src/AutoComplete/AutoComplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,16 @@ describe('<AutoComplete />', () => {
const wrapper = shallowWithContext(
<AutoComplete
dataSource={['foo', 'bar']}
searchText="f"
onNewRequest={handleNewRequest}
menuCloseDelay={10}
/>
);

wrapper.setState({open: true});
wrapper.setState({open: true, searchText: 'f'});
wrapper.find(Menu).props().onItemTouchTap({}, {
key: 0,
});
assert.strictEqual(handleNewRequest.callCount, 0);
assert.strictEqual(wrapper.state().searchText, 'foo');

setTimeout(() => {
assert.strictEqual(handleNewRequest.callCount, 1);
Expand All @@ -97,16 +95,14 @@ describe('<AutoComplete />', () => {
const wrapper = shallowWithContext(
<AutoComplete
dataSource={['foo', 'bar']}
searchText="f"
onUpdateInput={handleUpdateInput}
/>
);

wrapper.setState({open: true});
wrapper.setState({open: true, searchText: 'f'});
wrapper.find(Menu).props().onItemTouchTap({}, {
key: 0,
});
assert.strictEqual(wrapper.state().searchText, 'foo');

setTimeout(() => {
assert.strictEqual(handleUpdateInput.callCount, 1);
Expand Down Expand Up @@ -154,4 +150,21 @@ describe('<AutoComplete />', () => {
assert.strictEqual(handleClose.callCount, 1);
});
});

describe('prop: searchText', () => {
it('onItemTouchTap should not call setState:searchText when searchText is controlled', () => {
const wrapper = shallowWithContext(
<AutoComplete
dataSource={['foo', 'bar']}
searchText="f"
/>
);

wrapper.setState({open: true});
wrapper.find(Menu).props().onItemTouchTap({}, {
key: 0,
});
assert.strictEqual(wrapper.state().searchText, 'f');
});
});
});

0 comments on commit 3dad6d5

Please sign in to comment.