Skip to content

Commit ae7a524

Browse files
committed
feat: improve RadioGroup and CheckboxGroup layout flexibility
1 parent 7e77b07 commit ae7a524

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ChangeEvent } from "react";
22
import { useControllableState } from "../../hooks";
33
import type { PrimitiveProps } from "../../primitives";
4-
import { isInputEvent } from "../../utils";
4+
import type { Orientation } from "../../shared/types";
5+
import { isInputEvent, tx } from "../../utils";
56
import { type CheckboxGroupBaseProps, CheckboxGroupContext } from "./checkbox-group-context";
67

78
export type CheckboxGroupProps = {
@@ -14,6 +15,12 @@ export type CheckboxGroupProps = {
1415
* onChange 回调
1516
*/
1617
onChange?: (value: (string | number)[]) => void;
18+
19+
/**
20+
* 方向
21+
* @default 'horizontal'
22+
*/
23+
orientation?: Orientation;
1724
} & CheckboxGroupBaseProps;
1825

1926
export const CheckboxGroup = (props: PrimitiveProps<"div", CheckboxGroupProps, "role">) => {
@@ -25,6 +32,8 @@ export const CheckboxGroup = (props: PrimitiveProps<"div", CheckboxGroupProps, "
2532
value,
2633
defaultValue = [],
2734
onChange,
35+
orientation = "horizontal",
36+
className,
2837
children,
2938
...rest
3039
} = props;
@@ -63,7 +72,11 @@ export const CheckboxGroup = (props: PrimitiveProps<"div", CheckboxGroupProps, "
6372
};
6473

6574
return (
66-
<div role={"group"} {...rest}>
75+
<div
76+
role={"group"}
77+
className={tx("inline-flex", orientation == "horizontal" ? "flex-row" : "flex-col", className)}
78+
{...rest}
79+
>
6780
<CheckboxGroupContext value={context}>{children}</CheckboxGroupContext>
6881
</div>
6982
);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useControllableState } from "../../hooks";
22
import type { PrimitiveProps } from "../../primitives";
33
import type { Orientation } from "../../shared/types";
4-
import { ariaAttr } from "../../utils";
4+
import { ariaAttr, tx } from "../../utils";
55
import { type RadioGroupBaseProps, RadioGroupContext } from "./radio-group-context";
66

77
export type RadioGroupProps = RadioGroupBaseProps & {
@@ -35,6 +35,7 @@ export const RadioGroup = (props: PrimitiveProps<"div", RadioGroupProps, "role">
3535
value,
3636
defaultValue = "",
3737
onChange,
38+
className,
3839
children,
3940
...rest
4041
} = props;
@@ -73,6 +74,7 @@ export const RadioGroup = (props: PrimitiveProps<"div", RadioGroupProps, "role">
7374
aria-readonly={ariaAttr(readOnly)}
7475
aria-invalid={ariaAttr(invalid)}
7576
aria-orientation={orientation}
77+
className={tx("inline-flex", orientation == "horizontal" ? "flex-row" : "flex-col", className)}
7678
{...rest}
7779
>
7880
<RadioGroupContext value={groupContext}>{children}</RadioGroupContext>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const Radio = (
9191
checked ? ["text-fg-emphasized", colorStyle.checked] : "bg-bg-normal",
9292
sizeStyle,
9393
checked &&
94-
"before:relative before:inline-block before:h-1/2 before:w-1/2 before:rounded-[50%] before:bg-current before:content-['']",
94+
"before:h-5/9 before:w-5/9 before:relative before:inline-block before:rounded-full before:bg-current before:content-['']",
9595
)}
9696
/>
9797
{children && <div className={tx("select-none", labelSizeStyle)}>{children}</div>}

website/src/routes/docs/_mdx/components/checkbox.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default function App() {
7676
return (
7777
<div className={"flex flex-col gap-2"}>
7878
<p>你选择了框架: {value.join(", ")}</p>
79-
<CheckboxGroup value={value} onChange={setValue} className={"flex flex-row gap-5"}>
79+
<CheckboxGroup value={value} onChange={setValue} className={"gap-5"}>
8080
<Checkbox value={"React"}>React</Checkbox>
8181
<Checkbox value={"Angular"}>Angular</Checkbox>
8282
<Checkbox value={"Vue"}>Vue</Checkbox>
@@ -111,7 +111,7 @@ export default function App() {
111111
>
112112
工作日
113113
</Checkbox>
114-
<div className={"flex flex-row gap-5"}>
114+
<div className={"inline-flex flex-row gap-5"}>
115115
{["周一", "周二", "周三", "周四", "周五"].map((day, index) => (
116116
<Checkbox
117117
key={day}

website/src/routes/docs/_mdx/components/radio-group.mdx

+23-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Radio, RadioGroup } from "@resolid/react-ui";
1010

1111
export default function App() {
1212
return (
13-
<RadioGroup name={"frameworksRadioGroup"} className={"flex flex-row items-center gap-5"}>
13+
<RadioGroup name={"frameworksRadioGroup"} className={"gap-5"}>
1414
<Radio value={"React"}>React</Radio>
1515
<Radio value={"Angular"}>Angular</Radio>
1616
<Radio value={"Vue"}>Vue</Radio>
@@ -49,11 +49,7 @@ import { Radio, RadioGroup } from "@resolid/react-ui";
4949

5050
export default function App() {
5151
return (
52-
<RadioGroup
53-
name={"frameworksRadioGroup"}
54-
defaultValue={"React"}
55-
className={"flex flex-row items-center gap-5"}
56-
>
52+
<RadioGroup name={"frameworksRadioGroup"} defaultValue={"React"} className={"gap-5"}>
5753
<Radio value={"React"}>React</Radio>
5854
<Radio value={"Angular"}>Angular</Radio>
5955
<Radio value={"Vue"}>Vue</Radio>
@@ -82,7 +78,7 @@ export default function App() {
8278
name={"frameworksRadioGroup"}
8379
value={value}
8480
onChange={setValue}
85-
className={"flex flex-row items-center gap-5"}
81+
className={"gap-5"}
8682
>
8783
<Radio value={"React"}>React</Radio>
8884
<Radio value={"Angular"}>Angular</Radio>
@@ -95,6 +91,26 @@ export default function App() {
9591
}
9692
```
9793

94+
### 垂直方向
95+
96+
使用 `defaultValue` 属性来控制默认选定的单选项目
97+
98+
```jsx demo
99+
import { Radio, RadioGroup } from "@resolid/react-ui";
100+
101+
export default function App() {
102+
return (
103+
<RadioGroup name={"frameworksRadioGroup"} orientation={"vertical"} className={"gap-3"}>
104+
<Radio value={"React"}>React</Radio>
105+
<Radio value={"Angular"}>Angular</Radio>
106+
<Radio value={"Vue"}>Vue</Radio>
107+
<Radio value={"SolidJS"}>SolidJS</Radio>
108+
<Radio value={"Svelte"}>Svelte</Radio>
109+
</RadioGroup>
110+
);
111+
}
112+
```
113+
98114
## 属性
99115

100116
### RadioGroup

0 commit comments

Comments
 (0)