Skip to content

Commit

Permalink
fix(review): semantic enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
inomdzhon committed Jan 16, 2024
1 parent 67ab7c0 commit 377ca23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -68,10 +68,10 @@ export const ChipsInputBase = <O extends ChipOption>({

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<HTMLElement>(`[data-index="${index}"]`);

Expand Down
14 changes: 7 additions & 7 deletions packages/vkui/src/components/ChipsInputBase/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_INPUT_VALUE } from './constants';
import type { ChipOption, ChipOptionValue } from './types';
import type { ChipOption, ChipOptionValue, NavigateTo } from './types';

/**
* @private
Expand All @@ -16,11 +16,11 @@ export const isInputValueEmpty = (value: string) => value === DEFAULT_INPUT_VALU
* @private
*/
export const getChipOptionIndexByValueProp = <O extends ChipOption>(
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);
};

/**
Expand All @@ -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':
Expand Down
2 changes: 2 additions & 0 deletions packages/vkui/src/components/ChipsInputBase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 377ca23

Please sign in to comment.