Commit bdce84537aa58b9507744a3a14c8d598e88e95fc

Authored by vben
1 parent fb0c7763

perf(button): delete the button component useless code

src/components/Button/index.vue
1 <template> 1 <template>
2 <Button v-bind="getBindValue" :class="[getColor, $attrs.class]"> 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 <template #default="data"> 3 <template #default="data">
7 - <Icon :icon="preIcon" class="mr-1" v-if="preIcon" /> 4 + <Icon :icon="preIcon" :class="{ 'mr-1': !getIsCircleBtn }" v-if="preIcon" />
8 <slot v-bind="data" /> 5 <slot v-bind="data" />
9 - <Icon :icon="postIcon" class="ml-1" v-if="postIcon" /> 6 + <Icon :icon="postIcon" :class="{ 'ml-1': !getIsCircleBtn }" v-if="postIcon" />
10 </template> 7 </template>
11 </Button> 8 </Button>
12 </template> 9 </template>
13 <script lang="ts"> 10 <script lang="ts">
14 import { PropType } from 'vue'; 11 import { PropType } from 'vue';
15 12
16 - import { defineComponent, computed, unref } from 'vue'; 13 + import { defineComponent, computed } from 'vue';
17 import { Button } from 'ant-design-vue'; 14 import { Button } from 'ant-design-vue';
18 // import { extendSlots } from '/@/utils/helper/tsxHelper'; 15 // import { extendSlots } from '/@/utils/helper/tsxHelper';
19 - import { useThrottle } from '/@/hooks/core/useThrottle';  
20 - import { isFunction } from '/@/utils/is'; 16 + // import { useThrottle } from '/@/hooks/core/useThrottle';
  17 + // import { isFunction } from '/@/utils/is';
