Skip to content

Commit

Permalink
refactor: refactor hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Apr 10, 2021
1 parent 215d8ba commit 2037293
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 394 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
## Wip

## (破坏性更新) Breaking changes

- 使用 `pinia` 替换 `vuex`,`vuex-module-decorators`

- 影响,之前如果有自己使用 vuex-module-decorators,需要改造为 pinia。
- 原因:
- pinia 于 vuex5api 基本类似,且简单易懂。
- 后续切换 vuex5 成本非常低,也可以当作第三方状态管理库使用

- 移除 `useKeyPress` 使用`vueuse`-`onKeyStroke`代替
- 移除 `useDebounceFn` 使用`vueuse`-`useDebounceFn`代替
- 移除 `useThrottle` 使用`vueuse`-`useThrottleFn`代替

### ✨ Refactor

- 移除 `useElResize`

### 🐛 Bug Fixes

- 登录页样式修复
Expand Down
27 changes: 6 additions & 21 deletions src/components/Application/src/search/useMenuSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import type { Menu } from '/@/router/types';
import { ref, onBeforeMount, unref, Ref, nextTick } from 'vue';

import { getMenus } from '/@/router/menus';
import { KeyCodeEnum } from '/@/enums/keyCodeEnum';

import { cloneDeep } from 'lodash-es';
import { filter, forEach } from '/@/utils/helper/treeHelper';

import { useDebounce } from '/@/hooks/core/useDebounce';
import { useGo } from '/@/hooks/web/usePage';
import { useScrollTo } from '/@/hooks/event/useScrollTo';
import { useKeyPress } from '/@/hooks/event/useKeyPress';
import { onKeyStroke, useDebounceFn } from '@vueuse/core';
import { useI18n } from '/@/hooks/web/useI18n';

