index.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <Button v-bind="getBindValue" :class="[getColor, $attrs.class]">
  3. <template #[item]="data" v-for="item in Object.keys($slots)">
  4. <slot :name="item" v-bind="data" />
  5. </template>
  6. </Button>
  7. </template>
  8. <script lang="ts">
  9. import { PropType } from 'vue';
  10. import { defineComponent, computed, unref } from 'vue';
  11. import { Button } from 'ant-design-vue';
  12. // import { extendSlots } from '/@/utils/helper/tsxHelper';
  13. import { useThrottle } from '/@/hooks/core/useThrottle';
  14. import { isFunction } from '/@/utils/is';
  15. export default defineComponent({
  16. name: 'AButton',
  17. inheritAttrs: false,
  18. components: { Button },
  19. props: {
  20. // 按钮类型
  21. type: {
  22. type: String as PropType<'primary' | 'default' | 'danger' | 'dashed' | 'link'>,
  23. default: 'default',
  24. },
  25. // 节流防抖类型 throttle debounce
  26. throttle: {
  27. type: String as PropType<'throttle' | 'debounce'>,
  28. default: 'throttle',
  29. },
  30. color: {
  31. type: String as PropType<'error' | 'warning' | 'success'>,
  32. },
  33. // 防抖节流时间
  34. throttleTime: {
  35. type: Number as PropType<number>,
  36. default: 0,
  37. },
  38. loading: {
  39. type: Boolean as PropType<boolean>,
  40. default: false,
  41. },
  42. disabled: {
  43. type: Boolean as PropType<boolean>,
  44. default: false,
  45. },
  46. },
  47. setup(props, { attrs }) {
  48. const getListeners = computed(() => {
  49. const { throttle, throttleTime = 0 } = props;
  50. // 是否开启节流防抖
  51. const throttleType = throttle!.toLowerCase();
  52. const isDebounce = throttleType === 'debounce';
  53. const openThrottle = ['throttle', 'debounce'].includes(throttleType) && throttleTime > 0;
  54. const on: {
  55. onClick?: Fn;
  56. } = {};
  57. if (attrs.onClick && isFunction(attrs.onClick) && openThrottle) {
  58. const [handler] = useThrottle(attrs.onClick as any, throttleTime!, {
  59. debounce: isDebounce,
  60. immediate: true,
  61. });
  62. on.onClick = handler;
  63. }
  64. return {
  65. ...attrs,
  66. ...on,
  67. };
  68. });
  69. const getColor = computed(() => {
  70. const res: string[] = [];
  71. const { color, disabled } = props;
  72. color && res.push(`ant-btn-${color}`);
  73. disabled && res.push('is-disabled');
  74. return res;
  75. });
  76. const getBindValue = computed((): any => {
  77. return { ...unref(getListeners), ...props };
  78. });
  79. return { getBindValue, getColor };
  80. },
  81. });
  82. </script>