21 import Icon from '/@/components/Icon'; 18 import Icon from '/@/components/Icon';
22 export default defineComponent({ 19 export default defineComponent({
23 name: 'AButton', 20 name: 'AButton',
@@ -30,18 +27,18 @@ @@ -30,18 +27,18 @@
30 default: 'default', 27 default: 'default',
31 }, 28 },
32 // 节流防抖类型 throttle debounce 29 // 节流防抖类型 throttle debounce
33 - throttle: {  
34 - type: String as PropType<'throttle' | 'debounce'>,  
35 - default: 'throttle',  
36 - }, 30 + // throttle: {
  31 + // type: String as PropType<'throttle' | 'debounce'>,
  32 + // default: 'throttle',
  33 + // },
37 color: { 34 color: {
38 type: String as PropType<'error' | 'warning' | 'success' | ''>, 35 type: String as PropType<'error' | 'warning' | 'success' | ''>,
39 }, 36 },
40 - // 防抖节流时间  
41 - throttleTime: {  
42 - type: Number as PropType<number>,  
43 - default: 0,  
44 - }, 37 + // // 防抖节流时间
  38 + // throttleTime: {
  39 + // type: Number as PropType<number>,
  40 + // default: 50,
  41 + // },
45 loading: { 42 loading: {
46 type: Boolean as PropType<boolean>, 43 type: Boolean as PropType<boolean>,
47 default: false, 44 default: false,
@@ -58,30 +55,38 @@ @@ -58,30 +55,38 @@
58 }, 55 },
59 }, 56 },
60 setup(props, { attrs }) { 57 setup(props, { attrs }) {
61 - const getListeners = computed(() => {  
62 - const { throttle, throttleTime = 0 } = props;  
63 - // 是否开启节流防抖  
64 - const throttleType = throttle!.toLowerCase();  
65 - const isDebounce = throttleType === 'debounce';  
66 - const openThrottle = ['throttle', 'debounce'].includes(throttleType) && throttleTime > 0; 58 + const getIsCircleBtn = computed(() => {
  59 + return attrs.shape === 'circle';
  60 + });
  61 + // const getListeners = computed(() => {
  62 + // const { throttle, throttleTime = 0 } = props;
  63 + // // 是否开启节流防抖
  64 + // const throttleType = throttle!.toLowerCase();
  65 + // const isDebounce = throttleType === 'debounce';
  66 + // const openThrottle = ['throttle', 'debounce'].includes(throttleType) && throttleTime > 0;
  67 + // if (!openThrottle) {
  68 + // return {
  69 + // ...attrs,
  70 + // };
  71 + // }
67 72
68 - const on: {  
69 - onClick?: Fn;  
70 - } = {}; 73 + // const on: {
  74 + // onClick?: Fn;
  75 + // } = {};
71 76
72 - if (attrs.onClick && isFunction(attrs.onClick) && openThrottle) {  
73 - const [handler] = useThrottle(attrs.onClick as any, throttleTime!, {  
74 - debounce: isDebounce,  
75 - immediate: true,  
76 - });  
77 - on.onClick = handler;  
78 - } 77 + // if (attrs.onClick && isFunction(attrs.onClick) && openThrottle) {
  78 + // const [handler] = useThrottle(attrs.onClick as any, throttleTime!, {
  79 + // debounce: isDebounce,
  80 + // immediate: false,
  81 + // });
  82 + // on.onClick = handler;
  83 + // }
79 84
80 - return {  
81 - ...attrs,  
82 - ...on,  
83 - };  
84 - }); 85 + // return {
  86 + // ...attrs,
  87 + // ...on,
  88 + // };
  89 + // });
85 90
86 const getColor = computed(() => { 91 const getColor = computed(() => {
87 const res: string[] = []; 92 const res: string[] = [];
@@ -92,9 +97,10 @@ @@ -92,9 +97,10 @@
92 }); 97 });
93 98
94 const getBindValue = computed((): any => { 99 const getBindValue = computed((): any => {
95 - return { ...unref(getListeners), ...props }; 100 + return { ...attrs, ...props };
96 }); 101 });
97 - return { getBindValue, getColor }; 102 +
  103 + return { getBindValue, getColor, getIsCircleBtn };
98 }, 104 },
99 }); 105 });
100 </script> 106 </script>
src/components/ClickOutSide/index.vue
@@ -12,9 +12,11 @@ @@ -12,9 +12,11 @@
12 12
13 setup(_, { emit }) { 13 setup(_, { emit }) {
14 const wrapRef = ref<Nullable<HTMLDivElement | null>>(null); 14 const wrapRef = ref<Nullable<HTMLDivElement | null>>(null);
  15 +
15 useClickOutside(wrapRef as Ref<HTMLDivElement>, () => { 16 useClickOutside(wrapRef as Ref<HTMLDivElement>, () => {
16 emit('clickOutside'); 17 emit('clickOutside');
17 }); 18 });
  19 +
18 return { wrapRef }; 20 return { wrapRef };
19 }, 21 },
20 }); 22 });
src/components/Scrollbar/src/Bar.tsx
@@ -17,7 +17,7 @@ export default defineComponent({ @@ -17,7 +17,7 @@ export default defineComponent({
17 setup(props) { 17 setup(props) {
18 const thumbRef = ref<Nullable<HTMLDivElement>>(null); 18 const thumbRef = ref<Nullable<HTMLDivElement>>(null);
19 const elRef = ref<Nullable<HTMLDivElement>>(null); 19 const elRef = ref<Nullable<HTMLDivElement>>(null);
20 - const commonState = reactive<KeyString>({}); 20 + const commonState = reactive<Indexable>({});
21 const getBarRef = computed(() => { 21 const getBarRef = computed(() => {
22 return BAR_MAP[props.vertical ? 'vertical' : 'horizontal']; 22 return BAR_MAP[props.vertical ? 'vertical' : 'horizontal'];
23 }); 23 });
src/setup/directives/repeatClick.ts 0 → 100644
  1 +import { on, once } from '/@/utils/domUtils';
  2 +
  3 +export default {
  4 + beforeMount(el: Element, binding: any) {
  5 + let interval: ReturnType<typeof setInterval> | null = null;
  6 + let startTime = 0;
  7 + const handler = () => binding.value && binding.value();
  8 + const clear = () => {
  9 + if (Date.now() - startTime < 100) {
  10 + handler();
  11 + }
  12 + interval && clearInterval(interval);
  13 + interval = null;
  14 + };
  15 +
  16 + on(el, 'mousedown', (e) => {
  17 + if ((e as any).button !== 0) return;
  18 + startTime = Date.now();
  19 + once(document as any, 'mouseup', clear);
  20 + interval && clearInterval(interval);
  21 + interval = setInterval(handler, 100);
  22 + });
  23 + },
  24 +};
src/types/global.d.ts
@@ -23,15 +23,12 @@ declare type RefType&lt;T&gt; = T | null; @@ -23,15 +23,12 @@ declare type RefType&lt;T&gt; = T | null;
23 23
24 declare type CustomizedHTMLElement<T> = HTMLElement & T; 24 declare type CustomizedHTMLElement<T> = HTMLElement & T;
25 25
26 -declare type Indexable<T> = { 26 +declare type Indexable<T = any> = {
27 [key: string]: T; 27 [key: string]: T;
28 }; 28 };
  29 +declare type Hash<T> = Indexable<T>;
29 30
30 -declare type KeyString<T = any> = {  
31 - [key: string]: T;  
32 -};  
33 -  
34 -type DeepPartial<T> = { 31 +declare type DeepPartial<T> = {
35 [P in keyof T]?: T[P] extends (infer U)[] 32 [P in keyof T]?: T[P] extends (infer U)[]
36 ? RecursivePartial<U>[] 33 ? RecursivePartial<U>[]
37 : T[P] extends object 34 : T[P] extends object
@@ -39,11 +36,11 @@ type DeepPartial&lt;T&gt; = { @@ -39,11 +36,11 @@ type DeepPartial&lt;T&gt; = {
39 : T[P]; 36 : T[P];
40 }; 37 };
41 38
42 -type SelectOptions = { 39 +declare type SelectOptions = {
43 label: string; 40 label: string;
44 value: any; 41 value: any;
45 }[]; 42 }[];
46 43
47 -type EmitType = (event: string, ...args: any[]) => void; 44 +declare type EmitType = (event: string, ...args: any[]) => void;
48 45
49 -type TargetContext = '_self' | '_blank'; 46 +declare type TargetContext = '_self' | '_blank';
src/utils/domUtils.ts
@@ -15,9 +15,11 @@ export function getBoundingClientRect(element: Element): DOMRect | number { @@ -15,9 +15,11 @@ export function getBoundingClientRect(element: Element): DOMRect | number {
15 } 15 }
16 return element.getBoundingClientRect(); 16 return element.getBoundingClientRect();
17 } 17 }
  18 +
18 const trim = function (string: string) { 19 const trim = function (string: string) {
19 return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, ''); 20 return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
20 }; 21 };
  22 +
21 /* istanbul ignore next */ 23 /* istanbul ignore next */
22 export function hasClass(el: Element, cls: string) { 24 export function hasClass(el: Element, cls: string) {
23 if (!el || !cls) return false; 25 if (!el || !cls) return false;
@@ -28,6 +30,7 @@ export function hasClass(el: Element, cls: string) { @@ -28,6 +30,7 @@ export function hasClass(el: Element, cls: string) {
28 return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1; 30 return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
29 } 31 }
30 } 32 }
  33 +
31 /* istanbul ignore next */ 34 /* istanbul ignore next */
32 export function addClass(el: Element, cls: string) { 35 export function addClass(el: Element, cls: string) {
33 if (!el) return; 36 if (!el) return;
@@ -130,7 +133,7 @@ export function hackCss(attr: string, value: string) { @@ -130,7 +133,7 @@ export function hackCss(attr: string, value: string) {
130 133
131 /* istanbul ignore next */ 134 /* istanbul ignore next */
132 export const on = function ( 135 export const on = function (
133 - element: HTMLElement | Document | Window, 136 + element: Element | HTMLElement | Document | Window,
134 event: string, 137 event: string,
135 handler: EventListenerOrEventListenerObject 138 handler: EventListenerOrEventListenerObject
136 ): void { 139 ): void {
@@ -141,7 +144,7 @@ export const on = function ( @@ -141,7 +144,7 @@ export const on = function (
141 144
142 /* istanbul ignore next */ 145 /* istanbul ignore next */
143 export const off = function ( 146 export const off = function (
144 - element: HTMLElement | Document | Window, 147 + element: Element | HTMLElement | Document | Window,
145 event: string, 148 event: string,
146 handler: Fn 149 handler: Fn
147 ): void { 150 ): void {
@@ -149,3 +152,14 @@ export const off = function ( @@ -149,3 +152,14 @@ export const off = function (
149 element.removeEventListener(event, handler, false); 152 element.removeEventListener(event, handler, false);
150 } 153 }
151 }; 154 };
  155 +
  156 +/* istanbul ignore next */
  157 +export const once = function (el: HTMLElement, event: string, fn: EventListener): void {
  158 + const listener = function (this: any, ...args: unknown[]) {
  159 + if (fn) {
  160 + fn.apply(this, args);
  161 + }
  162 + off(el, event, listener);
  163 + };
  164 + on(el, event, listener);
  165 +};