三、核心 Hooks 详解
React Aria 提供了 50+ 个 Hooks,覆盖了从按钮、输入框到复杂的表格、拖拽、虚拟滚动等场景。
3.1 useButton - 按钮
useButton 使任何元素(如 div、span)都能作为可访问的按钮。它会自动处理键盘事件(Enter、Space)和 ARIA 角色。
import { useButton } from 'react-aria';
import { useRef } from 'react';
function CustomButton({ children, onPress }) {
let ref = useRef(null);
let { buttonProps } = useButton({ onPress, elementType: 'div' }, ref);
return (
<div
{...buttonProps}
ref={ref}
style={
{
display: 'inline-block',
padding: '10px 20px',
background: '#007bff',
color: 'white',
borderRadius: '8px',
cursor: 'pointer',
userSelect: 'none'
}}
>
{children}
</div>
);
}
3.2 usePress - 按压事件
usePress 处理点击、触摸、键盘 Enter/Space 等多种触发方式,返回统一的按压交互状态。它比原生的 onClick 更加强大,支持长按、悬停、禁用等状态。
import { usePress } from 'react-aria';
function PressButton({ children, onPress }) {
let { pressProps, isPressed } = usePress({
onPress,
onPressStart: () => console.log('开始按压'),
onPressEnd: () => console.log('结束按压')
});
return (
<div
{...pressProps}
style={
{
padding: '10px 20px',
background: isPressed ? '#0056b3' : '#007bff',
color: 'white',
borderRadius: '8px',
cursor: 'pointer',
transition: 'background 0.1s'
}}
>
{children}
</div>
);
}
// 使用示例
<PressButton onPress={() => alert('按压触发!')}>
按压触发
</PressButton>
3.3 useFocusRing - 焦点环
为元素添加可访问的焦点指示器,仅在键盘焦点时(而非鼠标点击时)显示。这对于键盘导航用户至关重要。
import { useFocusRing } from 'react-aria';
function FocusInput() {
let { focusProps, isFocusVisible } = useFocusRing();
return (
<input
{...focusProps}
placeholder="输入内容"
style={
{
padding: '10px',
border: '2px solid #ccc',
borderRadius: '4px',
outline: isFocusVisible ? '2px solid #007bff' : 'none',
outlineOffset: '2px'
}}
/>
);
}
3.4 useTextField - 文本输入框
useTextField 为文本输入框提供完整的可访问性支持,包括标签关联、描述、错误消息等。
import { useTextField } from 'react-aria';
import { useRef } from 'react';
function TextField(props) {
let { label, description, errorMessage, ...otherProps } = props;
let ref = useRef(null);
let { labelProps, inputProps, descriptionProps, errorMessageProps } = useTextField(
{ ...otherProps, label, description, errorMessage },
ref
);
return (
<div style={
{ display: 'flex', flexDirection: 'column', gap: '4px' }}>
<label {...labelProps} style={
{ fontWeight: 'bold' }}>{label}</label>
<input {...inputProps} ref={ref} style={
{ padding: '8px', border: '1px solid #ccc', borderRadius: '4px' }} />
{description && <div {...descriptionProps} style={
{ fontSize: '12px', color: '#666' }}>{description}</div>}
{errorMessage && <div {...errorMessageProps} style={
{ fontSize: '12px', color: 'red' }}>{errorMessage}</div>}
</div>
);
}
// 使用示例
<TextField
label="邮箱"
description="请输入您的邮箱地址"
errorMessage="邮箱格式不正确"
/>
3.5 useCheckbox - 复选框
http://oieaw.cn
useCheckbox 可让你创建完全可访问的复选框,支持 indeterminate 状态。它返回 inputProps 和 isSelected、isIndeterminate 等状态。
import { useCheckbox } from 'react-aria';
import { useToggleState } from 'react-stately';
function Checkbox({ children, ...props }) {
let state = useToggleState(props);
let ref = useRef(null);
let { inputProps } = useCheckbox(props, state, ref);
return (
<label style={
{ display: 'flex', alignItems: 'center', gap: '8px', cursor: 'pointer' }}>
<input {...inputProps} ref={ref} />
<span>{children}</span>
</label>
);
}
isSelected:复选框是否被选中
isIndeterminate:是否处于半选状态(需要手动设置,通常用于树形控件)
3.6 useSwitch - 开关
useSwitch 用于创建开关组件,语义上等同于复选框,但视觉样式不同。返回 inputProps 和 isSelected 状态。
import { useSwitch } from 'react-aria';
import { useToggleState } from 'react-stately';
function Switch({ children, ...props }) {
let state = useToggleState(props);
let ref = useRef(null);
let { inputProps } = useSwitch(props, state, ref);
return (
<label style={
{ display: 'flex', alignItems: 'center', gap: '12px', cursor: 'pointer' }}>
<input {...inputProps} ref={ref} />
<div style={
{
width: '44px',
height: '24px',
background: state.isSelected ? '#007bff' : '#ccc',
borderRadius: '12px',
position: 'relative',
transition: 'background 0.2s'
}}>
<div style={
{
width: '20px',
height: '20px',
background: 'white',
borderRadius: '50%',
position: 'absolute',
top: '2px',
left: state.isSelected ? '22px' : '2px',
transition: 'left 0.2s'
}} />
</div>
<span>{children}</span>
</label>
);
}
3.7 useSelect - 下拉选择
useSelect 提供完整的组合框(下拉选择)的可访问性,包括选项列表、展开/折叠、键盘导航等。
import { useSelect } from 'react-aria';
import { useSelectState } from 'react-stately';
function Dropdown(props) {
let state = useSelectState(props);
let ref = useRef(null);
let { labelProps, triggerProps, valueProps, menuProps } = useSelect(props, state, ref);
return (
<div style={
{ position: 'relative' }}>
<div {...labelProps}>{props.label}</div>
<button {...triggerProps} ref={ref} style={
{ padding: '8px 12px', border: '1px solid #ccc', borderRadius: '4px', background: 'white' }}>
<span {...valueProps}>{state.selectedItem ? state.selectedItem.rendered : '请选择'}</span>
<span aria-hidden="true">▼</span>
</button>
{state.isOpen && (
<ul {...menuProps} style={
{
position: 'absolute',
top: '100%',
left: 0,
right: 0,
margin: 0,
padding: '8px 0',
listStyle: 'none',
background: 'white',
border: '1px solid #ccc',
borderRadius: '4px',
boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
zIndex: 1
}}>
{[...state.collection].map(item => (
<li
key={item.key}
onClick={() => state.selectionManager.select(item.key)}
style={
{
padding: '8px 12px',
cursor: 'pointer',
background: state.selectionManager.isSelected(item.key) ? '#f0f0f0' : 'transparent'
}}
>
{item.rendered}
</li>
))}
</ul>
)}
</div>
);
}
3.8 useNumberField - 数字输入框
useNumberField 用于创建可访问的数字输入组件,支持步进按钮、格式化、数值限制等功能。
import { useNumberField } from 'react-aria';
import { useNumberFieldState } from 'react-stately';
function NumberInput(props) {
let state = useNumberFieldState(props);
let ref = useRef(null);
let { labelProps, inputProps, incrementButtonProps, decrementButtonProps } = useNumberField(props, state, ref);
return (
<div>
<label {...labelProps}>{props.label}</label>
<div style={
{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<button {...decrementButtonProps}>-</button>
<input {...inputProps} ref={ref} style={
{ width: '80px', textAlign: 'center' }} />
<button {...incrementButtonProps}>+</button>
</div>
</div>
);
}