cb9d836227
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
137 lines
3.8 KiB
React
137 lines
3.8 KiB
React
import React, { useRef } from 'react';
|
|
import { Tooltip } from '@/components/ui';
|
|
import { optionKey, optionValue } from './constraints';
|
|
import './creation-params.css';
|
|
|
|
/**
|
|
* 通用单选按钮组(radiogroup 语义 + roving tabindex),供 RatioPicker /
|
|
* ChipGroup / DurationSegmented 复用。
|
|
*
|
|
* - 方向键在可用选项间移动并选中,Home / End 跳到首尾;
|
|
* - 被禁用的选项用 aria-disabled(而非 disabled),以便悬停 / 聚焦时仍能看到原因 Tooltip;
|
|
* - 整组禁用(disabledReason)时所有选项不可选,原因挂在组外层 Tooltip 上。
|
|
*/
|
|
const OptionGroup = ({
|
|
options = [],
|
|
value,
|
|
onChange,
|
|
ariaLabel,
|
|
optionDisabledReasons = {},
|
|
disabledReason = '',
|
|
renderOption,
|
|
getOptionAriaLabel,
|
|
variant = 'chips',
|
|
className = '',
|
|
}) => {
|
|
const refs = useRef([]);
|
|
const groupDisabled = Boolean(disabledReason);
|
|
const selectedKey =
|
|
value === undefined || value === null ? null : optionKey(value);
|
|
|
|
const isDisabled = (opt) =>
|
|
groupDisabled || Boolean(optionDisabledReasons[optionKey(opt)]);
|
|
|
|
const selectedIndex = options.findIndex((o) => optionKey(o) === selectedKey);
|
|
const firstEnabled = options.findIndex((o) => !isDisabled(o));
|
|
const tabStop =
|
|
selectedIndex >= 0 ? selectedIndex : firstEnabled >= 0 ? firstEnabled : 0;
|
|
|
|
const select = (opt) => {
|
|
if (isDisabled(opt)) return;
|
|
if (optionKey(opt) === selectedKey) return;
|
|
onChange && onChange(optionValue(opt));
|
|
};
|
|
|
|
const moveTo = (from, step) => {
|
|
const n = options.length;
|
|
for (let i = 1; i <= n; i += 1) {
|
|
const idx = (from + step * i + n * n) % n;
|
|
if (!isDisabled(options[idx])) {
|
|
refs.current[idx] && refs.current[idx].focus();
|
|
select(options[idx]);
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
|
|
const onKeyDown = (e, idx) => {
|
|
switch (e.key) {
|
|
case 'ArrowRight':
|
|
case 'ArrowDown':
|
|
e.preventDefault();
|
|
moveTo(idx, 1);
|
|
break;
|
|
case 'ArrowLeft':
|
|
case 'ArrowUp':
|
|
e.preventDefault();
|
|
moveTo(idx, -1);
|
|
break;
|
|
case 'Home':
|
|
e.preventDefault();
|
|
moveTo(-1, 1);
|
|
break;
|
|
case 'End':
|
|
e.preventDefault();
|
|
moveTo(options.length, -1);
|
|
break;
|
|
default:
|
|
}
|
|
};
|
|
|
|
const group = (
|
|
<div
|
|
role='radiogroup'
|
|
aria-label={ariaLabel}
|
|
aria-disabled={groupDisabled || undefined}
|
|
className={`cp-option-group cp-option-group--${variant} ${className}`}
|
|
>
|
|
{options.map((opt, idx) => {
|
|
const key = optionKey(opt);
|
|
const selected = key === selectedKey;
|
|
const reason = groupDisabled ? '' : optionDisabledReasons[key] || '';
|
|
const disabled = isDisabled(opt);
|
|
const btn = (
|
|
<button
|
|
key={key}
|
|
ref={(el) => {
|
|
refs.current[idx] = el;
|
|
}}
|
|
type='button'
|
|
role='radio'
|
|
aria-checked={selected}
|
|
aria-disabled={disabled || undefined}
|
|
aria-label={
|
|
getOptionAriaLabel ? getOptionAriaLabel(opt) : undefined
|
|
}
|
|
tabIndex={idx === tabStop ? 0 : -1}
|
|
className={`cp-option${selected ? ' is-selected' : ''}${
|
|
disabled ? ' is-disabled' : ''
|
|
}`}
|
|
onClick={() => select(opt)}
|
|
onKeyDown={(e) => onKeyDown(e, idx)}
|
|
>
|
|
{renderOption ? renderOption(opt, { selected, disabled }) : key}
|
|
</button>
|
|
);
|
|
return reason ? (
|
|
<Tooltip key={key} content={reason} position='top'>
|
|
{btn}
|
|
</Tooltip>
|
|
) : (
|
|
btn
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
|
|
return groupDisabled ? (
|
|
<Tooltip content={disabledReason} position='top'>
|
|
{group}
|
|
</Tooltip>
|
|
) : (
|
|
group
|
|
);
|
|
};
|
|
|
|
export default OptionGroup;
|