diff options
author | 2024-04-19 09:02:56 +0530 | |
---|---|---|
committer | 2024-04-19 03:52:01 +0000 | |
commit | 03a12413d632a2a1613091e44c52ce46b4adc5b8 (patch) | |
tree | 0086e0f98a644df91644b9cdde5ca1515188113a /src/components/ui | |
parent | 6.7.3-nightly.13 [skip ci] (diff) | |
download | ferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.tar.gz ferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.tar.zst ferdium-app-03a12413d632a2a1613091e44c52ce46b4adc5b8.zip |
Minor refactoring to move repeated code into utility class
Diffstat (limited to 'src/components/ui')
-rw-r--r-- | src/components/ui/input/index.tsx | 3 | ||||
-rw-r--r-- | src/components/ui/select/index.tsx | 13 |
2 files changed, 11 insertions, 5 deletions
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 { | |||
16 | injectIntl, | 16 | injectIntl, |
17 | } from 'react-intl'; | 17 | } from 'react-intl'; |
18 | import withStyles, { type WithStylesProps } from 'react-jss'; | 18 | import withStyles, { type WithStylesProps } from 'react-jss'; |
19 | import { isEnterKeyPress } from '../../../jsUtils'; | ||
19 | // biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> | 20 | // biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> |
20 | import Error from '../error'; | 21 | import Error from '../error'; |
21 | import Icon from '../icon'; | 22 | import Icon from '../icon'; |
@@ -96,7 +97,7 @@ class Input extends Component<IProps, IState> { | |||
96 | } | 97 | } |
97 | 98 | ||
98 | onInputKeyPress(e: KeyboardEvent<HTMLInputElement>): void { | 99 | onInputKeyPress(e: KeyboardEvent<HTMLInputElement>): void { |
99 | if (e.key === 'Enter') { | 100 | if (isEnterKeyPress(e.key)) { |
100 | const { onEnterKey = noop } = this.props; | 101 | const { onEnterKey = noop } = this.props; |
101 | onEnterKey(); | 102 | onEnterKey(); |
102 | } | 103 | } |
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 { | |||
13 | createRef, | 13 | createRef, |
14 | } from 'react'; | 14 | } from 'react'; |
15 | import withStyles, { type WithStylesProps } from 'react-jss'; | 15 | import withStyles, { type WithStylesProps } from 'react-jss'; |
16 | import { | ||
17 | isArrowDownKeyPress, | ||
18 | isArrowUpKeyPress, | ||
19 | isEnterKeyPress, | ||
20 | } from '../../../jsUtils'; | ||
16 | import type { Theme } from '../../../themes'; | 21 | import type { Theme } from '../../../themes'; |
17 | // biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> | 22 | // biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation> |
18 | import Error from '../error'; | 23 | import Error from '../error'; |
@@ -288,23 +293,23 @@ class SelectComponent extends Component<IProps, IState> { | |||
288 | 293 | ||
289 | if (!open) return; | 294 | if (!open) return; |
290 | 295 | ||
291 | if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { | 296 | if (isArrowUpKeyPress(e.key) || isArrowDownKeyPress(e.key)) { |
292 | e.preventDefault(); | 297 | e.preventDefault(); |
293 | } | 298 | } |
294 | 299 | ||
295 | if (this.componentRef?.current) { | 300 | if (this.componentRef?.current) { |
296 | if (e.key === 'ArrowUp' && selected > 0) { | 301 | if (isArrowUpKeyPress(e.key) && selected > 0) { |
297 | this.setState((state: IState) => ({ | 302 | this.setState((state: IState) => ({ |
298 | selected: state.selected - 1, | 303 | selected: state.selected - 1, |
299 | })); | 304 | })); |
300 | } else if ( | 305 | } else if ( |
301 | e.key === 'ArrowDown' && | 306 | isArrowDownKeyPress(e.key) && |
302 | selected < Object.keys(options!).length - 1 | 307 | selected < Object.keys(options!).length - 1 |
303 | ) { | 308 | ) { |
304 | this.setState((state: IState) => ({ | 309 | this.setState((state: IState) => ({ |
305 | selected: state.selected + 1, | 310 | selected: state.selected + 1, |
306 | })); | 311 | })); |
307 | } else if (e.key === 'Enter') { | 312 | } else if (isEnterKeyPress(e.key)) { |
308 | this.select(Object.keys(options!)[selected]); | 313 | this.select(Object.keys(options!)[selected]); |
309 | } | 314 | } |
310 | 315 | ||