Skip to content

Commit 8799ef6

Browse files
committed
refactor: improve accessibility attributes and refactor props
1 parent 9f035a1 commit 8799ef6

File tree

11 files changed

+260
-286
lines changed

11 files changed

+260
-286
lines changed

packages/react-ui/src/components/checkbox/checkbox-group-context.ts

+5-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ChangeEvent } from "react";
22
import { createSafeContext } from "../../primitives";
33
import type { BinarySize, ToggleColor } from "../../shared/styles";
4+
import type { FormFieldProps } from "../../shared/types";
45

5-
export type CheckboxBaseProps = {
6+
export type CheckboxBaseProps = FormFieldProps & {
67
/**
78
* 颜色
89
* @default "primary"
@@ -14,37 +15,14 @@ export type CheckboxBaseProps = {
1415
* @default "md"
1516
*/
1617
size?: BinarySize;
17-
18-
/**
19-
* 是否禁用
20-
* @default false
21-
*/
22-
disabled?: boolean;
23-
24-
/**
25-
* 是否必选
26-
* @default false
27-
*/
28-
required?: boolean;
29-
30-
/**
31-
* 是否只读
32-
* @default false
33-
*/
34-
readOnly?: boolean;
3518
};
3619

37-
export type CheckboxGroupBaseProps = CheckboxBaseProps & {
38-
/**
39-
* 多选中输入字段的名称
40-
*/
41-
name?: string;
42-
20+
export type CheckboxGroupBaseProps = {
4321
/**
44-
* 选中的值
22+
* 可控值
4523
*/
4624
value?: (string | number)[];
47-
};
25+
} & CheckboxBaseProps;
4826

4927
export type CheckboxGroupContextValue = CheckboxGroupBaseProps & {
5028
onChange: (event: ChangeEvent<HTMLInputElement> | string | number) => void;

packages/react-ui/src/components/checkbox/checkbox-group.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { PrimitiveProps } from "../../primitives";
44
import { isInputEvent } from "../../utils";
55
import { type CheckboxGroupBaseProps, CheckboxGroupContext } from "./checkbox-group-context";
66

7-
export type CheckboxGroupProps = CheckboxGroupBaseProps & {
7+
export type CheckboxGroupProps = {
88
/**
99
* 默认值
1010
*/
@@ -14,7 +14,7 @@ export type CheckboxGroupProps = CheckboxGroupBaseProps & {
1414
* onChange 回调
1515
*/
1616
onChange?: (value: (string | number)[]) => void;
17-
};
17+
} & CheckboxGroupBaseProps;
1818

1919
export const CheckboxGroup = (props: PrimitiveProps<"div", CheckboxGroupProps, "role">) => {
2020
const {

packages/react-ui/src/components/checkbox/checkbox.tsx

+30-42
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,41 @@ import {
99
toggleControlShareStyles,
1010
toggleLabelShareStyles,
1111
} from "../../shared/styles";
12+
import type { CheckedValueProps } from "../../shared/types";
1213
import { ariaAttr, tx } from "../../utils";
1314
import { type CheckboxBaseProps, useCheckboxGroup } from "./checkbox-group-context";
1415

15-
export type CheckboxProps = CheckboxBaseProps & {
16-
/**
17-
* 可控值
18-
*/
19-
checked?: boolean;
20-
21-
/**
22-
* 默认值
23-
* @default false
24-
*/
25-
defaultChecked?: boolean;
26-
27-
/**
28-
* onChange 回调
29-
*/
30-
onChange?: (checked: boolean) => void;
31-
32-
/**
33-
* 值
34-
*/
35-
value?: string | number;
36-
37-
/**
38-
* 部分选中
39-
* @default false
40-
*/
41-
indeterminate?: boolean;
42-
43-
/**
44-
* 是否无效
45-
* @default false
46-
*/
47-
invalid?: boolean;
48-
49-
/**
50-
* 间距
51-
* @default "0.5em"
52-
*/
53-
spacing?: string | number;
54-
};
16+
export type CheckboxProps = CheckedValueProps &
17+
CheckboxBaseProps & {
18+
/**
19+
* 是否无效
20+
* @default false
21+
*/
22+
invalid?: boolean;
23+
24+
/**
25+
* 值
26+
*/
27+
value?: string | number;
28+
29+
/**
30+
* 部分选中
31+
* @default false
32+
*/
33+
indeterminate?: boolean;
34+
35+
/**
36+
* 间距
37+
* @default "0.5em"
38+
*/
39+
spacing?: string | number;
40+
};
5541

5642
export const Checkbox = (props: PrimitiveProps<"input", CheckboxProps, "role" | "type">) => {
5743
const group = useCheckboxGroup(true);
5844

5945
const {
60-
name = group?.name,
46+
name,
6147
size = group?.size || "md",
6248
color = group?.color || "primary",
6349
disabled = group?.disabled || false,
@@ -116,6 +102,8 @@ export const Checkbox = (props: PrimitiveProps<"input", CheckboxProps, "role" |
116102
const colorStyle = binaryColorShareStyles[color];
117103
const labelSizeStyle = inputTextShareStyles[size];
118104

105+
const htmlName = group?.name ? `${group.name}[]` : name;
106+
119107
return (
120108
<label
121109
style={
@@ -128,7 +116,7 @@ export const Checkbox = (props: PrimitiveProps<"input", CheckboxProps, "role" |
128116
>
129117
<input
130118
ref={refs}
131-
name={name}
119+
name={htmlName}
132120
className={"peer sr-only"}
133121
value={value}
134122
type="checkbox"

packages/react-ui/src/components/input/input.tsx

+64-98
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,74 @@
1-
import { type ChangeEvent, type CSSProperties, type HTMLInputTypeAttribute, type ReactNode, useRef } from "react";
1+
import { type ChangeEvent, type CSSProperties, type ReactNode, useRef } from "react";
22
import { useControllableState, useMergeRefs } from "../../hooks";
33
import type { PrimitiveProps } from "../../primitives";
44
import { inputTextShareStyles } from "../../shared/styles";
5+
import type { FormFieldProps } from "../../shared/types";
56
import { tx } from "../../utils";
67
import { InputAffix } from "./input-affix";
78
import { type InputGroupContextValue, useInputGroup } from "./input-group-context";
89
import { inputAffixDefaultSizes, inputGroupStyles, inputSizeStyles } from "./input.styles";
910

10-
export type InputProps = Partial<InputGroupContextValue> & {
11-
/**
12-
* 可控值
13-
*/
14-
value?: string | number;
15-
16-
/**
17-
* 默认值
18-
* @default ""
19-
*/
20-
defaultValue?: string | number;
21-
22-
/**
23-
* onChange 回调
24-
*/
25-
onChange?: (value: string | number) => void;
26-
27-
/**
28-
* 是否禁用
29-
* @default false
30-
*/
31-
disabled?: boolean;
32-
33-
/**
34-
* 是否必须
35-
* @default false
36-
*/
37-
required?: boolean;
38-
39-
/**
40-
* 是否只读
41-
* @default false
42-
*/
43-
readOnly?: boolean;
44-
45-
/**
46-
* 是否无效
47-
* @default false
48-
*/
49-
invalid?: boolean;
50-
51-
/**
52-
* 是否全宽度
53-
* @default false
54-
*/
55-
fullWidth?: boolean;
56-
57-
/**
58-
* 占位符文本
59-
*/
60-
placeholder?: string;
61-
62-
/**
63-
* 前置元素
64-
*/
65-
prefix?: ReactNode;
66-
67-
/**
68-
* 前置元素宽度
69-
*/
70-
prefixWidth?: number;
71-
72-
/**
73-
* 后置元素
74-
*/
75-
suffix?: ReactNode;
76-
77-
/**
78-
* 后置元素宽度
79-
*/
80-
suffixWidth?: number;
81-
82-
/**
83-
* 输入框类型
84-
* @ignore
85-
* @default "text"
86-
*/
87-
type?: Omit<
88-
HTMLInputTypeAttribute,
89-
| "button"
90-
| "submit"
91-
| "reset"
92-
| "checkbox"
93-
| "radio"
94-
| "range"
95-
| "color"
96-
| "date"
97-
| "datetime-local"
98-
| "month"
99-
| "time"
100-
| "week"
101-
| "hidden"
102-
| "file"
103-
| "image"
104-
>;
105-
};
11+
export type InputProps = Partial<InputGroupContextValue> &
12+
FormFieldProps & {
13+
/**
14+
* 可控值
15+
*/
16+
value?: string | number;
17+
18+
/**
19+
* 默认值
20+
* @default ""
21+
*/
22+
defaultValue?: string | number;
23+
24+
/**
25+
* onChange 回调
26+
*/
27+
onChange?: (value: string | number) => void;
28+
29+
/**
30+
* 是否无效
31+
* @default false
32+
*/
33+
invalid?: boolean;
34+
35+
/**
36+
* 占位符文本
37+
*/
38+
placeholder?: string;
39+
40+
/**
41+
* 是否全宽度
42+
* @default false
43+
*/
44+
fullWidth?: boolean;
45+
46+
/**
47+
* 前置元素
48+
*/
49+
prefix?: ReactNode;
50+
51+
/**
52+
* 前置元素宽度
53+
*/
54+
prefixWidth?: number;
55+
56+
/**
57+
* 后置元素
58+
*/
59+
suffix?: ReactNode;
60+
61+
/**
62+
* 后置元素宽度
63+
*/
64+
suffixWidth?: number;
65+
66+
/**
67+
* 输入框类型
68+
* @default "text"
69+
*/
70+
type?: "text" | "email" | "number" | "password" | "search" | "tel" | "url";
71+
};
10672

10773
export const Input = (props: PrimitiveProps<"input", InputProps, "children">) => {
10874
const group = useInputGroup(true);
@@ -185,7 +151,7 @@ export const Input = (props: PrimitiveProps<"input", InputProps, "children">) =>
185151
required={required}
186152
disabled={disabled}
187153
readOnly={readOnly}
188-
type={type as HTMLInputTypeAttribute}
154+
type={type}
189155
inputMode={inputMode}
190156
value={valueState}
191157
onChange={handleChange}

packages/react-ui/src/components/number-input/number-input.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { PrimitiveProps } from "../../primitives";
55
import { Input, type InputProps } from "../input/input";
66
import { NumberInputControl } from "./number-input-control";
77

8-
export type NumberInputProps = Omit<InputProps, "suffix" | "suffixWidth" | "onChange"> & {
8+
export type NumberInputProps = Omit<
9+
InputProps,
10+
"type" | "suffix" | "suffixWidth" | "value" | "defaultValue" | "onChange"
11+
> & {
912
/**
1013
* 可控值
1114
*/
@@ -20,7 +23,6 @@ export type NumberInputProps = Omit<InputProps, "suffix" | "suffixWidth" | "onCh
2023
* onChange 回调
2124
*/
2225
onChange?: (value: number | undefined) => void;
23-
2426
/**
2527
* 最小值
2628
* @default Number.MIN_SAFE_INTEGER

packages/react-ui/src/components/radio/radio-group-context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type RadioBaseProps = {
2323

2424
export type RadioGroupBaseProps = RadioBaseProps & {
2525
/**
26-
* 单选中输入字段的名称
26+
* 字段的名称
2727
*/
2828
name?: string;
2929

packages/react-ui/src/components/radio/radio-group.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ export const RadioGroup = (props: PrimitiveProps<"div", RadioGroupProps, "role">
6666
};
6767

6868
return (
69-
<div role={"radiogroup"} aria-invalid={ariaAttr(invalid)} aria-orientation={orientation} {...rest}>
69+
<div
70+
role={"radiogroup"}
71+
aria-disabled={ariaAttr(disabled)}
72+
aria-required={ariaAttr(required)}
73+
aria-readonly={ariaAttr(readOnly)}
74+
aria-invalid={ariaAttr(invalid)}
75+
aria-orientation={orientation}
76+
{...rest}
77+
>
7078
<RadioGroupContext value={groupContext}>{children}</RadioGroupContext>
7179
</div>
7280
);

0 commit comments

Comments
 (0)