Skip to content

Commit

Permalink
fix(ChipsSelect): add disabled prop
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackySoul committed Feb 27, 2024
1 parent 326a81a commit 21922e6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/vkui/src/components/Chip/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type ChipValue = string | number;
export interface ChipOption {
value?: ChipValue;
label?: string;
disabled?: boolean;
[otherProp: string]: any;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const groups = [
value: '1',
label: 'Arctic Monkeys',
},
{ value: '2', label: 'Звери' },
{ value: '2', label: 'Звери', disabled: true },
{ value: '4', label: 'FACE' },
{
value: '3',
Expand Down
46 changes: 42 additions & 4 deletions packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ import { IconButton } from '../IconButton/IconButton';
import { Footnote } from '../Typography/Footnote/Footnote';
import styles from './ChipsSelect.module.css';

const findIndexAfter = <O extends ChipOption>(options: O[] = [], startIndex = -1) => {
if (startIndex >= options.length - 1) {
return -1;

Check warning on line 25 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L25

Added line #L25 was not covered by tests
}
return options.findIndex((option, i) => i > startIndex && !option.disabled);
};

const findIndexBefore = <O extends ChipOption>(
options: O[] = [],
endIndex: number = options.length,

Check warning on line 32 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L31-L32

Added lines #L31 - L32 were not covered by tests
) => {
let result = -1;
if (endIndex <= 0) {
return result;

Check warning on line 36 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L34-L36

Added lines #L34 - L36 were not covered by tests
}
for (let i = endIndex - 1; i >= 0; i--) {
let option = options[i];

Check warning on line 39 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L38-L39

Added lines #L38 - L39 were not covered by tests

if (!option.disabled) {
result = i;
break;

Check warning on line 43 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L41-L43

Added lines #L41 - L43 were not covered by tests
}
}
return result;

Check warning on line 46 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L46

Added line #L46 was not covered by tests
};

export interface ChipsSelectProps<Option extends ChipOption>
extends Omit<ChipsInputProps<Option>, 'after'> {
popupDirection?: 'top' | 'bottom';
Expand Down Expand Up @@ -224,6 +250,12 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O
return;
}

const option = filteredOptions[index];

if (option?.disabled) {
return;

Check warning on line 256 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L256

Added line #L256 was not covered by tests
}

scrollToElement(index);
setFocusedOptionIndex(index);
};
Expand All @@ -232,9 +264,11 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O
let index = nextIndex === null ? -1 : nextIndex;

if (type === FOCUS_ACTION_NEXT) {
index = index + 1;
const nextIndex = findIndexAfter(filteredOptions, index);
index = nextIndex === -1 ? findIndexAfter(filteredOptions) : nextIndex; // Следующий за index или первый валидный до index
} else if (type === FOCUS_ACTION_PREV) {
index = index - 1;
const beforeIndex = findIndexBefore(filteredOptions, index);
index = beforeIndex === -1 ? findIndexBefore(filteredOptions) : beforeIndex; // Предшествующий index или последний валидный после index

Check warning on line 271 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L270-L271

Added lines #L270 - L271 were not covered by tests
}

focusOptionByIndex(index, focusedOptionIndex);
Expand All @@ -248,7 +282,7 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O

if (!opened) {
setOpened(true);
setFocusedOptionIndex(0);
focusOption(null, FOCUS_ACTION_NEXT);

Check warning on line 285 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L285

Added line #L285 was not covered by tests
} else {
focusOption(focusedOptionIndex, FOCUS_ACTION_PREV);
}
Expand All @@ -259,7 +293,7 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O

if (!opened) {
setOpened(true);
setFocusedOptionIndex(0);
focusOption(null, FOCUS_ACTION_NEXT);

Check warning on line 296 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L296

Added line #L296 was not covered by tests
} else {
focusOption(focusedOptionIndex, FOCUS_ACTION_NEXT);
}
Expand Down Expand Up @@ -424,6 +458,7 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O
option,
hovered: Boolean(hovered),
children: label,
disabled: option.disabled,
selected: !!selected,
getRootRef: (e) => {
if (e) {
Expand All @@ -432,6 +467,9 @@ export const ChipsSelect = <Option extends ChipOption>(props: ChipsSelectProps<O
return undefined;
},
onMouseDown: (e: React.MouseEvent<HTMLDivElement>) => {
if (option.disabled) {
return;

Check warning on line 471 in packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx#L471

Added line #L471 was not covered by tests
}
onChangeStart?.(e, option);

if (!e.defaultPrevented) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vkui/src/components/ChipsSelect/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const groups = [
label: 'Arctic Monkeys',
src: getAvatarUrl('audio_arctic_monkeys'),
},
{ value: '2', label: 'Звери', src: getAvatarUrl('audio_leto_zveri') },
{ value: '2', label: 'Звери', disabled: true, src: getAvatarUrl('audio_leto_zveri') },
{ value: '4', label: 'FACE', src: getAvatarUrl('audio_face') },
{
value: '3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export interface CustomSelectOptionProps extends HTMLAttributesWithRootRef<HTMLD
/**
* Блокирует весь блок.
*
* > ⚠️ Важно: если CustomSelectOption используется внутри [Select](https://vkcom.github.io/VKUI/#/Select) или [CustomSelect](https://vkcom.github.io/VKUI/#/CustomSelect), то свойство явно должно выставляться только через структуру `options`.
* > Запрещается выставлять `disabled` проп опциям в обход `options`, иначе [CustomSelect](https://vkcom.github.io/VKUI/#/CustomSelect) не будет знать об актуальном состоянии
* > ⚠️ Важно: если CustomSelectOption используется внутри [Select](https://vkcom.github.io/VKUI/#/Select), [CustomSelect](https://vkcom.github.io/VKUI/#/CustomSelect) или [ChipsSelect](https://vkcom.github.io/VKUI/#/ChipsSelect), то свойство явно должно выставляться только через структуру `options`.
* > Запрещается выставлять `disabled` проп опциям в обход `options`, иначе [CustomSelect](https://vkcom.github.io/VKUI/#/CustomSelect) и [ChipsSelect](https://vkcom.github.io/VKUI/#/ChipsSelect) не будут знать об актуальном состоянии
* опции.
*/
disabled?: boolean;
Expand Down

0 comments on commit 21922e6

Please sign in to comment.