Skip to content

Commit 6b99c82

Browse files
authored
feat(radio): support readonly (#3431)
* feat(radio): support readonly * docs: api update
1 parent a24ebc8 commit 6b99c82

File tree

9 files changed

+82
-45
lines changed

9 files changed

+82
-45
lines changed

src/radio/_usage/props.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"options": []
1919
},
2020
{
21-
"name": "value",
21+
"name": "readonly",
2222
"type": "Boolean",
2323
"defaultValue": false,
2424
"options": []

src/radio/instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { RadioValue } from './type';
44

55
export type RadioButtonInstance = InstanceType<typeof RadioButton>;
66
export type RadioGroupInstance = InstanceType<typeof RadioGroup> & {
7-
handleRadioChange: (value: RadioValue, context: { e: Event }) => void;
7+
handleRadioChange: (value: RadioValue, context: { e: Event; name?: string }) => void;
88
};

src/radio/props.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ export default {
1111
/** 是否允许取消选中 */
1212
allowUncheck: Boolean,
1313
/** 是否选中 */
14-
checked: Boolean,
14+
checked: {
15+
type: Boolean,
16+
default: undefined,
17+
},
1518
/** 是否选中,非受控属性 */
1619
defaultChecked: Boolean,
1720
/** 单选按钮内容,同 label */
1821
default: {
1922
type: [String, Function] as PropType<TdRadioProps['default']>,
2023
},
21-
/** 是否为禁用态 */
24+
/** 是否为禁用态。如果存在父组件 RadioGroup,默认值由 RadioGroup.disabled 控制。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled */
2225
disabled: {
2326
type: Boolean,
2427
default: undefined,
@@ -32,6 +35,11 @@ export default {
3235
type: String,
3336
default: '',
3437
},
38+
/** 只读状态 */
39+
readonly: {
40+
type: Boolean,
41+
default: undefined,
42+
},
3543
/** 单选按钮的值 */
3644
value: {
3745
type: [String, Number, Boolean] as PropType<TdRadioProps['value']>,

src/radio/radio-button.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default (Vue as VueConstructor<RadioButtonInstance>).extend({
4040
if (radioGroup) {
4141
radioProps.props.checked = $props.value === radioGroup.value;
4242
radioProps.props.disabled = $props.disabled === undefined ? radioGroup.disabled : $props.disabled;
43+
radioProps.props.readonly = $props.readonly === undefined ? radioGroup.readonly : $props.readonly;
4344
radioProps.props.name = radioGroup.name;
4445
}
4546

src/radio/radio-group-props.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { PropType } from 'vue';
1010
export default {
1111
/** 是否允许取消选中 */
1212
allowUncheck: Boolean,
13-
/** 是否禁用全部子单选框 */
13+
/** 是否禁用全部子单选框。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled */
1414
disabled: {
1515
type: Boolean,
1616
default: undefined,
@@ -24,13 +24,10 @@ export default {
2424
options: {
2525
type: Array as PropType<TdRadioGroupProps['options']>,
2626
},
27-
/** 组件风格 */
28-
theme: {
29-
type: String as PropType<TdRadioGroupProps['theme']>,
30-
default: 'radio' as TdRadioGroupProps['theme'],
31-
validator(val: TdRadioGroupProps['theme']): boolean {
32-
return ['radio', 'button'].includes(val);
33-
},
27+
/** 只读状态 */
28+
readonly: {
29+
type: Boolean,
30+
default: undefined,
3431
},
3532
/** 组件尺寸【讨论中】 */
3633
size: {
@@ -41,9 +38,19 @@ export default {
4138
return ['small', 'medium', 'large'].includes(val);
4239
},
4340
},
41+
/** 组件风格 */
42+
theme: {
43+
type: String as PropType<TdRadioGroupProps['theme']>,
44+
default: 'radio' as TdRadioGroupProps['theme'],
45+
validator(val: TdRadioGroupProps['theme']): boolean {
46+
if (!val) return true;
47+
return ['radio', 'button'].includes(val);
48+
},
49+
},
4450
/** 选中的值 */
4551
value: {
4652
type: [String, Number, Boolean] as PropType<TdRadioGroupProps['value']>,
53+
default: undefined,
4754
},
4855
/** 选中的值,非受控属性 */
4956
defaultValue: {
@@ -58,6 +65,6 @@ export default {
5865
return ['outline', 'primary-filled', 'default-filled'].includes(val);
5966
},
6067
},
61-
/** 选中值发生变化时触发 */
68+
/** 选中值发生变化时触发, `context.name` 指 RadioGroup 的 name 属性 */
6269
onChange: Function as PropType<TdRadioGroupProps['onChange']>,
6370
};

src/radio/radio.en-US.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
:: BASE_DOC ::
22

33
## API
4+
45
### Radio Props
56

67
name | type | default | description | required
@@ -12,6 +13,7 @@ default | String / Slot / Function | - | Typescript:`string \| TNode`。[see m
1213
disabled | Boolean | undefined | \- | N
1314
label | String / Slot / Function | - | Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1415
name | String | - | \- | N
16+
readonly | Boolean | undefined | \- | N
1517
value | String / Number / Boolean | undefined | Typescript:`T` | N
1618
onChange | Function | | Typescript:`(checked: boolean, context: { e: Event }) => void`<br/> | N
1719
onClick | Function | | Typescript:`(context: { e: MouseEvent }) => void`<br/>trigger on click | N
@@ -23,23 +25,25 @@ name | params | description
2325
change | `(checked: boolean, context: { e: Event })` | \-
2426
click | `(context: { e: MouseEvent })` | trigger on click
2527

28+
2629
### RadioGroup Props
2730

2831
name | type | default | description | required
2932
-- | -- | -- | -- | --
3033
allowUncheck | Boolean | false | \- | N
3134
disabled | Boolean | undefined | \- | N
3235
name | String | - | \- | N
33-
options | Array | - | Typescript:`Array<RadioOption>` `type RadioOption = string \| number \| RadioOptionObj` `interface RadioOptionObj { label?: string \| TNode; value?: string \| number; disabled?: boolean }`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts)[see more ts definition](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
34-
theme | String | radio | options:radio/button。 | N
35-
size | String | medium | options:small/medium/large。Typescript:`SizeEnum`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
36-
value | String / Number / Boolean | - | `v-model` is supported。Typescript:`T` | N
37-
defaultValue | String / Number / Boolean | - | uncontrolled property。Typescript:`T` | N
38-
variant | String | outline | options:outline/primary-filled/default-filled | N
39-
onChange | Function | | Typescript:`(value: T, context: { e: Event }) => void`<br/> | N
36+
options | Array | - | Typescript:`Array<RadioOption>` `type RadioOption = string \| number \| RadioOptionObj` `interface RadioOptionObj { label?: string \| TNode; value?: string \| number \| boolean; disabled?: boolean }`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts)[see more ts definition](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
37+
readonly | Boolean | undefined | \- | N
38+
size | String | medium | options: small/medium/large。Typescript:`SizeEnum`[see more ts definition](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
39+
theme | String | radio | options: radio/button | N
40+
value | String / Number / Boolean | - | `v-model` is supported。Typescript:`T` `type RadioValue = string \| number \| boolean`[see more ts definition](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
41+
defaultValue | String / Number / Boolean | - | uncontrolled property。Typescript:`T` `type RadioValue = string \| number \| boolean`[see more ts definition](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
42+
variant | String | outline | options: outline/primary-filled/default-filled | N
43+
onChange | Function | | Typescript:`(value: T, context: { e: Event; name?:string }) => void`<br/> | N
4044

4145
### RadioGroup Events
4246

4347
name | params | description
4448
-- | -- | --
45-
change | `(value: T, context: { e: Event })` | \-
49+
change | `(value: T, context: { e: Event; name?:string })` | \-

src/radio/radio.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
:: BASE_DOC ::
22

33
## API
4+
45
### Radio Props
56

6-
名称 | 类型 | 默认值 | 说明 | 必传
7+
名称 | 类型 | 默认值 | 描述 | 必传
78
-- | -- | -- | -- | --
89
allowUncheck | Boolean | false | 是否允许取消选中 | N
910
checked | Boolean | false | 是否选中。支持语法糖 `v-model` | N
1011
defaultChecked | Boolean | false | 是否选中。非受控属性 | N
1112
default | String / Slot / Function | - | 单选按钮内容,同 label。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
12-
disabled | Boolean | undefined | 是否为禁用态 | N
13+
disabled | Boolean | undefined | 是否为禁用态。如果存在父组件 RadioGroup,默认值由 RadioGroup.disabled 控制。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled | N
1314
label | String / Slot / Function | - | 主文案。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
1415
name | String | - | HTML 元素原生属性 | N
16+
readonly | Boolean | undefined | 只读状态 | N
1517
value | String / Number / Boolean | undefined | 单选按钮的值。TS 类型:`T` | N
1618
onChange | Function | | TS 类型:`(checked: boolean, context: { e: Event }) => void`<br/>选中状态变化时触发 | N
1719
onClick | Function | | TS 类型:`(context: { e: MouseEvent }) => void`<br/>点击时触发,一般用于外层阻止冒泡场景 | N
@@ -23,23 +25,25 @@ onClick | Function | | TS 类型:`(context: { e: MouseEvent }) => void`<br/>
2325
change | `(checked: boolean, context: { e: Event })` | 选中状态变化时触发
2426
click | `(context: { e: MouseEvent })` | 点击时触发,一般用于外层阻止冒泡场景
2527

28+
2629
### RadioGroup Props
2730

28-
名称 | 类型 | 默认值 | 说明 | 必传
31+
名称 | 类型 | 默认值 | 描述 | 必传
2932
-- | -- | -- | -- | --
3033
allowUncheck | Boolean | false | 是否允许取消选中 | N
31-
disabled | Boolean | undefined | 是否禁用全部子单选框 | N
34+
disabled | Boolean | undefined | 是否禁用全部子单选框。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled | N
3235
name | String | - | HTML 元素原生属性 | N
33-
options | Array | - | 单选组件按钮形式。RadioOption 数据类型为 string 或 number 时,表示 label 和 value 值相同。TS 类型:`Array<RadioOption>` `type RadioOption = string \| number \| RadioOptionObj` `interface RadioOptionObj { label?: string \| TNode; value?: string \| number; disabled?: boolean }`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts)[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
34-
theme | String | radio | 组件风格,可选项:radio/button。 | N
36+
options | Array | - | 单选组件按钮形式。RadioOption 数据类型为 string 或 number 时,表示 label 和 value 值相同。TS 类型:`Array<RadioOption>` `type RadioOption = string \| number \| RadioOptionObj` `interface RadioOptionObj { label?: string \| TNode; value?: string \| number \| boolean; disabled?: boolean }`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts)[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
37+
readonly | Boolean | undefined | 只读状态 | N
3538
size | String | medium | 组件尺寸【讨论中】。可选项:small/medium/large。TS 类型:`SizeEnum`[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
36-
value | String / Number / Boolean | - | 选中的值。支持语法糖 `v-model`。TS 类型:`T` | N
37-
defaultValue | String / Number / Boolean | - | 选中的值。非受控属性。TS 类型:`T` | N
39+
theme | String | radio | 组件风格。可选项:radio/button | N
40+
value | String / Number / Boolean | - | 选中的值。支持语法糖 `v-model`。TS 类型:`T` `type RadioValue = string \| number \| boolean`[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
41+
defaultValue | String / Number / Boolean | - | 选中的值。非受控属性。TS 类型:`T` `type RadioValue = string \| number \| boolean`[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
3842
variant | String | outline | 单选组件按钮形式。可选项:outline/primary-filled/default-filled | N
39-
onChange | Function | | TS 类型:`(value: T, context: { e: Event }) => void`<br/>选中值发生变化时触发 | N
43+
onChange | Function | | TS 类型:`(value: T, context: { e: Event; name?:string }) => void`<br/>选中值发生变化时触发, `context.name` 指 RadioGroup 的 name 属性 | N
4044

4145
### RadioGroup Events
4246

4347
名称 | 参数 | 描述
4448
-- | -- | --
45-
change | `(value: T, context: { e: Event })` | 选中值发生变化时触发
49+
change | `(value: T, context: { e: Event; name?:string })` | 选中值发生变化时触发, `context.name` 指 RadioGroup 的 name 属性

src/radio/radio.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ export default mixins(Vue as VueConstructor<RadioParentInjectInstance>, classPre
8080
return Boolean((this.formDisabled || this.disabled) ?? this.radioGroup?.disabled);
8181
},
8282

83+
getReadonly() {
84+
return Boolean(this.readonly ?? this.radioGroup?.readonly);
85+
},
86+
8387
onInputClick(e: MouseEvent) {
8488
e.stopPropagation();
8589
},
8690

8791
handleRadioClick(e: MouseEvent) {
8892
const tDisabled = this.getDisabled();
89-
if (tDisabled) return;
93+
const tReadonly = this.getReadonly();
94+
if (tDisabled || tReadonly) return;
9095
this.$emit('click', { e });
9196
this.checkRadio(e);
9297
},
@@ -99,7 +104,7 @@ export default mixins(Vue as VueConstructor<RadioParentInjectInstance>, classPre
99104

100105
if (this.radioGroup) {
101106
const value = tChecked && allowUncheck ? undefined : this.value;
102-
this.radioGroup.handleRadioChange(value, { e });
107+
this.radioGroup.handleRadioChange(value, { e, name: this.radioGroup.name });
103108
} else {
104109
const value = allowUncheck ? !tChecked : true;
105110
emitEvent<Parameters<TdRadioProps['onChange']>>(this, 'change', value, { e });

src/radio/type.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface TdRadioProps<T = RadioValue> {
2727
*/
2828
default?: string | TNode;
2929
/**
30-
* 是否为禁用态
30+
* 是否为禁用态。如果存在父组件 RadioGroup,默认值由 RadioGroup.disabled 控制。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled
3131
*/
3232
disabled?: boolean;
3333
/**
@@ -39,6 +39,10 @@ export interface TdRadioProps<T = RadioValue> {
3939
* @default ''
4040
*/
4141
name?: string;
42+
/**
43+
* 只读状态
44+
*/
45+
readonly?: boolean;
4246
/**
4347
* 单选按钮的值
4448
*/
@@ -60,7 +64,7 @@ export interface TdRadioGroupProps<T = RadioValue> {
6064
*/
6165
allowUncheck?: boolean;
6266
/**
63-
* 是否禁用全部子单选框
67+
* 是否禁用全部子单选框。优先级:Radio.disabled > RadioGroup.disabled > Form.disabled
6468
*/
6569
disabled?: boolean;
6670
/**
@@ -72,11 +76,20 @@ export interface TdRadioGroupProps<T = RadioValue> {
7276
* 单选组件按钮形式。RadioOption 数据类型为 string 或 number 时,表示 label 和 value 值相同
7377
*/
7478
options?: Array<RadioOption>;
79+
/**
80+
* 只读状态
81+
*/
82+
readonly?: boolean;
7583
/**
7684
* 组件尺寸【讨论中】
7785
* @default medium
7886
*/
7987
size?: SizeEnum;
88+
/**
89+
* 组件风格
90+
* @default radio
91+
*/
92+
theme?: 'radio' | 'button';
8093
/**
8194
* 选中的值
8295
*/
@@ -90,23 +103,18 @@ export interface TdRadioGroupProps<T = RadioValue> {
90103
* @default outline
91104
*/
92105
variant?: 'outline' | 'primary-filled' | 'default-filled';
93-
94106
/**
95-
* 组件风格
96-
*/
97-
theme?: 'radio' | 'button';
98-
/**
99-
* 选中值发生变化时触发
107+
* 选中值发生变化时触发, `context.name` 指 RadioGroup 的 name 属性
100108
*/
101-
onChange?: (value: T, context: { e: Event }) => void;
109+
onChange?: (value: T, context: { e: Event; name?: string }) => void;
102110
}
103111

104-
export type RadioValue = string | number | boolean;
105-
106112
export type RadioOption = string | number | RadioOptionObj;
107113

108114
export interface RadioOptionObj {
109115
label?: string | TNode;
110-
value?: string | number;
116+
value?: string | number | boolean;
111117
disabled?: boolean;
112118
}
119+
120+
export type RadioValue = string | number | boolean;

0 commit comments

Comments
 (0)