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

feat: improve keyboard operation accessibility #285

Merged
merged 10 commits into from
Nov 28, 2024
Merged
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
11 changes: 11 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,14 @@
direction: rtl;
}
}

.rc-segmented-item {
&:focus {
outline: none;
}

&-focused {
border-radius: 2px;
box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}
}
3 changes: 3 additions & 0 deletions docs/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export default function App() {
<div className="wrapper">
<Segmented
options={['iOS', 'Android', 'Web']}
defaultValue="Android"
name="segmented1"
onChange={(value) => console.log(value, typeof value)}
/>
</div>
<div className="wrapper">
<Segmented
vertical
options={['iOS', 'Android', 'Web']}
name="segmented2"
onChange={(value) => console.log(value, typeof value)}
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@rc-component/father-plugin": "^1.0.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.2.1",
"@testing-library/user-event": "^14.5.2",
"@types/classnames": "^2.2.9",
"@types/jest": "^29.2.4",
"@types/react": "^18.3.11",
Expand Down
81 changes: 78 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ const InternalSegmentedOption: React.FC<{
e: React.ChangeEvent<HTMLInputElement>,
value: SegmentedRawOption,
) => void;
onFocus: (e: React.FocusEvent<HTMLInputElement>) => void;
onBlur: (e?: React.FocusEvent<HTMLInputElement>) => void;
onKeyDown: (e: React.KeyboardEvent) => void;
onKeyUp: (e: React.KeyboardEvent) => void;
onMouseDown: () => void;
}> = ({
prefixCls,
className,
Expand All @@ -94,6 +99,11 @@ const InternalSegmentedOption: React.FC<{
value,
name,
onChange,
onFocus,
onBlur,
onKeyDown,
onKeyUp,
onMouseDown,
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
Expand All @@ -107,20 +117,23 @@ const InternalSegmentedOption: React.FC<{
className={classNames(className, {
[`${prefixCls}-item-disabled`]: disabled,
})}
onMouseDown={onMouseDown}
>
<input
name={name}
className={`${prefixCls}-item-input`}
aria-hidden="true"
type="radio"
disabled={disabled}
checked={checked}
onChange={handleChange}
onFocus={onFocus}
onBlur={onBlur}
onKeyDown={onKeyDown}
onKeyUp={onKeyUp}
/>
<div
className={`${prefixCls}-item-label`}
title={title}
role="option"
aria-selected={checked}
>
{label}
Expand Down Expand Up @@ -176,10 +189,63 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(

const divProps = omit(restProps, ['children']);

// ======================= Focus ========================
const [isKeyboard, setIsKeyboard] = React.useState(false);
const [isFocused, setIsFocused] = React.useState(false);

const handleFocus = () => {
setIsFocused(true);
};

const handleBlur = () => {
setIsFocused(false);
};

const handleMouseDown = () => {
setIsKeyboard(false);
};

// capture keyboard tab interaction for correct focus style
const handleKeyUp = (event: React.KeyboardEvent) => {
if (event.key === 'Tab') {
setIsKeyboard(true);
}
};

// ======================= Keyboard ========================
const onOffset = (offset: number) => {
const currentIndex = segmentedOptions.findIndex(
(option) => option.value === rawValue,
);

const total = segmentedOptions.length;
const nextIndex = (currentIndex + offset + total) % total;

const nextOption = segmentedOptions[nextIndex];
if (nextOption) {
setRawValue(nextOption.value);
onChange?.(nextOption.value);
}
};
aojunhao123 marked this conversation as resolved.
Show resolved Hide resolved

const handleKeyDown = (event: React.KeyboardEvent) => {
switch (event.key) {
case 'ArrowLeft':
case 'ArrowUp':
onOffset(-1);
break;
case 'ArrowRight':
case 'ArrowDown':
onOffset(1);
break;
}
};

return (
<div
role="listbox"
role="radiogroup"
aria-label="segmented control"
tabIndex={disabled ? undefined : 0}
aojunhao123 marked this conversation as resolved.
Show resolved Hide resolved
{...divProps}
className={classNames(
prefixCls,
Expand Down Expand Up @@ -222,10 +288,19 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
[`${prefixCls}-item-focused`]:
isFocused &&
isKeyboard &&
segmentedOption.value === rawValue,
},
)}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onMouseDown={handleMouseDown}
disabled={!!disabled || !!segmentedOption.disabled}
/>
))}
Expand Down
Loading