UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 4.31 kB
{"version":3,"file":"use-button.mjs","names":[],"sources":["../../../../../../packages/components/button/src/use-button.ts"],"sourcesContent":["import { Text, computed, inject, ref, useSlots } from 'vue'\nimport {\n useFormDisabled,\n useFormItem,\n useFormSize,\n} from '@element-plus/components/form'\nimport { useGlobalConfig } from '@element-plus/components/config-provider'\nimport { useDeprecated } from '@element-plus/hooks'\nimport { buttonGroupContextKey } from './constants'\n\nimport type { SetupContext } from 'vue'\nimport type { ButtonEmits, ButtonProps } from './button'\n\nexport const useButton = (\n props: ButtonProps,\n emit: SetupContext<ButtonEmits>['emit']\n) => {\n useDeprecated(\n {\n from: 'type.text',\n replacement: 'link',\n version: '3.0.0',\n scope: 'props',\n ref: 'https://element-plus.org/en-US/component/button.html#button-attributes',\n },\n computed(() => props.type === 'text')\n )\n\n const buttonGroupContext = inject(buttonGroupContextKey, undefined)\n const globalConfig = useGlobalConfig('button')\n const { form } = useFormItem()\n const _size = useFormSize(computed(() => buttonGroupContext?.size))\n const _disabled = useFormDisabled()\n const _ref = ref<HTMLButtonElement>()\n const slots = useSlots()\n\n const _type = computed(\n () =>\n props.type || buttonGroupContext?.type || globalConfig.value?.type || ''\n )\n const autoInsertSpace = computed(\n () => props.autoInsertSpace ?? globalConfig.value?.autoInsertSpace ?? false\n )\n const _plain = computed(\n () => props.plain ?? globalConfig.value?.plain ?? false\n )\n const _round = computed(\n () => props.round ?? globalConfig.value?.round ?? false\n )\n const _text = computed(() => props.text ?? globalConfig.value?.text ?? false)\n const _dashed = computed(\n () => props.dashed ?? globalConfig.value?.dashed ?? false\n )\n\n const _props = computed(() => {\n if (props.tag === 'button') {\n return {\n ariaDisabled: _disabled.value || props.loading,\n disabled: _disabled.value || props.loading,\n autofocus: props.autofocus,\n type: props.nativeType,\n }\n }\n return {}\n })\n\n // add space between two characters in Chinese\n const shouldAddSpace = computed(() => {\n const defaultSlot = slots.default?.()\n if (autoInsertSpace.value && defaultSlot?.length === 1) {\n const slot = defaultSlot[0]\n if (slot?.type === Text) {\n const text = slot.children as string\n return /^\\p{Unified_Ideograph}{2}$/u.test(text.trim())\n }\n }\n return false\n })\n\n const handleClick = (evt: MouseEvent) => {\n if (_disabled.value || props.loading) {\n evt.stopPropagation()\n return\n }\n if (props.nativeType === 'reset') {\n form?.resetFields()\n }\n emit('click', evt)\n }\n\n return {\n _disabled,\n _size,\n _type,\n _ref,\n _props,\n _plain,\n _round,\n _text,\n _dashed,\n shouldAddSpace,\n handleClick,\n }\n}\n"],"mappings":";;;;;;;;AAaA,MAAa,aACX,OACA,SACG;AACH,eACE;EACE,MAAM;EACN,aAAa;EACb,SAAS;EACT,OAAO;EACP,KAAK;EACN,EACD,eAAe,MAAM,SAAS,OAAO,CACtC;CAED,MAAM,qBAAqB,OAAO,uBAAuB,OAAU;CACnE,MAAM,eAAe,gBAAgB,SAAS;CAC9C,MAAM,EAAE,SAAS,aAAa;CAC9B,MAAM,QAAQ,YAAY,eAAe,oBAAoB,KAAK,CAAC;CACnE,MAAM,YAAY,iBAAiB;CACnC,MAAM,OAAO,KAAwB;CACrC,MAAM,QAAQ,UAAU;CAExB,MAAM,QAAQ,eAEV,MAAM,QAAQ,oBAAoB,QAAQ,aAAa,OAAO,QAAQ,GACzE;CACD,MAAM,kBAAkB,eAChB,MAAM,mBAAmB,aAAa,OAAO,mBAAmB,MACvE;CACD,MAAM,SAAS,eACP,MAAM,SAAS,aAAa,OAAO,SAAS,MACnD;CACD,MAAM,SAAS,eACP,MAAM,SAAS,aAAa,OAAO,SAAS,MACnD;CACD,MAAM,QAAQ,eAAe,MAAM,QAAQ,aAAa,OAAO,QAAQ,MAAM;CAC7E,MAAM,UAAU,eACR,MAAM,UAAU,aAAa,OAAO,UAAU,MACrD;CAED,MAAM,SAAS,eAAe;AAC5B,MAAI,MAAM,QAAQ,SAChB,QAAO;GACL,cAAc,UAAU,SAAS,MAAM;GACvC,UAAU,UAAU,SAAS,MAAM;GACnC,WAAW,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