Skip to content

Commit

Permalink
Fix onResolveSuggestions not being called after component is remounte…
Browse files Browse the repository at this point in the history
…d in react 18 strict mode (#28227)

Co-authored-by: KHMakoto <Humberto.Morimoto@microsoft.com>
  • Loading branch information
andyrooger and khmakoto authored Sep 9, 2024
1 parent 32c8cb7 commit 2a31760
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: Debouncing onResolveSuggestions correctly after a picker has been remounted in React 18 strict mode.",
"packageName": "@fluentui/react",
"email": "420834+andyrooger@users.noreply.github.com",
"dependentChangeType": "patch"
}
9 changes: 5 additions & 4 deletions packages/react/src/components/pickers/BasePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class BasePicker<T extends {}, P extends IBasePickerProps<T>>
private _styledSuggestions = getStyledSuggestions(this.SuggestionOfProperType);
private _id: string;
private _async: Async;
private _onResolveSuggestionsDebounced: (updatedValue: string) => void;
private _overrideScrollDismiss = false;
private _overrideScrollDimissTimeout: number;

Expand All @@ -129,7 +130,6 @@ export class BasePicker<T extends {}, P extends IBasePickerProps<T>>
super(basePickerProps);

initializeComponentRef(this);
this._async = new Async(this);

const items: T[] = basePickerProps.selectedItems || basePickerProps.defaultSelectedItems || [];

Expand Down Expand Up @@ -160,8 +160,9 @@ export class BasePicker<T extends {}, P extends IBasePickerProps<T>>
}

public componentDidMount(): void {
this._async = new Async(this);
this.selection.setItems(this.state.items);
this._onResolveSuggestions = this._async.debounce(this._onResolveSuggestions, this.props.resolveDelay);
this._onResolveSuggestionsDebounced = this._async.debounce(this._onResolveSuggestions, this.props.resolveDelay);
}

public componentDidUpdate(oldProps: P, oldState: IBasePickerState<T>) {
Expand Down Expand Up @@ -485,7 +486,7 @@ export class BasePicker<T extends {}, P extends IBasePickerProps<T>>
}

protected updateValue(updatedValue: string) {
this._onResolveSuggestions(updatedValue);
this._onResolveSuggestionsDebounced(updatedValue);
}

protected updateSuggestionsList(suggestions: T[] | PromiseLike<T[]>, updatedValue?: string) {
Expand Down Expand Up @@ -1086,7 +1087,7 @@ export class BasePicker<T extends {}, P extends IBasePickerProps<T>>
this.onEmptyInputFocus();
} else {
if (this.suggestionStore.suggestions.length === 0) {
this._onResolveSuggestions(input);
this._onResolveSuggestionsDebounced(input);
} else {
this.setState({
isMostRecentlyUsedVisible: false,
Expand Down

0 comments on commit 2a31760

Please sign in to comment.