diff --git a/packages/components/checkbox/__tests__/checkbox.test.ts b/packages/components/checkbox/__tests__/checkbox.test.tsx similarity index 97% rename from packages/components/checkbox/__tests__/checkbox.test.ts rename to packages/components/checkbox/__tests__/checkbox.test.tsx index c66a347cc9..f829112640 100644 --- a/packages/components/checkbox/__tests__/checkbox.test.ts +++ b/packages/components/checkbox/__tests__/checkbox.test.tsx @@ -5,13 +5,12 @@ import { ElFormItem } from '@element-plus/components/form' import Checkbox from '../src/checkbox.vue' import CheckboxButton from '../src/checkbox-button.vue' import CheckboxGroup from '../src/checkbox-group.vue' +import type { VueWrapper } from '@vue/test-utils' +import type { FormItemInstance } from '@element-plus/components/form' +import type { CheckboxInstance } from '@element-plus/components/checkbox' -const _mount = ( - template: string, - data: () => D, - otherObj?: Record -) => - mount({ +const _mount = (template: string, data, otherObj?: Record) => + mount({ components: { 'el-checkbox': Checkbox, 'el-checkbox-group': CheckboxGroup, @@ -55,7 +54,7 @@ describe('Checkbox', () => { describe('disabled', () => { test('checkbox without label', async () => { - const wrapper = _mount( + const wrapper: VueWrapper = _mount( ` `, @@ -71,7 +70,7 @@ describe('Checkbox', () => { }) test('checkbox with label attribute', async () => { - const wrapper = _mount( + const wrapper: VueWrapper = _mount( '', () => ({ checkbox: false }) ) @@ -86,7 +85,7 @@ describe('Checkbox', () => { describe('change event', () => { test('checkbox without label', async () => { - const wrapper = _mount( + const wrapper: VueWrapper = _mount( ` `, @@ -108,7 +107,7 @@ describe('Checkbox', () => { }) test('checkbox with label attribute', async () => { - const wrapper = _mount( + const wrapper: VueWrapper = _mount( ``, () => ({ data: null, @@ -128,7 +127,7 @@ describe('Checkbox', () => { }) test('checkbox with label as slot content', async () => { - const wrapper = _mount( + const wrapper: VueWrapper = _mount( `Foobar`, () => ({ data: null, diff --git a/packages/components/checkbox/index.ts b/packages/components/checkbox/index.ts index f542b1885d..be52c066d9 100644 --- a/packages/components/checkbox/index.ts +++ b/packages/components/checkbox/index.ts @@ -12,3 +12,6 @@ export default ElCheckbox export const ElCheckboxButton = withNoopInstall(CheckboxButton) export const ElCheckboxGroup = withNoopInstall(CheckboxGroup) + +export * from './src/checkbox' +export * from './src/checkbox.type' diff --git a/packages/components/checkbox/src/checkbox-button.vue b/packages/components/checkbox/src/checkbox-button.vue index 413e36741d..d4c31f4f5d 100644 --- a/packages/components/checkbox/src/checkbox-button.vue +++ b/packages/components/checkbox/src/checkbox-button.vue @@ -49,11 +49,11 @@ import { computed, defineComponent } from 'vue' import { UPDATE_MODEL_EVENT } from '@element-plus/constants' import { useNamespace } from '@element-plus/hooks' -import { useCheckbox, useCheckboxGroup, useCheckboxProps } from './useCheckbox' +import { checkboxProps, useCheckbox, useCheckboxGroup } from './checkbox' export default defineComponent({ name: 'ElCheckboxButton', - props: useCheckboxProps, + props: checkboxProps, emits: [UPDATE_MODEL_EVENT, 'change'], setup(props, { slots }) { const { focus, isChecked, isDisabled, size, model, handleChange } = diff --git a/packages/components/checkbox/src/checkbox-group.vue b/packages/components/checkbox/src/checkbox-group.vue index 81e772a719..d1a727c875 100644 --- a/packages/components/checkbox/src/checkbox-group.vue +++ b/packages/components/checkbox/src/checkbox-group.vue @@ -16,7 +16,7 @@ import { useCheckboxGroup, useCheckboxGroupId, useCheckboxGroupProps, -} from './useCheckbox' +} from './checkbox' export default defineComponent({ name: 'ElCheckboxGroup', diff --git a/packages/components/checkbox/src/useCheckbox.ts b/packages/components/checkbox/src/checkbox.ts similarity index 89% rename from packages/components/checkbox/src/useCheckbox.ts rename to packages/components/checkbox/src/checkbox.ts index cc7fbcad3f..80605e65f2 100644 --- a/packages/components/checkbox/src/useCheckbox.ts +++ b/packages/components/checkbox/src/checkbox.ts @@ -2,12 +2,12 @@ import { computed, getCurrentInstance, inject, nextTick, ref, watch } from 'vue' import { toTypeString } from '@vue/shared' import { UPDATE_MODEL_EVENT } from '@element-plus/constants' import { formContextKey, formItemContextKey } from '@element-plus/tokens' -import { useFormItemInputId, useSize } from '@element-plus/hooks' -import { debugWarn, isValidComponentSize } from '@element-plus/utils' +import { useFormItemInputId, useSize, useSizeProp } from '@element-plus/hooks' +import { debugWarn, isBoolean, isNumber, isString } from '@element-plus/utils' import type { ComponentInternalInstance, ExtractPropTypes, PropType } from 'vue' -import type { ComponentSize } from '@element-plus/constants' import type { FormContext, FormItemContext } from '@element-plus/tokens' import type { ICheckboxGroupInstance } from './checkbox.type' +import type Checkbox from './checkbox.vue' export const useCheckboxGroupProps = { modelValue: { @@ -23,10 +23,7 @@ export const useCheckboxGroupProps = { type: Number, default: undefined, }, - size: { - type: String as PropType, - validator: isValidComponentSize, - }, + size: useSizeProp, id: { type: String, default: undefined, @@ -53,7 +50,7 @@ export type IUseCheckboxGroupProps = ExtractPropTypes< typeof useCheckboxGroupProps > -export const useCheckboxProps = { +export const checkboxProps = { modelValue: { type: [Number, String, Boolean], default: () => undefined, @@ -85,15 +82,10 @@ export const useCheckboxProps = { default: undefined, }, border: Boolean, - size: { - type: String as PropType, - validator: isValidComponentSize, - }, + size: useSizeProp, tabindex: [String, Number], } -export type IUseCheckboxProps = ExtractPropTypes - export const useCheckboxGroup = () => { const elForm = inject(formContextKey, {} as FormContext) const elFormItem = inject(formItemContextKey, {} as FormItemContext) @@ -127,7 +119,7 @@ export const useCheckboxGroupId = ( } } -const useModel = (props: IUseCheckboxProps) => { +const useModel = (props: CheckboxProps) => { const selfModel = ref(false) const { emit } = getCurrentInstance()! const { isGroup, checkboxGroup, elFormItem } = useCheckboxGroup() @@ -161,7 +153,7 @@ const useModel = (props: IUseCheckboxProps) => { } const useCheckboxStatus = ( - props: IUseCheckboxProps, + props: CheckboxProps, slots: ComponentInternalInstance['slots'], { model }: Partial> ) => { @@ -201,7 +193,7 @@ const useCheckboxStatus = ( } const useDisabled = ( - props: IUseCheckboxProps, + props: CheckboxProps, { model, isChecked, @@ -233,7 +225,7 @@ const useDisabled = ( } const setStoreValue = ( - props: IUseCheckboxProps, + props: CheckboxProps, { model }: Partial> ) => { function addToStore() { @@ -247,7 +239,7 @@ const setStoreValue = ( } const useEvent = ( - props: IUseCheckboxProps, + props: CheckboxProps, { model, isLimitExceeded, @@ -277,7 +269,7 @@ const useEvent = ( emit('change', getLabeledValue(checked), e) } - function handleChange(e: InputEvent) { + function handleChange(e: Event) { if (isLimitExceeded!.value) return const target = e.target as HTMLInputElement emit('change', getLabeledValue(target.checked), e) @@ -311,8 +303,15 @@ const useEvent = ( } } +export const checkboxEmits = { + [UPDATE_MODEL_EVENT]: (val: string | number | boolean) => + isString(val) || isNumber(val) || isBoolean(val), + change: (val: string | number | boolean) => + isString(val) || isNumber(val) || isBoolean(val), +} + export const useCheckbox = ( - props: IUseCheckboxProps, + props: CheckboxProps, slots: ComponentInternalInstance['slots'] ) => { const { model, isGroup, isLimitExceeded, elFormItem } = useModel(props) @@ -352,3 +351,7 @@ export const useCheckbox = ( size, } } + +export type CheckboxProps = ExtractPropTypes +export type CheckboxEmits = typeof checkboxEmits +export type CheckboxInstance = InstanceType diff --git a/packages/components/checkbox/src/checkbox.vue b/packages/components/checkbox/src/checkbox.vue index 809c52806b..90ab7d0708 100644 --- a/packages/components/checkbox/src/checkbox.vue +++ b/packages/components/checkbox/src/checkbox.vue @@ -62,23 +62,32 @@ -