Skip to content

Commit d3de52f

Browse files
authored
feat: improve a11y (#292)
* feat: improve a11y * feat: improve a11y * feat: improve a11y * fix: label fix
1 parent 818d219 commit d3de52f

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/index.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
3838
type = 'checkbox',
3939
title,
4040
onChange,
41+
children,
4142
...inputProps
4243
} = props;
4344

4445
const inputRef = useRef<HTMLInputElement>(null);
45-
const holderRef = useRef<HTMLElement>(null);
46+
const holderRef = useRef<HTMLLabelElement>(null);
4647

4748
const [rawValue, setRawValue] = useMergedState(defaultChecked, {
4849
value: checked,
@@ -90,7 +91,7 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
9091
};
9192

9293
return (
93-
<span className={classString} title={title} style={style} ref={holderRef}>
94+
<label className={classString} title={title} style={style} ref={holderRef}>
9495
<input
9596
{...inputProps}
9697
className={`${prefixCls}-input`}
@@ -101,7 +102,8 @@ export const Checkbox = forwardRef<CheckboxRef, CheckboxProps>((props, ref) => {
101102
type={type}
102103
/>
103104
<span className={`${prefixCls}-inner`} />
104-
</span>
105+
{children !== undefined && <span>{children}</span>}
106+
</label>
105107
);
106108
});
107109

tests/__snapshots__/index.test.tsx.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`rc-checkbox click checkbox 1`] = `
4-
<span
4+
<label
55
class="rc-checkbox"
66
>
77
<input
@@ -11,11 +11,11 @@ exports[`rc-checkbox click checkbox 1`] = `
1111
<span
1212
class="rc-checkbox-inner"
1313
/>
14-
</span>
14+
</label>
1515
`;
1616

1717
exports[`rc-checkbox click radio 1`] = `
18-
<span
18+
<label
1919
class="rc-checkbox rc-checkbox-checked"
2020
>
2121
<input
@@ -25,11 +25,11 @@ exports[`rc-checkbox click radio 1`] = `
2525
<span
2626
class="rc-checkbox-inner"
2727
/>
28-
</span>
28+
</label>
2929
`;
3030

3131
exports[`rc-checkbox control mode 1`] = `
32-
<span
32+
<label
3333
class="rc-checkbox rc-checkbox-checked"
3434
>
3535
<input
@@ -40,11 +40,11 @@ exports[`rc-checkbox control mode 1`] = `
4040
<span
4141
class="rc-checkbox-inner"
4242
/>
43-
</span>
43+
</label>
4444
`;
4545

4646
exports[`rc-checkbox works 1`] = `
47-
<span
47+
<label
4848
class="rc-checkbox"
4949
>
5050
<input
@@ -54,5 +54,5 @@ exports[`rc-checkbox works 1`] = `
5454
<span
5555
class="rc-checkbox-inner"
5656
/>
57-
</span>
57+
</label>
5858
`;

0 commit comments

Comments
 (0)