Skip to content

Commit

Permalink
Merge pull request #721 from vrk-kpa/fix/dropdown-unmounted
Browse files Browse the repository at this point in the history
[Fix] Dropdown unmounted
  • Loading branch information
riitasointi authored May 12, 2023
2 parents 07d6f4c + c6ce317 commit 9e825d5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/core/Dropdown/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class BaseDropdown extends Component<DropdownProps> {

popoverRef: React.RefObject<HTMLUListElement>;

componentIsMounted: boolean;

constructor(props: DropdownProps) {
super(props);
this.buttonRef = React.createRef();
Expand Down Expand Up @@ -235,6 +237,14 @@ class BaseDropdown extends Component<DropdownProps> {
}
}

componentDidMount(): void {
this.componentIsMounted = true;
}

componentWillUnmount(): void {
this.componentIsMounted = false;
}

private isOutsideClick(event: MouseEvent) {
return (
!!this.buttonRef &&
Expand All @@ -256,10 +266,13 @@ class BaseDropdown extends Component<DropdownProps> {
ariaExpanded: false,
});
setTimeout(() => {
// NVDA with Firefox requires small timeout before focus to read updated value
this.buttonRef.current?.focus();
// Set showPopover separately to prevent NVDA focus from going to body
this.setState({ showPopover: false });
// Check mounted status to prevent setting state on an unmounted component
if (this.componentIsMounted) {
// NVDA with Firefox requires small timeout before focus to read updated value
this.buttonRef.current?.focus();
// Set showPopover separately to prevent NVDA focus from going to body
this.setState({ showPopover: false });
}
}, 10);
}

Expand Down

0 comments on commit 9e825d5

Please sign in to comment.