feat(components): [el-button-group] add type prop (#3702)

* feat(components): [el-button-group] add type prop

* fix breadcrumb import
This commit is contained in:
三咲智子 2021-09-28 20:51:08 +08:00 committed by GitHub
parent 8583c70fdc
commit 93f0a35905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 15 deletions

View File

@ -152,4 +152,23 @@ describe('Button Group', () => {
wrapper.findAll('.el-button-group button.el-button--mini').length
).toBe(1)
})
it('button group type', async () => {
const wrapper = mount({
setup() {
return () =>
h(ButtonGroup, { type: 'warning' }, () => [
h(Button, { type: 'primary' }, () => 'Prev'),
h(Button, {}, () => 'Next'),
])
},
})
expect(wrapper.classes()).toContain('el-button-group')
expect(
wrapper.findAll('.el-button-group button.el-button--primary').length
).toBe(1)
expect(
wrapper.findAll('.el-button-group button.el-button--warning').length
).toBe(1)
})
})

View File

@ -0,0 +1,9 @@
import { buttonProps } from './button'
import type { ExtractPropTypes } from 'vue'
export const buttonGroupProps = {
size: buttonProps.size,
type: buttonProps.type,
} as const
export type ButtonGroupProps = ExtractPropTypes<typeof buttonGroupProps>

View File

@ -6,24 +6,18 @@
<script lang="ts">
import { defineComponent, provide, reactive, toRef } from 'vue'
import { elButtonGroupKey } from '@element-plus/tokens'
import { isValidComponentSize } from '@element-plus/utils/validators'
import type { PropType } from 'vue'
import type { ComponentSize } from '@element-plus/utils/types'
import { buttonGroupProps } from './button-group'
export default defineComponent({
name: 'ElButtonGroup',
props: {
size: {
type: String as PropType<ComponentSize>,
validator: isValidComponentSize,
},
},
props: buttonGroupProps,
setup(props) {
provide(
elButtonGroupKey,
reactive({
size: toRef(props, 'size'),
type: toRef(props, 'type'),
})
)
},

View File

@ -11,6 +11,7 @@ export const buttonType = [
'info',
'danger',
'text',
'',
] as const
export const buttonSize = ['', 'large', 'medium', 'small', 'mini'] as const
export const buttonNativeType = ['button', 'submit', 'reset'] as const
@ -20,7 +21,7 @@ export const buttonProps = {
type: buildProp({
type: String,
values: buttonType,
default: 'default',
default: '',
} as const),
icon: {
type: String,

View File

@ -2,7 +2,7 @@
<button
:class="[
'el-button',
type ? 'el-button--' + type : '',
buttonType ? 'el-button--' + buttonType : '',
buttonSize ? 'el-button--' + buttonSize : '',
{
'is-disabled': buttonDisabled,
@ -40,6 +40,9 @@ export default defineComponent({
const { size: buttonSize, disabled: buttonDisabled } = useFormItem({
size: computed(() => elBtnGroup?.size),
})
const buttonType = computed(
() => props.type || elBtnGroup?.type || 'default'
)
const elForm = inject(elFormKey, undefined)
@ -52,7 +55,9 @@ export default defineComponent({
return {
buttonSize,
buttonType,
buttonDisabled,
handleClick,
}
},

View File

@ -1,5 +1,5 @@
import type { InjectionKey } from 'vue'
import type { BreadcrumbProps } from '@element-plus/components/breadcrumb/src/breadcrumb'
import type { BreadcrumbProps } from '@element-plus/components/breadcrumb'
export const elBreadcrumbKey: InjectionKey<BreadcrumbProps> =
Symbol('elBreadcrumbKey')

View File

@ -1,9 +1,10 @@
import type { InjectionKey } from 'vue'
import type { ComponentSize } from '@element-plus/utils/types'
import type { ButtonProps } from '@element-plus/components/button'
export interface ElButtonGroupContext {
size?: ComponentSize
size?: ButtonProps['size']
type?: ButtonProps['type']
}
export const elButtonGroupKey: InjectionKey<ElButtonGroupContext> = Symbol()