From 377ca232e0ab253caf15a36b8a154ae9b9465ff3 Mon Sep 17 00:00:00 2001 From: Inomdzhon Mirdzhamolov Date: Tue, 16 Jan 2024 15:45:38 +0300 Subject: [PATCH] fix(review): semantic enhancement --- .../components/ChipsInputBase/ChipsInputBase.tsx | 8 ++++---- .../vkui/src/components/ChipsInputBase/helpers.ts | 14 +++++++------- .../vkui/src/components/ChipsInputBase/types.ts | 2 ++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/vkui/src/components/ChipsInputBase/ChipsInputBase.tsx b/packages/vkui/src/components/ChipsInputBase/ChipsInputBase.tsx index 1465a7fa82..039f64bd61 100644 --- a/packages/vkui/src/components/ChipsInputBase/ChipsInputBase.tsx +++ b/packages/vkui/src/components/ChipsInputBase/ChipsInputBase.tsx @@ -12,10 +12,10 @@ import { getChipOptionIndexByHTMLElement, getChipOptionIndexByValueProp, getChipOptionValueByHTMLElement, + getNextChipOptionIndexByNavigateToProp, isInputValueEmpty, - resolveNextChipOptionIndex, } from './helpers'; -import type { ChipOption, ChipOptionValue, ChipsInputBasePrivateProps } from './types'; +import type { ChipOption, ChipOptionValue, ChipsInputBasePrivateProps, NavigateTo } from './types'; import styles from './ChipsInputBase.module.css'; const sizeYClassNames = { @@ -68,10 +68,10 @@ export const ChipsInputBase = ({ const moveFocusToChipOption = ( currentIndex: number, - nextIndex: 'first' | 'prev' | 'next' | 'last', + navigateTo: NavigateTo, listboxEl: HTMLElement, ) => { - const index = resolveNextChipOptionIndex(currentIndex, nextIndex, valueLength); + const index = getNextChipOptionIndexByNavigateToProp(currentIndex, navigateTo, valueLength); // eslint-disable-next-line no-restricted-properties const foundEl = listboxEl.querySelector(`[data-index="${index}"]`); diff --git a/packages/vkui/src/components/ChipsInputBase/helpers.ts b/packages/vkui/src/components/ChipsInputBase/helpers.ts index 504e395f0d..687d1d2036 100644 --- a/packages/vkui/src/components/ChipsInputBase/helpers.ts +++ b/packages/vkui/src/components/ChipsInputBase/helpers.ts @@ -1,5 +1,5 @@ import { DEFAULT_INPUT_VALUE } from './constants'; -import type { ChipOption, ChipOptionValue } from './types'; +import type { ChipOption, ChipOptionValue, NavigateTo } from './types'; /** * @private @@ -16,11 +16,11 @@ export const isInputValueEmpty = (value: string) => value === DEFAULT_INPUT_VALU * @private */ export const getChipOptionIndexByValueProp = ( - v: O | ChipOptionValue, + optionProp: O | ChipOptionValue, valueProp: O[], ) => { - const value = isValueLikeChipOptionObject(v) ? v.value : v; - return valueProp.findIndex((o) => o.value === value); + const value = isValueLikeChipOptionObject(optionProp) ? optionProp.value : optionProp; + return valueProp.findIndex((option) => option.value === value); }; /** @@ -42,12 +42,12 @@ export const getChipOptionValueByHTMLElement = (el: HTMLElement | null) => { /** * @private */ -export const resolveNextChipOptionIndex = ( +export const getNextChipOptionIndexByNavigateToProp = ( currentIndex: number, - nextIndex: 'first' | 'prev' | 'next' | 'last', + navigateTo: NavigateTo, length: number, ) => { - switch (nextIndex) { + switch (navigateTo) { case 'first': return 0; case 'prev': diff --git a/packages/vkui/src/components/ChipsInputBase/types.ts b/packages/vkui/src/components/ChipsInputBase/types.ts index 10b2ffec36..787bb7648b 100644 --- a/packages/vkui/src/components/ChipsInputBase/types.ts +++ b/packages/vkui/src/components/ChipsInputBase/types.ts @@ -8,6 +8,8 @@ import type { } from '../../types'; import { FormFieldProps } from '../FormField/FormField'; +export type NavigateTo = 'first' | 'prev' | 'next' | 'last'; + export type ChipOptionValue = string | number; export type ChipOptionLabel = React.ReactElement | string | number;