export interface SearchResult {
Expand Down Expand Up @@ -41,7 +39,7 @@ export function useMenuSearch(refs: Ref<HTMLElement[]>, scrollWrap: Ref<ElRef>,

const { t } = useI18n();
const go = useGo();
const [handleSearch] = useDebounce(search, 200);
const handleSearch = useDebounceFn(search, 200);

onBeforeMount(async () => {
const list = await getMenus();
Expand Down Expand Up @@ -146,23 +144,10 @@ export function useMenuSearch(refs: Ref<HTMLElement[]>, scrollWrap: Ref<ElRef>,
emit('close');
}

useKeyPress(['enter', 'up', 'down', 'esc'], (events) => {
const keyCode = events.keyCode;
switch (keyCode) {
case KeyCodeEnum.UP:
handleUp();
break;
case KeyCodeEnum.DOWN:
handleDown();
break;
case KeyCodeEnum.ENTER:
handleEnter();
break;
case KeyCodeEnum.ESC:
handleClose();
break;
}
});
onKeyStroke('Enter', handleEnter);
onKeyStroke('ArrowUp', handleUp);
onKeyStroke('ArrowDown', handleDown);
onKeyStroke('Escape', handleClose);

return { handleSearch, searchResult, keyword, activeIndex, handleMouseenter, handleEnter };
}
4 changes: 2 additions & 2 deletions src/components/Icon/src/IconPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
import iconsData from '../data/icons.data';
import { propTypes } from '/@/utils/propTypes';
import { usePagination } from '/@/hooks/web/usePagination';
import { useDebounce } from '/@/hooks/core/useDebounce';
import { useDebounceFn } from '@vueuse/core';
import { useI18n } from '/@/hooks/web/useI18n';
import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
import { useMessage } from '/@/hooks/web/useMessage';
Expand Down Expand Up @@ -123,7 +123,7 @@
const { t } = useI18n();
const { prefixCls } = useDesign('icon-picker');
const [debounceHandleSearchChange] = useDebounce(handleSearchChange, 100);
const debounceHandleSearchChange = useDebounceFn(handleSearchChange, 100);
const { clipboardRef, isSuccessRef } = useCopyToClipboard(props.value);
const { createMessage } = useMessage();
Expand Down
1 change: 0 additions & 1 deletion src/components/Modal/src/components/ModalWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import { ScrollContainer } from '/@/components/Container';
// import { useElResize } from '/@/hooks/event/useElResize';
import { propTypes } from '/@/utils/propTypes';
import { createModalContext } from '../hooks/useModalContext';
Expand Down
4 changes: 2 additions & 2 deletions src/components/SimpleMenu/src/useOpenKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { uniq } from 'lodash-es';
import { getAllParentPath } from '/@/router/helper/menuHelper';

import { useTimeoutFn } from '/@/hooks/core/useTimeout';
import { useDebounce } from '/@/hooks/core/useDebounce';
import { useDebounceFn } from '@vueuse/core';

export function useOpenKeys(
menuState: MenuState,
Expand All @@ -17,7 +17,7 @@ export function useOpenKeys(
mixSider: Ref<boolean>,
collapse: Ref<boolean>
) {
const [debounceSetOpenKeys] = useDebounce(setOpenKeys, 50);
const debounceSetOpenKeys = useDebounceFn(setOpenKeys, 50);
async function setOpenKeys(path: string) {
const native = !mixSider.value;
const menuList = toRaw(menus.value);
Expand Down
8 changes: 4 additions & 4 deletions src/components/Table/src/hooks/useTableScroll.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { BasicTableProps, TableRowSelection } from '../types/table';
import type { BasicTableProps, TableRowSelection, BasicColumn } from '../types/table';
import type { Ref, ComputedRef } from 'vue';

import { computed, unref, ref, nextTick, watch } from 'vue';

import { getViewportOffset } from '/@/utils/domUtils';
import { isBoolean } from '/@/utils/is';

import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import { useModalContext } from '/@/components/Modal';
import { useDebounce } from '/@/hooks/core/useDebounce';
import type { BasicColumn } from '/@/components/Table';
import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
import { useDebounceFn } from '@vueuse/core';

export function useTableScroll(
propsRef: ComputedRef<BasicTableProps>,
Expand All @@ -23,7 +23,7 @@ export function useTableScroll(
const modalFn = useModalContext();

// Greater than animation time 280
const [debounceRedoHeight] = useDebounce(redoHeight, 100);
const debounceRedoHeight = useDebounceFn(redoHeight, 100);

const getCanResize = computed(() => {
const { canResize, scroll } = unref(propsRef);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Tree/src/TreeHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import { propTypes } from '/@/utils/propTypes';
import { useI18n } from '/@/hooks/web/useI18n';
import { useDebounce } from '/@/hooks/core/useDebounce';
import { useDebounceFn } from '@vueuse/core';
import { ToolbarEnum } from './enum';
Expand Down Expand Up @@ -128,7 +128,7 @@
function emitChange(value?: string): void {
emit('search', value);
}
const [debounceEmitChange] = useDebounce(emitChange, 200);
const debounceEmitChange = useDebounceFn(emitChange, 200);
function handleSearch(e: ChangeEvent): void {
debounceEmitChange(e.target.value);
Expand Down
6 changes: 0 additions & 6 deletions src/enums/keyCodeEnum.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/hooks/core/useContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,7 @@ export function createContext<T>(
const provideData = readonly ? defineReadonly(state) : state;
!createProvider && provide(key, native ? context : provideData);

// const Provider = createProvider
// ? defineComponent({
// name: 'Provider',
// inheritAttrs: false,
// setup(_, { slots }) {
// provide(key, provideData);
// return () => slots.default?.();
// },
// })
// : null;

return {
// Provider,
state,
};
}
Expand Down
39 changes: 0 additions & 39 deletions src/hooks/core/useDebounce.ts

This file was deleted.

84 changes: 0 additions & 84 deletions src/hooks/core/useThrottle.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/hooks/event/useElResize.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/hooks/event/useEventListener.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Ref } from 'vue';

import { ref, watch, unref } from 'vue';
import { useDebounce } from '/@/hooks/core/useDebounce';
import { useThrottle } from '/@/hooks/core/useThrottle';
import { useThrottleFn, useDebounceFn } from '@vueuse/core';

export type RemoveEventFn = () => void;

Expand Down Expand Up @@ -31,7 +30,7 @@ export function useEventListener({
if (el) {
const element: Ref<Element> = ref(el as Element);

const [handler] = isDebounce ? useDebounce(listener, wait) : useThrottle(listener, wait);
const handler = isDebounce ? useDebounceFn(listener, wait) : useThrottleFn(listener, wait);
const realHandler = wait ? handler : listener;
const removeEventListener = (e: Element) => {
isAddRef.value = true;
Expand Down
Loading

0 comments on commit 2037293

Please sign in to comment.