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 6 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
59 changes: 56 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ 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;
}> = ({
prefixCls,
className,
Expand All @@ -94,11 +97,16 @@ const InternalSegmentedOption: React.FC<{
value,
name,
onChange,
onFocus,
onBlur,
onKeyDown,
}) => {
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) {
return;
}
// Do not add focus style when clicking
onBlur();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

手动调用 onBlur() 可能导致意外行为

handleChange 函数中手动调用 onBlur() 可能会导致与正常的事件流不一致,可能引发意外的焦点丢失或其他副作用。建议重新评估这种调用的必要性,或者寻找更符合 React 事件处理规范的方法。

aojunhao123 marked this conversation as resolved.
Show resolved Hide resolved
onChange(event, value);
};

Expand All @@ -111,16 +119,17 @@ const InternalSegmentedOption: React.FC<{
<input
name={name}
className={`${prefixCls}-item-input`}
aria-hidden="true"
type="radio"
disabled={disabled}
checked={checked}
onChange={handleChange}
onFocus={onFocus}
onBlur={onBlur}
onKeyDown={onKeyDown}
/>
<div
className={`${prefixCls}-item-label`}
title={title}
role="option"
aria-selected={checked}
>
{label}
Expand Down Expand Up @@ -176,10 +185,49 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(

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

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

const currentIndex = segmentedOptions.findIndex(
(option) => option.value === rawValue,
);

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

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

// ======================= Keyboard ========================
const handleKeyDown = (event: React.KeyboardEvent) => {
const total = segmentedOptions.length;
let nextIndex = currentIndex;

switch (event.key) {
case 'ArrowLeft':
case 'ArrowUp':
nextIndex = currentIndex === 0 ? total - 1 : currentIndex - 1;
aojunhao123 marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'ArrowRight':
case 'ArrowDown':
nextIndex = currentIndex === total - 1 ? 0 : currentIndex + 1;
break;
}

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

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 +270,15 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
{
[`${prefixCls}-item-selected`]:
segmentedOption.value === rawValue && !thumbShow,
[`${prefixCls}-item-focused`]:
isFocused && segmentedOption.value === rawValue,
},
)}
checked={segmentedOption.value === rawValue}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
disabled={!!disabled || !!segmentedOption.disabled}
/>
))}
Expand Down
Loading
Loading