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 wrapper.findAll('.el-button-group button.el-button--mini').length
).toBe(1) ).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"> <script lang="ts">
import { defineComponent, provide, reactive, toRef } from 'vue' import { defineComponent, provide, reactive, toRef } from 'vue'
import { elButtonGroupKey } from '@element-plus/tokens' import { elButtonGroupKey } from '@element-plus/tokens'
import { isValidComponentSize } from '@element-plus/utils/validators' import { buttonGroupProps } from './button-group'
import type { PropType } from 'vue'
import type { ComponentSize } from '@element-plus/utils/types'
export default defineComponent({ export default defineComponent({
name: 'ElButtonGroup', name: 'ElButtonGroup',
props: { props: buttonGroupProps,
size: {
type: String as PropType<ComponentSize>,
validator: isValidComponentSize,
},
},
setup(props) { setup(props) {
provide( provide(
elButtonGroupKey, elButtonGroupKey,
reactive({ reactive({
size: toRef(props, 'size'), size: toRef(props, 'size'),
type: toRef(props, 'type'),
}) })
) )
}, },

View File

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

View File

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

View File

@ -1,5 +1,5 @@
import type { InjectionKey } from 'vue' 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> = export const elBreadcrumbKey: InjectionKey<BreadcrumbProps> =
Symbol('elBreadcrumbKey') Symbol('elBreadcrumbKey')

View File

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