Skip to content

Commit dc5ae6f

Browse files
committed
Trigger onSuggestionHighlighted when the autohighlighted suggestion changes
1 parent 5ae2cf5 commit dc5ae6f

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/Autosuggest.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default class Autosuggest extends Component {
103103
isCollapsed: !alwaysRenderSuggestions,
104104
highlightedSectionIndex: null,
105105
highlightedSuggestionIndex: null,
106+
highlightedSuggestion: null,
106107
valueBeforeUpDown: null
107108
};
108109

@@ -129,10 +130,6 @@ export default class Autosuggest extends Component {
129130
}
130131
} else {
131132
if (this.willRenderSuggestions(nextProps)) {
132-
if (nextProps.highlightFirstSuggestion) {
133-
this.highlightFirstSuggestion();
134-
}
135-
136133
if (this.state.isCollapsed && !this.justSelectedSuggestion) {
137134
this.revealSuggestions();
138135
}
@@ -143,21 +140,30 @@ export default class Autosuggest extends Component {
143140
}
144141

145142
componentDidUpdate(prevProps, prevState) {
146-
const { onSuggestionHighlighted } = this.props;
143+
const {
144+
suggestions,
145+
onSuggestionHighlighted,
146+
highlightFirstSuggestion
147+
} = this.props;
147148

148-
if (!onSuggestionHighlighted) {
149+
if (
150+
!shallowEqualArrays(suggestions, prevProps.suggestions) &&
151+
suggestions.length > 0 &&
152+
highlightFirstSuggestion
153+
) {
154+
this.highlightFirstSuggestion();
149155
return;
150156
}
151157

152-
const { highlightedSectionIndex, highlightedSuggestionIndex } = this.state;
153-
154-
if (
155-
highlightedSectionIndex !== prevState.highlightedSectionIndex ||
156-
highlightedSuggestionIndex !== prevState.highlightedSuggestionIndex
157-
) {
158-
const suggestion = this.getHighlightedSuggestion();
158+
if (onSuggestionHighlighted) {
159+
const highlightedSuggestion = this.getHighlightedSuggestion();
160+
const prevHighlightedSuggestion = prevState.highlightedSuggestion;
159161

160-
onSuggestionHighlighted({ suggestion });
162+
if (highlightedSuggestion != prevHighlightedSuggestion) {
163+
onSuggestionHighlighted({
164+
suggestion: highlightedSuggestion
165+
});
166+
}
161167
}
162168
}
163169

@@ -181,6 +187,10 @@ export default class Autosuggest extends Component {
181187
return {
182188
highlightedSectionIndex: sectionIndex,
183189
highlightedSuggestionIndex: suggestionIndex,
190+
highlightedSuggestion:
191+
suggestionIndex === null
192+
? null
193+
: this.getSuggestion(sectionIndex, suggestionIndex),
184194
valueBeforeUpDown
185195
};
186196
});
@@ -193,6 +203,7 @@ export default class Autosuggest extends Component {
193203
return {
194204
highlightedSectionIndex: null,
195205
highlightedSuggestionIndex: null,
206+
highlightedSuggestion: null,
196207
valueBeforeUpDown: shouldResetValueBeforeUpDown
197208
? null
198209
: valueBeforeUpDown
@@ -210,6 +221,7 @@ export default class Autosuggest extends Component {
210221
this.setState({
211222
highlightedSectionIndex: null,
212223
highlightedSuggestionIndex: null,
224+
highlightedSuggestion: null,
213225
valueBeforeUpDown: null,
214226
isCollapsed: true
215227
});
@@ -407,6 +419,7 @@ export default class Autosuggest extends Component {
407419
isFocused: false,
408420
highlightedSectionIndex: null,
409421
highlightedSuggestionIndex: null,
422+
highlightedSuggestion: null,
410423
valueBeforeUpDown: null,
411424
isCollapsed: !shouldRender
412425
});
@@ -461,7 +474,8 @@ export default class Autosuggest extends Component {
461474
getSectionSuggestions,
462475
theme,
463476
getSuggestionValue,
464-
alwaysRenderSuggestions
477+
alwaysRenderSuggestions,
478+
highlightFirstSuggestion
465479
} = this.props;
466480
const {
467481
isFocused,
@@ -520,8 +534,13 @@ export default class Autosuggest extends Component {
520534
this.maybeCallOnChange(event, value, 'type');
521535

522536
this.setState({
523-
highlightedSectionIndex: null,
524-
highlightedSuggestionIndex: null,
537+
...(highlightFirstSuggestion
538+
? {}
539+
: {
540+
highlightedSectionIndex: null,
541+
highlightedSuggestionIndex: null,
542+
highlightedSuggestion: null
543+
}),
525544
valueBeforeUpDown: null,
526545
isCollapsed: !shouldRender
527546
});
@@ -602,7 +621,7 @@ export default class Autosuggest extends Component {
602621
this.closeSuggestions();
603622
}
604623

605-
if (highlightedSuggestion !== null) {
624+
if (highlightedSuggestion != null) {
606625
const newValue = getSuggestionValue(highlightedSuggestion);
607626

608627
this.maybeCallOnChange(event, newValue, 'enter');

test/focus-first-suggestion/AutosuggestApp.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,15 @@ describe('Autosuggest with highlightFirstSuggestion={true}', () => {
149149
suggestion: { name: 'Perl', year: 1987 }
150150
});
151151
});
152+
153+
it('should be called once with the new suggestion when typing more changes the autohighlighted suggestion', () => {
154+
focusAndSetInputValue('c');
155+
onSuggestionHighlighted.reset();
156+
focusAndSetInputValue('c+');
157+
expect(onSuggestionHighlighted).to.have.been.calledOnce;
158+
expect(onSuggestionHighlighted).to.have.been.calledWithExactly({
159+
suggestion: { name: 'C++', year: 1983 }
160+
});
161+
});
152162
});
153163
});

0 commit comments

Comments
 (0)