-
- {useOptions
- ? options.map((v: any, index) => {
- switch (typeof v) {
- case 'string':
- return (
-
- {v}
-
- );
- case 'number': {
- return (
-
- {String(v)}
-
- );
- }
- case 'object': {
- const vs = v as CheckboxOptionObj;
- // CheckAll 的 checkBox 不存在 value,故用 checkAll_index 来保证尽量不和用户的 value 冲突.
- return vs.checkAll ? (
-
- ) : (
-
- );
- }
- default:
- return null;
- }
- })
- : children}
-
+
+ {useOptions ? renderOptions() : children}
);
};
diff --git a/packages/components/checkbox/_example/button.tsx b/packages/components/checkbox/_example/button.tsx
new file mode 100644
index 0000000000..2255f9fd5c
--- /dev/null
+++ b/packages/components/checkbox/_example/button.tsx
@@ -0,0 +1,102 @@
+import React, { useState } from 'react';
+import { Checkbox, Space } from 'tdesign-react';
+
+export default function CheckboxButtonExample() {
+ const [outlineValue, setOutlineValue] = useState
(['bj', 'gz']);
+ const [filledDefaultValue, setFilledDefaultValue] = useState(['bj', 'sh']);
+ const [filledPrimaryValue, setFilledPrimaryValue] = useState(['gz']);
+ const [verticalValue, setVerticalValue] = useState(['bj', 'sz']);
+
+ return (
+
+ {/* 场景 1:边框型多选按钮 */}
+
+
边框型多选按钮
+
theme="button" variant="outline" value={outlineValue} onChange={setOutlineValue}>
+ 北京
+ 上海
+ 广州
+ 深圳
+
+
已选: {outlineValue.join('、') || '无'}
+
+
+ {/* 场景 2:填充型 - 默认/白色高亮 */}
+
+
填充型多选按钮 - 默认高亮(白色卡片)
+
+ theme="button"
+ variant="default-filled"
+ value={filledDefaultValue}
+ onChange={setFilledDefaultValue}
+ >
+ 北京
+ 上海
+ 广州
+ 深圳
+
+
已选: {filledDefaultValue.join('、') || '无'}
+
+
+ {/* 场景 3:填充型 - 主题色高亮 */}
+
+
填充型多选按钮 - 主题色高亮(蓝色)
+
+ theme="button"
+ variant="primary-filled"
+ value={filledPrimaryValue}
+ onChange={setFilledPrimaryValue}
+ >
+ 北京
+ 上海
+ 广州
+ 深圳
+
+
已选: {filledPrimaryValue.join('、') || '无'}
+
+
+ {/* 场景 4:纵向排列 */}
+
+
纵向排列(卡片式)
+
+ theme="button"
+ variant="primary-filled"
+ direction="vertical"
+ value={verticalValue}
+ onChange={setVerticalValue}
+ >
+ 北京
+ 上海
+ 广州
+ 深圳
+
+
已选: {verticalValue.join('、') || '无'}
+
+
+ {/* 附加:禁用状态展示 */}
+
+
禁用状态
+
+ theme="button" variant="outline" defaultValue={['bj']}>
+
+ 选中禁用
+
+ 上海
+
+ theme="button" variant="default-filled" defaultValue={['gz']}>
+
+ 选中禁用
+
+ 深圳
+
+ theme="button" variant="primary-filled" defaultValue={['sh']}>
+
+ 选中禁用
+
+ 北京
+
+
+
+
+ );
+}
diff --git a/packages/components/checkbox/checkbox.en-US.md b/packages/components/checkbox/checkbox.en-US.md
index b8aac877fa..462108fbe5 100644
--- a/packages/components/checkbox/checkbox.en-US.md
+++ b/packages/components/checkbox/checkbox.en-US.md
@@ -1,5 +1,9 @@
:: BASE_DOC ::
+### Button Style
+
+{{ button }}
+
## API
### Checkbox Props
@@ -27,11 +31,15 @@ name | type | default | description | required
-- | -- | -- | -- | --
className | String | - | className of component | N
style | Object | - | CSS(Cascading Style Sheets),Typescript: `React.CSSProperties` | N
+direction | String | horizontal | arrangement of checkbox buttons. Options: horizontal/vertical | N
disabled | Boolean | - | \- | N
max | Number | undefined | \- | N
name | String | - | \- | N
options | Array | - | Typescript:`Array` `type CheckboxOption = string \| number \| CheckboxOptionObj` `interface CheckboxOptionObj { label?: string \| TNode; value?: string \| number; disabled?: boolean; name?: string; checkAll?: true }`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/common.ts)。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
readOnly | Boolean | undefined | \- | N
+size | String | medium | component size, only valid when theme is button. Options: small/medium/large. Typescript: `SizeEnum`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/common.ts) | N
+theme | String | checkbox | style of checkbox group when rendering with options. Options: checkbox/button | N
value | Array | [] | Typescript:`T` `type CheckboxGroupValue = Array`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
defaultValue | Array | [] | uncontrolled property。Typescript:`T` `type CheckboxGroupValue = Array`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
+variant | String | outline | button style variant. Options: outline/primary-filled/default-filled | N
onChange | Function | | Typescript:`(value: T, context: CheckboxGroupChangeContext) => void`
[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts)。
`interface CheckboxGroupChangeContext { e: ChangeEvent; current: CheckboxOption \| TdCheckboxProps; type: 'check' \| 'uncheck' }`
| N
diff --git a/packages/components/checkbox/checkbox.md b/packages/components/checkbox/checkbox.md
index e8a1d80f7d..6c37ac06cb 100644
--- a/packages/components/checkbox/checkbox.md
+++ b/packages/components/checkbox/checkbox.md
@@ -1,6 +1,10 @@
:: BASE_DOC ::
+### 按钮风格
+
+{{ button }}
+
### 最多选中的数量
{{ max }}
@@ -32,11 +36,15 @@ onClick | Function | | TS 类型:`(context: { e: MouseEvent }) => void`
-- | -- | -- | -- | --
className | String | - | 类名 | N
style | Object | - | 样式,TS 类型:`React.CSSProperties` | N
+direction | String | horizontal | 多选框按钮排列方式。可选项:horizontal/vertical | N
disabled | Boolean | - | 是否禁用组件,默认为 false。CheckboxGroup.disabled 优先级低于 Checkbox.disabled | N
max | Number | undefined | 支持最多选中的数量 | N
name | String | - | 统一设置内部复选框 HTML 属性 | N
options | Array | - | 以配置形式设置子元素。示例1:`['北京', '上海']` ,示例2: `[{ label: '全选', checkAll: true }, { label: '上海', value: 'shanghai' }]`。checkAll 值为 true 表示当前选项为「全选选项」。TS 类型:`Array` `type CheckboxOption = string \| number \| CheckboxOptionObj` `interface CheckboxOptionObj { label?: string \| TNode; value?: string \| number; disabled?: boolean; name?: string; checkAll?: true }`。[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/common.ts)。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
readOnly | Boolean | undefined | 只读状态 | N
+size | String | medium | 组件尺寸,仅在 theme 为 button 生效。可选项:small/medium/large。TS 类型:`SizeEnum`。[通用类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/common.ts) | N
+theme | String | checkbox | 用于在使用 options 方式渲染时决定组件的风格。可选项:checkbox/button | N
value | Array | [] | 选中值。TS 类型:`T` `type CheckboxGroupValue = Array`。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
defaultValue | Array | [] | 选中值。非受控属性。TS 类型:`T` `type CheckboxGroupValue = Array`。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts) | N
+variant | String | outline | 多选组件按钮形式。可选项:outline/primary-filled/default-filled | N
onChange | Function | | TS 类型:`(value: T, context: CheckboxGroupChangeContext) => void`
值变化时触发,`context.current` 表示当前变化的数据值,如果是全选则为空;`context.type` 表示引起选中数据变化的是选中或是取消选中;`context.option` 表示当前变化的数据项。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/packages/components/checkbox/type.ts)。
`interface CheckboxGroupChangeContext { e: ChangeEvent; current: CheckboxOption \| TdCheckboxProps; type: 'check' \| 'uncheck' }`
| N
diff --git a/packages/components/checkbox/defaultProps.ts b/packages/components/checkbox/defaultProps.ts
index 38f90f3812..1e3b03d0e7 100644
--- a/packages/components/checkbox/defaultProps.ts
+++ b/packages/components/checkbox/defaultProps.ts
@@ -13,4 +13,11 @@ export const checkboxDefaultProps: TdCheckboxProps = {
readOnly: false,
};
-export const checkboxGroupDefaultProps: TdCheckboxGroupProps = { max: undefined, defaultValue: [] };
+export const checkboxGroupDefaultProps: TdCheckboxGroupProps = {
+ max: undefined,
+ defaultValue: [],
+ direction: 'horizontal',
+ size: 'medium',
+ theme: 'checkbox',
+ variant: 'outline',
+};
diff --git a/packages/components/checkbox/type.ts b/packages/components/checkbox/type.ts
index b7b0f6a378..95a2cdb843 100644
--- a/packages/components/checkbox/type.ts
+++ b/packages/components/checkbox/type.ts
@@ -4,8 +4,8 @@
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* */
-import { TNode } from '../common';
-import { MouseEvent, ChangeEvent } from 'react';
+import type { TNode, SizeEnum } from "../common";
+import type { MouseEvent, ChangeEvent } from "react";
export interface TdCheckboxProps {
/**
@@ -68,7 +68,10 @@ export interface TdCheckboxProps {
/**
* 值变化时触发
*/
- onChange?: (checked: boolean, context: { e: ChangeEvent }) => void;
+ onChange?: (
+ checked: boolean,
+ context: { e: ChangeEvent },
+ ) => void;
/**
* 点击时触发,一般用于外层阻止冒泡场景
*/
@@ -80,6 +83,11 @@ export interface TdCheckboxGroupProps {
* 是否禁用组件,默认为 false。CheckboxGroup.disabled 优先级低于 Checkbox.disabled
*/
disabled?: boolean;
+ /**
+ * 多选框按钮排列方式
+ * @default horizontal
+ */
+ direction?: "horizontal" | "vertical";
/**
* 支持最多选中的数量
*/
@@ -102,6 +110,16 @@ export interface TdCheckboxGroupProps {
* 只读状态
*/
readOnly?: boolean;
+ /**
+ * 组件尺寸
+ * @default medium
+ */
+ size?: SizeEnum;
+ /**
+ * 用于在使用 options 方式渲染时决定组件的风格
+ * @default checkbox
+ */
+ theme?: "checkbox" | "button";
/**
* 选中值
* @default []
@@ -112,6 +130,11 @@ export interface TdCheckboxGroupProps {
* @default []
*/
defaultValue?: T;
+ /**
+ * 多选组件按钮形式
+ * @default outline
+ */
+ variant?: "outline" | "primary-filled" | "default-filled";
/**
* 值变化时触发,`context.current` 表示当前变化的数据值,如果是全选则为空;`context.type` 表示引起选中数据变化的是选中或是取消选中;`context.option` 表示当前变化的数据项
*/
@@ -139,5 +162,5 @@ export interface CheckboxGroupChangeContext {
e: ChangeEvent;
current: CheckboxOption | TdCheckboxProps;
option: CheckboxOption | TdCheckboxProps;
- type: 'check' | 'uncheck';
+ type: "check" | "uncheck";
}