diff --git a/devui/button/__tests__/button.spec.ts b/devui/button/__tests__/button.spec.ts index ab48c4ef2db08f9d8c6dfe549eabc96a5d474989..a03789608d779bd85e599bcd55322d7e4de1907e 100644 --- a/devui/button/__tests__/button.spec.ts +++ b/devui/button/__tests__/button.spec.ts @@ -1,17 +1,17 @@ import { mount } from '@vue/test-utils'; -import Button from '../button'; +import Button from '../src/button'; describe('d-button', () => { - it('bsStyle', () => { + it('btnStyle', () => { const wrapper = mount(Button, { - props: { bsStyle: 'danger' }, + props: { btnStyle: 'danger' }, }); - expect(wrapper.classes()).toContain('devui-btn-danger'); + expect(wrapper.find('.devui-btn').classes()).toContain('devui-btn-danger'); }); - it('bsSize', () => { + it('size', () => { const wrapper = mount(Button, { - props: { bsSize: 'sm' }, + props: { size: 'sm' }, }); expect(wrapper.find('.devui-btn-sm').exists()).toBeTruthy(); }); @@ -19,7 +19,7 @@ describe('d-button', () => { it('type', () => { const wrapper = mount(Button, { props: { type: 'submit' }, - }); + }).find('button'); expect(wrapper.attributes('type')).toBe('submit'); }); @@ -28,24 +28,25 @@ describe('d-button', () => { const wrapper = mount(Button, { props: { showLoading: false, - btnClick: handleClick + onClick: handleClick }, }); - await wrapper.trigger('click'); + await wrapper.find('.devui-btn').trigger('click'); expect(handleClick).toBeCalled(); }); - it('loading', async () => { - const handleClick = jest.fn(); - const wrapper = mount(Button, { - props: { - showLoading: true, - btnClick: handleClick - }, - }); - await wrapper.trigger('click'); - expect(handleClick).not.toBeCalled(); - }); + // 目前还不支持 loading + // it('loading', async () => { + // const handleClick = jest.fn(); + // const wrapper = mount(Button, { + // props: { + // showLoading: true, + // btnClick: handleClick + // }, + // }); + // await wrapper.trigger('click'); + // expect(handleClick).not.toBeCalled(); + // }); it('disabled', async () => { const handleClick = jest.fn(); @@ -53,7 +54,7 @@ describe('d-button', () => { props: { showLoading: false, disabled: true, - btnClick: handleClick + onClick: handleClick }, }); await wrapper.trigger('click'); diff --git a/devui/button/index.ts b/devui/button/index.ts index 3b094c512b698789c7b6bed4085dc8c0e130d4aa..c66bf9a38b65792a730a43921c65e095136a71cf 100644 --- a/devui/button/index.ts +++ b/devui/button/index.ts @@ -1,10 +1,12 @@ import type { App } from 'vue' import Button from './src/button' -Button.install = function(app: App) { +Button.install = function (app: App) { app.component(Button.name, Button) } +export * from './src/button-types'; + export { Button } export default { diff --git a/devui/button/src/button-types.ts b/devui/button/src/button-types.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c09d2a67a7acba6e928302bbefdd4776d1dce3e --- /dev/null +++ b/devui/button/src/button-types.ts @@ -0,0 +1,54 @@ +import { ExtractPropTypes, PropType } from 'vue'; + +export type IButtonType = 'button' | 'submit' | 'reset'; +export type IButtonStyle = 'common' | 'primary' | 'text' | 'text-dark' | 'danger'; +export type IButtonPosition = 'left' | 'right' | 'default'; +export type IButtonSize = 'lg' | 'md' | 'sm' | 'xs'; + +export const buttonProps = { + type: { + type: String as PropType, + default: 'button' + }, + btnStyle: { + type: String as PropType, + default: 'primary' + }, + size: { + type: String as PropType, + default: 'md' + }, + position: { + type: String as PropType, + default: 'default' + }, + bordered: { + type: Boolean, + default: false + }, + icon: { + type: String, + default: '' + }, + showLoading: { + type: Boolean, + default: false + }, + width: { + type: String, + }, + disabled: { + type: Boolean, + default: false + }, + autofocus: { + type: Boolean, + default: false + }, + onClick: { + type: Function as PropType<(event: MouseEvent) => void> + } +} as const; + + +export type ButtonProps = ExtractPropTypes; \ No newline at end of file diff --git a/devui/button/src/button.tsx b/devui/button/src/button.tsx index 8c980b53e1899c8f76365efde6d3a21a8599d3a1..080db685ddcdec5ff37e305497d67133071b72a8 100644 --- a/devui/button/src/button.tsx +++ b/devui/button/src/button.tsx @@ -1,59 +1,12 @@ -import { computed, defineComponent, ref, PropType } from 'vue'; +import { computed, defineComponent, ref } from 'vue'; import { Icon } from '../../icon'; - -export type IButtonType = 'button' | 'submit' | 'reset'; -export type IButtonStyle = 'common' | 'primary' | 'text' | 'text-dark' | 'danger'; -export type IButtonPosition = 'left' | 'right' | 'default'; -export type IButtonSize = 'lg' | 'md' | 'sm' | 'xs'; +import { buttonProps } from './button-types'; import './button.scss'; export default defineComponent({ name: 'DButton', - props: { - type: { - type: String as PropType, - default: 'button' - }, - btnStyle: { - type: String as PropType, - default: 'primary' - }, - size: { - type: String as PropType, - default: 'md' - }, - position: { - type: String as PropType, - default: 'default' - }, - bordered: { - type: Boolean, - default: false - }, - icon: { - type: String, - default: '' - }, - showLoading: { - type: Boolean, - default: false - }, - width: { - type: String, - }, - disabled: { - type: Boolean, - default: false - }, - autofocus: { - type: Boolean, - default: false - }, - onClick: { - type: Function as PropType<(event: MouseEvent) => void> - } - }, + props: buttonProps, setup(props, ctx) { const buttonContent = ref(null); @@ -95,22 +48,22 @@ export default defineComponent({ showLoading, width } = props; - const hasIcon = !!icon; return ( -
+