Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(radio): add support for allowUncheck #123

Merged
merged 5 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions examples/radio/radio.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
:: BASE_DOC ::

## API

### Radio Props

名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
allowUncheck | Boolean | false | 【开发中】是否允许取消选中 | N
allowUncheck | Boolean | false | 是否允许取消选中 | N
checked | Boolean | - | 是否选中。支持语法糖 | N
defaultChecked | Boolean | - | 是否选中。非受控属性 | N
default | String / Slot / Function | - | 单选按钮内容,同 label。TS 类型:`string | TNode`。[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
disabled | Boolean | undefined | 是否为禁用态 | N
label | String / Slot / Function | - | 主文案。TS 类型:`string | TNode`。[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts) | N
name | String | - | HTM 元素原生属性 | N
name | String | - | HTML 元素原生属性 | N
value | String / Number / Boolean | undefined | 单选按钮的值。TS 类型:`RadioValue`。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
onChange | Function | | 选中状态变化时触发。`(checked: boolean, context: { e: Event }) => {}` | N

Expand All @@ -26,7 +25,6 @@ change | `(checked: boolean, context: { e: Event })` | 选中状态变化时触

名称 | 类型 | 默认值 | 说明 | 必传
-- | -- | -- | -- | --
buttonStyle | String | outline | 已废弃。单选组件按钮形式(请使用 variant 代替)。可选项:outline/solid | N
disabled | Boolean | undefined | 是否禁用全部子单选框 | N
name | String | - | HTML 元素原生属性 | N
options | Array | - | 单选组件按钮形式。RadioOption 数据类型为 string 或 number 时,表示 label 和 value 值相同。TS 类型:`Array<RadioOption>`。[通用类型定义](https://github.com/Tencent/tdesign-vue/blob/develop/src/common.ts)。[详细类型定义](https://github.com/Tencent/tdesign-vue/tree/develop/src/radio/type.ts) | N
Expand Down
6 changes: 3 additions & 3 deletions src/radio/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* updated at 2021-12-12 16:59:59
* updated at 2021-12-28 21:43:41
* */

import { TdRadioProps } from './type';
import { PropType } from 'vue';

export default {
/** 【开发中】是否允许取消选中 */
/** 是否允许取消选中 */
allowUncheck: Boolean,
/** 是否选中 */
checked: Boolean,
Expand All @@ -28,7 +28,7 @@ export default {
label: {
type: [String, Function] as PropType<TdRadioProps['label']>,
},
/** HTM 元素原生属性 */
/** HTML 元素原生属性 */
name: {
type: String,
default: '',
Expand Down
10 changes: 1 addition & 9 deletions src/radio/radio-group-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* updated at 2021-11-19 10:44:26
* updated at 2021-12-28 21:43:41
* */

import { TdRadioGroupProps } from '../radio/type';
import { PropType } from 'vue';

export default {
/** 已废弃。单选组件按钮形式(请使用 variant 代替) */
buttonStyle: {
type: String as PropType<TdRadioGroupProps['buttonStyle']>,
default: 'outline' as TdRadioGroupProps['buttonStyle'],
validator(val: TdRadioGroupProps['buttonStyle']): boolean {
return ['outline', 'solid'].includes(val);
},
},
/** 是否禁用全部子单选框 */
disabled: {
type: Boolean,
Expand Down
14 changes: 13 additions & 1 deletion src/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export default (Vue as VueConstructor<RadioInstance>).extend({
<input
type="radio"
class={`${prefixCls}__former`}
on={{ ...omit(this.$listeners, ['change']) }}
on={{ ...omit(this.$listeners, ['change', 'click']) }}
{...{ domProps: inputProps }}
onChange={this.handleChange}
onClick={this.handleClick}
/>
<span class={`${prefixCls}__input`}></span>
<span class={`${prefixCls}__label`}>{renderContent(this, 'default', 'label')}</span>
Expand All @@ -78,5 +79,16 @@ export default (Vue as VueConstructor<RadioInstance>).extend({
emitEvent<Parameters<TdRadioProps['onChange']>>(this, 'change', target.checked, { e });
}
},
handleClick(e: Event) {
if (typeof this.$listeners.click === 'function') {
this.$listeners.click(e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.$listeners.click(e)
此处直接写成 this.$emit('click')

}
if (!this.checked || !this.allowUncheck) return;
if (this.radioGroup) {
this.radioGroup.$emit('checked-change', null, { e });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null 更为 undefined

} else {
emitEvent<Parameters<TdRadioProps['onChange']>>(this, 'change', false, { e });
}
},
},
});
16 changes: 5 additions & 11 deletions src/radio/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* updated at 2021-12-12 16:59:59
* updated at 2021-12-28 21:43:41
* */

import { TNode, SizeEnum } from '../common';

export interface TdRadioProps {
/**
* 【开发中】是否允许取消选中
* 是否允许取消选中
* @default false
*/
allowUncheck?: boolean;
Expand All @@ -34,7 +34,7 @@ export interface TdRadioProps {
*/
label?: string | TNode;
/**
* HTM 元素原生属性
* HTML 元素原生属性
* @default ''
*/
name?: string;
Expand All @@ -46,15 +46,9 @@ export interface TdRadioProps {
* 选中状态变化时触发
*/
onChange?: (checked: boolean, context: { e: Event }) => void;
};
}

export interface TdRadioGroupProps {
/**
* 单选组件按钮形式(请使用 variant 代替)
* @default outline
* @deprecated
*/
buttonStyle?: 'outline' | 'solid';
/**
* 是否禁用全部子单选框
*/
Expand Down Expand Up @@ -90,7 +84,7 @@ export interface TdRadioGroupProps {
* 选中值发生变化时触发
*/
onChange?: (value: RadioValue, context: { e: Event }) => void;
};
}

export type RadioValue = string | number | boolean;

Expand Down