Files
element-plus/packages/components/button/src/use-button.ts
Jeremy 4150baa7a3 refactor(components): [button] (#10152)
* refactor(components): [button]

* Extract logic code out of component.
* Reorganize code for better readabilities.

* chore: remove export of button group instance
2022-10-20 17:14:02 +08:00

71 lines
1.8 KiB
TypeScript

import { Text, computed, inject, ref, useSlots } from 'vue'
import {
useDeprecated,
useDisabled,
useFormItem,
useGlobalConfig,
useSize,
} from '@element-plus/hooks'
import { buttonGroupContextKey } from '@element-plus/tokens'
import type { SetupContext } from 'vue'
import type { ButtonEmits, ButtonProps } from './button'
export const useButton = (
props: ButtonProps,
emit: SetupContext<ButtonEmits>['emit']
) => {
useDeprecated(
{
from: 'type.text',
replacement: 'link',
version: '3.0.0',
scope: 'props',
ref: 'https://element-plus.org/en-US/component/button.html#button-attributes',
},
computed(() => props.type === 'text')
)
const buttonGroupContext = inject(buttonGroupContextKey, undefined)
const globalConfig = useGlobalConfig('button')
const { form } = useFormItem()
const _size = useSize(computed(() => buttonGroupContext?.size))
const _disabled = useDisabled()
const _ref = ref<HTMLButtonElement>()
const slots = useSlots()
const _type = computed(() => props.type || buttonGroupContext?.type || '')
const autoInsertSpace = computed(
() => props.autoInsertSpace ?? globalConfig.value?.autoInsertSpace ?? false
)
// add space between two characters in Chinese
const shouldAddSpace = computed(() => {
const defaultSlot = slots.default?.()
if (autoInsertSpace.value && defaultSlot?.length === 1) {
const slot = defaultSlot[0]
if (slot?.type === Text) {
const text = slot.children as string
return /^\p{Unified_Ideograph}{2}$/u.test(text.trim())
}
}
return false
})
const handleClick = (evt: MouseEvent) => {
if (props.nativeType === 'reset') {
form?.resetFields()
}
emit('click', evt)
}
return {
_disabled,
_size,
_type,
_ref,
shouldAddSpace,
handleClick,
}
}