From 03a12413d632a2a1613091e44c52ce46b4adc5b8 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Fri, 19 Apr 2024 09:02:56 +0530 Subject: Minor refactoring to move repeated code into utility class --- src/components/ui/input/index.tsx | 3 ++- src/components/ui/select/index.tsx | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src/components/ui') diff --git a/src/components/ui/input/index.tsx b/src/components/ui/input/index.tsx index 225517ba8..7e3179baf 100644 --- a/src/components/ui/input/index.tsx +++ b/src/components/ui/input/index.tsx @@ -16,6 +16,7 @@ import { injectIntl, } from 'react-intl'; import withStyles, { type WithStylesProps } from 'react-jss'; +import { isEnterKeyPress } from '../../../jsUtils'; // biome-ignore lint/suspicious/noShadowRestrictedNames: import Error from '../error'; import Icon from '../icon'; @@ -96,7 +97,7 @@ class Input extends Component { } onInputKeyPress(e: KeyboardEvent): void { - if (e.key === 'Enter') { + if (isEnterKeyPress(e.key)) { const { onEnterKey = noop } = this.props; onEnterKey(); } diff --git a/src/components/ui/select/index.tsx b/src/components/ui/select/index.tsx index 397fd97ed..c19c12417 100644 --- a/src/components/ui/select/index.tsx +++ b/src/components/ui/select/index.tsx @@ -13,6 +13,11 @@ import { createRef, } from 'react'; import withStyles, { type WithStylesProps } from 'react-jss'; +import { + isArrowDownKeyPress, + isArrowUpKeyPress, + isEnterKeyPress, +} from '../../../jsUtils'; import type { Theme } from '../../../themes'; // biome-ignore lint/suspicious/noShadowRestrictedNames: import Error from '../error'; @@ -288,23 +293,23 @@ class SelectComponent extends Component { if (!open) return; - if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { + if (isArrowUpKeyPress(e.key) || isArrowDownKeyPress(e.key)) { e.preventDefault(); } if (this.componentRef?.current) { - if (e.key === 'ArrowUp' && selected > 0) { + if (isArrowUpKeyPress(e.key) && selected > 0) { this.setState((state: IState) => ({ selected: state.selected - 1, })); } else if ( - e.key === 'ArrowDown' && + isArrowDownKeyPress(e.key) && selected < Object.keys(options!).length - 1 ) { this.setState((state: IState) => ({ selected: state.selected + 1, })); - } else if (e.key === 'Enter') { + } else if (isEnterKeyPress(e.key)) { this.select(Object.keys(options!)[selected]); } -- cgit v1.2.3-54-g00ecf