From ea812ae622b8ef979abb3538c3e7dd6d4b748ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sat, 12 Feb 2022 18:37:16 +0800 Subject: [PATCH] refactor(components): refactor button (#5933) * refactor(components): refactor button * refactor: rename * test: apply jsx * feat: expose * test: fix --- .../{button.spec.ts => button.spec.tsx} | 79 +++---- packages/components/button/src/button.ts | 17 +- packages/components/button/src/button.vue | 202 ++++++++---------- .../components/popconfirm/src/popconfirm.ts | 6 +- .../hooks/__tests__/use-form-item.spec.ts | 15 +- 5 files changed, 141 insertions(+), 178 deletions(-) rename packages/components/button/__tests__/{button.spec.ts => button.spec.tsx} (77%) diff --git a/packages/components/button/__tests__/button.spec.ts b/packages/components/button/__tests__/button.spec.tsx similarity index 77% rename from packages/components/button/__tests__/button.spec.ts rename to packages/components/button/__tests__/button.spec.tsx index 6d6a891285..0b5efd5e47 100644 --- a/packages/components/button/__tests__/button.spec.ts +++ b/packages/components/button/__tests__/button.spec.tsx @@ -1,8 +1,9 @@ -import { ref, h, nextTick, defineComponent } from 'vue' +import { ref, nextTick, defineComponent } from 'vue' import { mount } from '@vue/test-utils' import { Loading, Search } from '@element-plus/icons-vue' import Button from '../src/button.vue' import ButtonGroup from '../src/button-group.vue' +import type { ComponentSize } from '@element-plus/constants' const AXIOM = 'Rem is the best girl' @@ -83,7 +84,7 @@ describe('Button.vue', () => { default: '', }, }) - await (wrapper.element.querySelector('.inner-slot')).click() + wrapper.element.querySelector('.inner-slot')!.click() expect(wrapper.emitted()).toBeDefined() }) @@ -119,25 +120,15 @@ describe('Button.vue', () => { it('loading slot', () => { const App = defineComponent({ - setup() { - return () => - h( - Button, - { - loading: true, - }, - { - default: 'Loading', - loading: h( - 'span', - { - class: 'custom-loading', - }, - ['111'] - ), - } - ) - }, + setup: () => () => + ( + + ), }) const wrapper = mount(App) expect(wrapper.find('.custom-loading').exists()).toBeTruthy() @@ -146,30 +137,28 @@ describe('Button.vue', () => { describe('Button Group', () => { it('create', () => { const wrapper = mount({ - template: ` - - Prev - Next - `, - components: { - 'el-button-group': ButtonGroup, - 'el-button': Button, - }, + setup: () => () => + ( + + + + + ), }) expect(wrapper.classes()).toContain('el-button-group') expect(wrapper.findAll('button').length).toBe(2) }) it('button group reactive size', async () => { - const size = ref('small') + const size = ref('small') const wrapper = mount({ - setup() { - return () => - h(ButtonGroup, { size: size.value }, () => [ - h(Button, { type: 'primary' }, () => 'Prev'), - h(Button, { type: 'primary' }, () => 'Next'), - ]) - }, + setup: () => () => + ( + + + + + ), }) expect(wrapper.classes()).toContain('el-button-group') expect( @@ -186,13 +175,13 @@ describe('Button Group', () => { it('button group type', async () => { const wrapper = mount({ - setup() { - return () => - h(ButtonGroup, { type: 'warning' }, () => [ - h(Button, { type: 'primary' }, () => 'Prev'), - h(Button, {}, () => 'Next'), - ]) - }, + setup: () => () => + ( + + + + + ), }) expect(wrapper.classes()).toContain('el-button-group') expect( diff --git a/packages/components/button/src/button.ts b/packages/components/button/src/button.ts index e0f11d011c..36459fa7c8 100644 --- a/packages/components/button/src/button.ts +++ b/packages/components/button/src/button.ts @@ -4,7 +4,7 @@ import { Loading } from '@element-plus/icons-vue' import type { ExtractPropTypes } from 'vue' import type button from './button.vue' -export const buttonType = [ +export const buttonTypes = [ 'default', 'primary', 'success', @@ -14,14 +14,14 @@ export const buttonType = [ 'text', '', ] as const -export const buttonNativeType = ['button', 'submit', 'reset'] as const +export const buttonNativeTypes = ['button', 'submit', 'reset'] as const export const buttonProps = buildProps({ size: useSizeProp, disabled: Boolean, type: { type: String, - values: buttonType, + values: buttonTypes, default: '', }, icon: { @@ -30,7 +30,7 @@ export const buttonProps = buildProps({ }, nativeType: { type: String, - values: buttonNativeType, + values: buttonNativeTypes, default: 'button', }, loading: Boolean, @@ -48,11 +48,6 @@ export const buttonProps = buildProps({ default: undefined, }, } as const) - -export interface ButtonConfigContext { - autoInsertSpace?: boolean -} - export const buttonEmits = { click: (evt: MouseEvent) => evt instanceof MouseEvent, } @@ -64,3 +59,7 @@ export type ButtonType = ButtonProps['type'] export type ButtonNativeType = ButtonProps['nativeType'] export type ButtonInstance = InstanceType + +export interface ButtonConfigContext { + autoInsertSpace?: boolean +} diff --git a/packages/components/button/src/button.vue b/packages/components/button/src/button.vue index 951371d9f8..93df913652 100644 --- a/packages/components/button/src/button.vue +++ b/packages/components/button/src/button.vue @@ -1,17 +1,17 @@ - diff --git a/packages/components/popconfirm/src/popconfirm.ts b/packages/components/popconfirm/src/popconfirm.ts index dc1649b646..b894dbd325 100644 --- a/packages/components/popconfirm/src/popconfirm.ts +++ b/packages/components/popconfirm/src/popconfirm.ts @@ -1,4 +1,4 @@ -import { buttonType } from '@element-plus/components/button' +import { buttonTypes } from '@element-plus/components/button' import { QuestionFilled } from '@element-plus/icons-vue' import { buildProps, definePropType, iconPropType } from '@element-plus/utils' import { useTooltipContentProps } from '@element-plus/components/tooltip' @@ -16,12 +16,12 @@ export const popconfirmProps = buildProps({ }, confirmButtonType: { type: String, - values: buttonType, + values: buttonTypes, default: 'primary', }, cancelButtonType: { type: String, - values: buttonType, + values: buttonTypes, default: 'text', }, icon: { diff --git a/packages/hooks/__tests__/use-form-item.spec.ts b/packages/hooks/__tests__/use-form-item.spec.ts index d987254471..fd2997df3e 100644 --- a/packages/hooks/__tests__/use-form-item.spec.ts +++ b/packages/hooks/__tests__/use-form-item.spec.ts @@ -34,17 +34,10 @@ const mountComponent = (setup = NOOP, options = {}) => { ) } -const getButtonVm = (wrapper: ReturnType) => { - return wrapper.findComponent(ElButton).vm as any as { - buttonSize: string - buttonDisabled: boolean - } -} - describe('use-form-item', () => { it('should return local value', () => { const wrapper = mountComponent() - expect(getButtonVm(wrapper).buttonSize).toBe('default') + expect(wrapper.find('.el-button--default').exists()).toBe(true) }) it('should return props.size instead of injected.size', () => { @@ -62,7 +55,7 @@ describe('use-form-item', () => { } ) - expect(getButtonVm(wrapper).buttonSize).toBe(propSize) + expect(wrapper.find(`.el-button--${propSize}`).exists()).toBe(true) }) it('should return fallback.size instead inject.size', () => { @@ -77,7 +70,7 @@ describe('use-form-item', () => { } as ElFormItemContext) }) - expect(getButtonVm(wrapper).buttonSize).toBe(fallbackSize) + expect(wrapper.find(`.el-button--${fallbackSize}`).exists()).toBe(true) }) it('should return formItem.size instead form.size', () => { @@ -92,6 +85,6 @@ describe('use-form-item', () => { } as ElFormContext) }) - expect(getButtonVm(wrapper).buttonSize).toBe(itemSize) + expect(wrapper.find(`.el-button--${itemSize}`).exists()).toBe(true) }) })