Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CustomSelect,ChipsSelect): disabled with screen readers #6716

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions packages/vkui/src/components/ChipsSelect/ChipsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
isNotServicePreset,
renderOptionDefault,
} from './constants';
import type { FocusActionType, OptionPreset } from './types';
import type { FocusActionType } from './types';
import { useChipsSelect, type UseChipsSelectProps } from './useChipsSelect';
import styles from './ChipsSelect.module.css';

Expand All @@ -40,37 +40,6 @@ const stylesDropdownVerticalPlacement = {
bottom: styles['ChipsSelect--pop-down'],
} as const;

const findIndexAfter = <O extends ChipOption>(
options: Array<OptionPreset<O>> = [],
startIndex = -1,
) => {
if (startIndex >= options.length - 1) {
return -1;
}
return options.findIndex(
(option, i) => i > startIndex && (!isNotServicePreset(option) || !option.disabled),
);
};

const findIndexBefore = <O extends ChipOption>(
options: Array<OptionPreset<O>> = [],
endIndex: number = options.length,
) => {
let result = -1;
if (endIndex <= 0) {
return result;
}
for (let i = endIndex - 1; i >= 0; i--) {
let option = options[i];

if (!isNotServicePreset(option) || !option.disabled) {
result = i;
break;
}
}
return result;
};

export interface ChipsSelectProps<O extends ChipOption>
extends ChipsInputBaseProps<O>,
UseChipsSelectProps<O>,
Expand Down Expand Up @@ -283,12 +252,6 @@ export const ChipsSelect = <Option extends ChipOption>({
return;
}

const option = options[index];

if (isNotServicePreset(option) && option.disabled) {
return;
}

scrollToElement(index);
setFocusedOptionIndex(index);
};
Expand All @@ -297,11 +260,9 @@ export const ChipsSelect = <Option extends ChipOption>({
let index = nextIndex === null ? -1 : nextIndex;

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

focusOptionByIndex(index, focusedOptionIndex);
Expand Down Expand Up @@ -337,7 +298,7 @@ export const ChipsSelect = <Option extends ChipOption>({
}
if (focusedOptionIndex != null) {
const foundOption = options[focusedOptionIndex];
if (foundOption && isNotServicePreset(foundOption)) {
if (foundOption && isNotServicePreset(foundOption) && !foundOption.disabled) {
event.preventDefault();

if (onChangeStart) {
Expand Down
44 changes: 7 additions & 37 deletions packages/vkui/src/components/CustomSelect/CustomSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,6 @@ const sizeYClassNames = {
['compact']: styles['CustomSelect--sizeY-compact'],
};

const findIndexAfter = (options: CustomSelectOptionInterface[] = [], startIndex = -1) => {
if (startIndex >= options.length - 1) {
return -1;
}
return options.findIndex((option, i) => i > startIndex && !option.disabled);
};

const findIndexBefore = (
options: CustomSelectOptionInterface[] = [],
endIndex: number = options.length,
) => {
let result = -1;
if (endIndex <= 0) {
return result;
}
for (let i = endIndex - 1; i >= 0; i--) {
let option = options[i];

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

const warn = warnOnce('CustomSelect');

const checkOptionsValueType = <T extends CustomSelectOptionInterface>(options: T[]) => {
Expand Down Expand Up @@ -339,12 +313,6 @@ export function CustomSelect<OptionInterfaceT extends CustomSelectOptionInterfac
return;
}

const option = options[index];

if (option?.disabled) {
return;
}

if (scrollTo) {
scrollToElement(index);
}
Expand Down Expand Up @@ -416,6 +384,10 @@ export function CustomSelect<OptionInterfaceT extends CustomSelectOptionInterfac
(index: number) => {
const item = options[index];

if (item.disabled) {
return;
}

setNativeSelectValue(item?.value);
close();

Expand Down Expand Up @@ -472,14 +444,12 @@ export function CustomSelect<OptionInterfaceT extends CustomSelectOptionInterfac

const focusOption = React.useCallback(
(type: 'next' | 'prev') => {
let index = focusedOptionIndex;
let index = focusedOptionIndex ?? -1;

if (type === 'next') {
const nextIndex = findIndexAfter(options, index);
index = nextIndex === -1 ? findIndexAfter(options) : nextIndex; // Следующий за index или первый валидный до index
index = index >= options.length - 1 ? 0 : ++index;
} else if (type === 'prev') {
const beforeIndex = findIndexBefore(options, index);
index = beforeIndex === -1 ? findIndexBefore(options) : beforeIndex; // Предшествующий index или последний валидный после index
index = index <= 0 ? options.length - 1 : --index;
}

focusOptionByIndex(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface CustomSelectOptionProps extends HTMLAttributesWithRootRef<HTMLD
export const CustomSelectOption = ({
children,
hierarchy = 0,
hovered: hoveredProp,
hovered,
selected,
before,
after,
Expand All @@ -82,7 +82,6 @@ export const CustomSelectOption = ({
: styleProp,
[hierarchy, styleProp],
);
const hovered = hoveredProp && !disabled ? true : false;

return (
<Paragraph
Expand Down
Loading