Skip to content

Commit 19ec975

Browse files
authored
refactor:checkbox (#6248)
* refactor:checkbox * docs:update & refactor: checkbox type
1 parent 4ccb1c3 commit 19ec975

File tree

8 files changed

+353
-42
lines changed

8 files changed

+353
-42
lines changed

components/checkbox/Checkbox.tsx

+16-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import useConfigInject from '../config-provider/hooks/useConfigInject';
1111
import type { CheckboxChangeEvent, CheckboxProps } from './interface';
1212
import { CheckboxGroupContextKey, checkboxProps } from './interface';
1313

14+
// CSSINJS
15+
import useStyle from './style';
16+
1417
export default defineComponent({
1518
compatConfig: { MODE: 3 },
1619
name: 'ACheckbox',
@@ -22,6 +25,10 @@ export default defineComponent({
2225
const formItemContext = useInjectFormItemContext();
2326
const formItemInputContext = FormItemInputContext.useInject();
2427
const { prefixCls, direction } = useConfigInject('checkbox', props);
28+
29+
// style
30+
const [wrapSSR, hashId] = useStyle(prefixCls);
31+
2532
const checkboxGroup = inject(CheckboxGroupContextKey, undefined);
2633
const uniId = Symbol('checkboxUniId');
2734

@@ -90,12 +97,16 @@ export default defineComponent({
9097
[`${prefixCls.value}-wrapper-in-form-item`]: formItemInputContext.isFormItemInput,
9198
},
9299
className,
100+
hashId.value,
101+
);
102+
const checkboxClass = classNames(
103+
{
104+
[`${prefixCls.value}-indeterminate`]: indeterminate,
105+
},
106+
hashId.value,
93107
);
94-
const checkboxClass = classNames({
95-
[`${prefixCls.value}-indeterminate`]: indeterminate,
96-
});
97108
const ariaChecked = indeterminate ? 'mixed' : undefined;
98-
return (
109+
return wrapSSR(
99110
<label
100111
class={classString}
101112
style={style as CSSProperties}
@@ -109,7 +120,7 @@ export default defineComponent({
109120
ref={checkboxRef}
110121
/>
111122
{children.length ? <span>{children}</span> : null}
112-
</label>
123+
</label>,
113124
);
114125
};
115126
},

components/checkbox/Group.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ import useConfigInject from '../config-provider/hooks/useConfigInject';
55
import type { CheckboxOptionType } from './interface';
66
import { CheckboxGroupContextKey, checkboxGroupProps } from './interface';
77

8+
// CSSINJS
9+
import useStyle from './style';
10+
811
export default defineComponent({
912
compatConfig: { MODE: 3 },
1013
name: 'ACheckboxGroup',
14+
inheritAttrs: false,
1115
props: checkboxGroupProps(),
1216
// emits: ['change', 'update:value'],
13-
setup(props, { slots, emit, expose }) {
17+
setup(props, { slots, attrs, emit, expose }) {
1418
const formItemContext = useInjectFormItemContext();
1519
const { prefixCls, direction } = useConfigInject('checkbox', props);
20+
const groupPrefixCls = computed(() => `${prefixCls.value}-group`);
21+
22+
// style
23+
const [wrapSSR, hashId] = useStyle(groupPrefixCls);
24+
1625
const mergedValue = ref((props.value === undefined ? props.defaultValue : props.value) || []);
1726
watch(
1827
() => props.value,
@@ -87,7 +96,6 @@ export default defineComponent({
8796
return () => {
8897
const { id = formItemContext.id.value } = props;
8998
let children = null;
90-
const groupPrefixCls = `${prefixCls.value}-group`;
9199
if (options.value && options.value.length > 0) {
92100
children = options.value.map(option => (
93101
<Checkbox
@@ -98,19 +106,25 @@ export default defineComponent({
98106
value={option.value}
99107
checked={mergedValue.value.indexOf(option.value) !== -1}
100108
onChange={option.onChange}
101-
class={`${groupPrefixCls}-item`}
109+
class={`${groupPrefixCls.value}-item`}
102110
>
103111
{option.label === undefined ? slots.label?.(option) : option.label}
104112
</Checkbox>
105113
));
106114
}
107-
return (
115+
return wrapSSR(
108116
<div
109-
class={[groupPrefixCls, { [`${groupPrefixCls}-rtl`]: direction.value === 'rtl' }]}
117+
{...attrs}
118+
class={[
119+
groupPrefixCls.value,
120+
{ [`${groupPrefixCls.value}-rtl`]: direction.value === 'rtl' },
121+
attrs.class,
122+
hashId.value,
123+
]}
110124
id={id}
111125
>
112126
{children || slots.default?.()}
113-
</div>
127+
</div>,
114128
);
115129
};
116130
},

components/checkbox/index.en-US.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
category: Components
33
type: Data Entry
44
title: Checkbox
5-
cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg
5+
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*DzgiRbW3khIAAAAAAAAAAAAADrJ8AQ/original
66
---
77

88
Checkbox component.

components/checkbox/index.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ category: Components
33
subtitle: 多选框
44
type: 数据录入
55
title: Checkbox
6-
cover: https://gw.alipayobjects.com/zos/alicdn/8nbVbHEm_/CheckBox.svg
6+
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*DzgiRbW3khIAAAAAAAAAAAAADrJ8AQ/original
77
---
88

99
多选框。

components/checkbox/interface.ts

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { ExtractPropTypes, InjectionKey, PropType, Ref } from 'vue';
1+
import type { ExtractPropTypes, InjectionKey, Ref } from 'vue';
22
import type { MouseEventHandler } from '../_util/EventInterface';
33
import type { VueNode } from '../_util/type';
44
import PropTypes from '../_util/vue-types';
5+
import { booleanType, functionType, stringType, arrayType } from '../_util/type';
56

67
export type CheckboxValueType = string | number | boolean;
78
export interface CheckboxOptionType {
@@ -27,10 +28,9 @@ export const abstractCheckboxGroupProps = () => {
2728
return {
2829
name: String,
2930
prefixCls: String,
30-
options: {
31-
type: Array as PropType<Array<CheckboxOptionType | string | number>>,
32-
default: () => [] as Array<CheckboxOptionType | string | number>,
33-
},
31+
options: arrayType<Array<CheckboxOptionType | string | number>>(
32+
[] as Array<CheckboxOptionType | string | number>,
33+
),
3434
disabled: Boolean,
3535
id: String,
3636
};
@@ -39,12 +39,10 @@ export const abstractCheckboxGroupProps = () => {
3939
export const checkboxGroupProps = () => {
4040
return {
4141
...abstractCheckboxGroupProps(),
42-
defaultValue: { type: Array as PropType<Array<CheckboxValueType>> },
43-
value: { type: Array as PropType<Array<CheckboxValueType>> },
44-
onChange: { type: Function as PropType<(checkedValue: Array<CheckboxValueType>) => void> },
45-
'onUpdate:value': {
46-
type: Function as PropType<(checkedValue: Array<CheckboxValueType>) => void>,
47-
},
42+
defaultValue: arrayType<Array<CheckboxValueType>>(),
43+
value: arrayType<Array<CheckboxValueType>>(),
44+
onChange: functionType<(checkedValue: Array<CheckboxValueType>) => void>(),
45+
'onUpdate:value': functionType<(checkedValue: Array<CheckboxValueType>) => void>(),
4846
};
4947
};
5048

@@ -53,27 +51,27 @@ export type CheckboxGroupProps = Partial<ExtractPropTypes<ReturnType<typeof chec
5351
export const abstractCheckboxProps = () => {
5452
return {
5553
prefixCls: String,
56-
defaultChecked: { type: Boolean, default: undefined },
57-
checked: { type: Boolean, default: undefined },
58-
disabled: { type: Boolean, default: undefined },
59-
isGroup: { type: Boolean, default: undefined },
54+
defaultChecked: booleanType(),
55+
checked: booleanType(),
56+
disabled: booleanType(),
57+
isGroup: booleanType(),
6058
value: PropTypes.any,
6159
name: String,
6260
id: String,
63-
indeterminate: { type: Boolean, default: undefined },
64-
type: { type: String, default: 'checkbox' },
65-
autofocus: { type: Boolean, default: undefined },
66-
onChange: Function as PropType<(e: CheckboxChangeEvent) => void>,
67-
'onUpdate:checked': Function as PropType<(checked: boolean) => void>,
68-
onClick: Function as PropType<MouseEventHandler>,
69-
skipGroup: { type: Boolean, default: false },
61+
indeterminate: booleanType(),
62+
type: stringType('checkbox'),
63+
autofocus: booleanType(),
64+
onChange: functionType<(e: CheckboxChangeEvent) => void>(),
65+
'onUpdate:checked': functionType<(checked: boolean) => void>(),
66+
onClick: functionType<MouseEventHandler>(),
67+
skipGroup: booleanType(false),
7068
};
7169
};
7270

7371
export const checkboxProps = () => {
7472
return {
7573
...abstractCheckboxProps(),
76-
indeterminate: { type: Boolean, default: false },
74+
indeterminate: booleanType(false),
7775
};
7876
};
7977

0 commit comments

Comments
 (0)