Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Sep 10, 2018
1 parent 49cad58 commit 0162142
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
13 changes: 9 additions & 4 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export default class Autosuggest extends Component {

onSuggestionMouseEnter = (event, { sectionIndex, itemIndex }) => {
this.updateHighlightedSuggestion(sectionIndex, itemIndex);

if (event.target === this.pressedSuggestion) {
this.justSelectedSuggestion = true;
}
Expand All @@ -358,12 +359,12 @@ export default class Autosuggest extends Component {
}
};

onSuggestionMouseDown = e => {
onSuggestionMouseDown = event => {
// Checking if this.justSelectedSuggestion is already true to not duplicate touch events in chrome
// See: https://github.com/facebook/react/issues/9809#issuecomment-413978405
if (!this.justSelectedSuggestion) {
this.justSelectedSuggestion = true;
this.pressedSuggestion = e.target;
this.pressedSuggestion = event.target;
}
};

Expand Down Expand Up @@ -446,9 +447,13 @@ export default class Autosuggest extends Component {
onBlur && onBlur(this.blurEvent, { highlightedSuggestion });
};

onSuggestionMouseLeave = e => {
onSuggestionMouseLeave = event => {
this.resetHighlightedSuggestion(false); // shouldResetValueBeforeUpDown
if (this.justSelectedSuggestion && e.target === this.pressedSuggestion) {

if (
this.justSelectedSuggestion &&
event.target === this.pressedSuggestion
) {
this.justSelectedSuggestion = false;
}
};
Expand Down
19 changes: 10 additions & 9 deletions test/focus-first-suggestion/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ describe('Autosuggest with highlightFirstSuggestion={true}', () => {
onSuggestionSelected.reset();
clickEnter();
expect(onSuggestionSelected).to.have.been.calledOnce;
expect(
onSuggestionSelected
).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'Perl', year: 1987 },
suggestionValue: 'Perl',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
});
expect(onSuggestionSelected).to.have.been.calledWithExactly(
syntheticEventMatcher,
{
suggestion: { name: 'Perl', year: 1987 },
suggestionValue: 'Perl',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
}
);
});
});

Expand Down
12 changes: 7 additions & 5 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ export const getSuggestion = suggestionIndex => {
throw Error(
`
Cannot find suggestion #${suggestionIndex}.
${suggestions.length === 0
? 'No suggestions found.'
: `Only ${suggestions.length} suggestion${suggestions.length === 1
? ''
: 's'} found.`}
${
suggestions.length === 0
? 'No suggestions found.'
: `Only ${suggestions.length} suggestion${
suggestions.length === 1 ? '' : 's'
} found.`
}
`
);
}
Expand Down
38 changes: 20 additions & 18 deletions test/plain-list/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,30 +602,32 @@ describe('Default Autosuggest', () => {
it('should be called once with the right parameters when suggestion is clicked', () => {
clickSuggestion(1);
expect(onSuggestionSelected).to.have.been.calledOnce;
expect(
onSuggestionSelected
).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'JavaScript', year: 1995 },
suggestionValue: 'JavaScript',
suggestionIndex: 1,
sectionIndex: null,
method: 'click'
});
expect(onSuggestionSelected).to.have.been.calledWithExactly(
syntheticEventMatcher,
{
suggestion: { name: 'JavaScript', year: 1995 },
suggestionValue: 'JavaScript',
suggestionIndex: 1,
sectionIndex: null,
method: 'click'
}
);
});

it('should be called once with the right parameters when Enter is pressed and suggestion is highlighted', () => {
clickDown();
clickEnter();
expect(onSuggestionSelected).to.have.been.calledOnce;
expect(
onSuggestionSelected
).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'Java', year: 1995 },
suggestionValue: 'Java',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
});
expect(onSuggestionSelected).to.have.been.calledWithExactly(
syntheticEventMatcher,
{
suggestion: { name: 'Java', year: 1995 },
suggestionValue: 'Java',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
}
);
});

it('should not be called when Enter is pressed and there is no highlighted suggestion', () => {
Expand Down

0 comments on commit 0162142

Please sign in to comment.