You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB

1 month ago
  1. import { computed, inject, ref, useSlots, Text } from 'vue';
  2. import { buttonGroupContextKey } from './constants.mjs';
  3. import { useDeprecated } from '../../../hooks/use-deprecated/index.mjs';
  4. import { useGlobalConfig } from '../../config-provider/src/hooks/use-global-config.mjs';
  5. import { useFormItem } from '../../form/src/hooks/use-form-item.mjs';
  6. import { useFormSize, useFormDisabled } from '../../form/src/hooks/use-form-common-props.mjs';
  7. const useButton = (props, emit) => {
  8. useDeprecated({
  9. from: "type.text",
  10. replacement: "link",
  11. version: "3.0.0",
  12. scope: "props",
  13. ref: "https://element-plus.org/en-US/component/button.html#button-attributes"
  14. }, computed(() => props.type === "text"));
  15. const buttonGroupContext = inject(buttonGroupContextKey, void 0);
  16. const globalConfig = useGlobalConfig("button");
  17. const { form } = useFormItem();
  18. const _size = useFormSize(computed(() => buttonGroupContext == null ? void 0 : buttonGroupContext.size));
  19. const _disabled = useFormDisabled();
  20. const _ref = ref();
  21. const slots = useSlots();
  22. const _type = computed(() => props.type || (buttonGroupContext == null ? void 0 : buttonGroupContext.type) || "");
  23. const autoInsertSpace = computed(() => {
  24. var _a, _b, _c;
  25. return (_c = (_b = props.autoInsertSpace) != null ? _b : (_a = globalConfig.value) == null ? void 0 : _a.autoInsertSpace) != null ? _c : false;
  26. });
  27. const _props = computed(() => {
  28. if (props.tag === "button") {
  29. return {
  30. ariaDisabled: _disabled.value || props.loading,
  31. disabled: _disabled.value || props.loading,
  32. autofocus: props.autofocus,
  33. type: props.nativeType
  34. };
  35. }
  36. return {};
  37. });
  38. const shouldAddSpace = computed(() => {
  39. var _a;
  40. const defaultSlot = (_a = slots.default) == null ? void 0 : _a.call(slots);
  41. if (autoInsertSpace.value && (defaultSlot == null ? void 0 : defaultSlot.length) === 1) {
  42. const slot = defaultSlot[0];
  43. if ((slot == null ? void 0 : slot.type) === Text) {
  44. const text = slot.children;
  45. return /^\p{Unified_Ideograph}{2}$/u.test(text.trim());
  46. }
  47. }
  48. return false;
  49. });
  50. const handleClick = (evt) => {
  51. if (_disabled.value || props.loading) {
  52. evt.stopPropagation();
  53. return;
  54. }
  55. if (props.nativeType === "reset") {
  56. form == null ? void 0 : form.resetFields();
  57. }
  58. emit("click", evt);
  59. };
  60. return {
  61. _disabled,
  62. _size,
  63. _type,
  64. _ref,
  65. _props,
  66. shouldAddSpace,
  67. handleClick
  68. };
  69. };
  70. export { useButton };
  71. //# sourceMappingURL=use-button.mjs.map