Skip to content

Commit

Permalink
feat: improve a11y
Browse files Browse the repository at this point in the history
  • Loading branch information
aojunhao123 committed Nov 17, 2024
1 parent bbfc8bb commit de87141
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import classNames from 'classnames';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import omit from 'rc-util/lib/omit';
import pickAttrs from 'rc-util/lib/pickAttrs';
import * as React from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';

Expand Down Expand Up @@ -27,6 +29,8 @@ export interface CheckboxProps
onChange?: (e: CheckboxChangeEvent) => void;
}

const DEFAULT_ARIA_LABEL = 'checkbox';

export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
const {
prefixCls = 'rc-checkbox',
Expand All @@ -41,6 +45,16 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
...inputProps
} = props;

const a11yProps = {
...pickAttrs(inputProps, true),
'aria-label': inputProps['aria-label'] || DEFAULT_ARIA_LABEL,
} as React.AriaAttributes;

const restProps = omit<React.HTMLAttributes<HTMLDivElement>, keyof React.AriaAttributes>(
inputProps,
Object.keys(a11yProps) as Array<keyof React.AriaAttributes>,
);

const inputRef = useRef<HTMLInputElement>(null);
const holderRef = useRef<HTMLElement>(null);

Expand Down Expand Up @@ -92,7 +106,8 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
return (
<span className={classString} title={title} style={style} ref={holderRef}>
<input
{...inputProps}
{...restProps}
{...a11yProps}
className={`${prefixCls}-input`}
ref={inputRef}
onChange={handleChange}
Expand Down

0 comments on commit de87141

Please sign in to comment.