Skip to content

Commit

Permalink
fix(Select): reject focus of unmounted input (#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
lossir committed Nov 20, 2023
1 parent 12831c0 commit f3ac028
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/react-ui/components/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ReactNode, ReactPortal, AriaAttributes } from 'react';
import invariant from 'invariant';
import { globalObject } from '@skbkontur/global-object';
import debounce from 'lodash.debounce';

import {
isKeyArrowDown,
Expand Down Expand Up @@ -488,7 +489,7 @@ export class Select<TValue = {}, TItem = {}> extends React.Component<SelectProps
private getSearch = () => {
return (
<div className={styles.search()} onKeyDown={this.handleKey}>
<Input ref={this.focusInput} onValueChange={this.handleSearch} width="100%" />
<Input ref={this.debouncedFocusInput} onValueChange={this.handleSearch} width="100%" />
</div>
);
};
Expand Down Expand Up @@ -516,7 +517,7 @@ export class Select<TValue = {}, TItem = {}> extends React.Component<SelectProps
<Input
autoFocus
value={this.state.searchPattern}
ref={this.focusInput}
ref={this.debouncedFocusInput}
onValueChange={this.handleSearch}
width="100%"
/>
Expand Down Expand Up @@ -569,10 +570,10 @@ export class Select<TValue = {}, TItem = {}> extends React.Component<SelectProps
return getRootNode(this);
};

private focusInput = (input: Input) => {
// fix cases when an Input is rendered in portal
globalObject.setTimeout(() => input?.focus(), 0);
};
// fix cases when an Input is rendered in portal
// https://github.com/skbkontur/retail-ui/issues/1995
private focusInput = (input: Input) => input?.focus();
private debouncedFocusInput = debounce(this.focusInput);

private refMenu = (menu: Menu) => {
this.menu = menu;
Expand Down

0 comments on commit f3ac028

Please sign in to comment.