Skip to content

Commit

Permalink
feat(hooks): add @sa/hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Oct 19, 2023
1 parent 0c66695 commit 7677af5
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@iconify/vue": "^4.1.1",
"@sa/hooks": "workspace:*",
"@sa/materials": "workspace:*",
"@sa/utils": "workspace:*",
"@vueuse/core": "10.5.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@sa/hooks",
"version": "2.0.0",
"exports": {
".": "./src/index.ts"
},
"typesVersions": {
"*": {
"*": [
"./src/*"
]
}
}
}
5 changes: 5 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import useBoolean from './use-boolean';
import useLoading from './use-loading';
import useContext from './use-context';

export { useBoolean, useLoading, useContext };
30 changes: 30 additions & 0 deletions packages/hooks/src/use-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ref } from 'vue';

/**
* boolean
* @param initValue init value
*/
export default function useBoolean(initValue = false) {
const bool = ref(initValue);

function setBool(value: boolean) {
bool.value = value;
}
function setTrue() {
setBool(true);
}
function setFalse() {
setBool(false);
}
function toggle() {
setBool(!bool.value);
}

return {
bool,
setBool,
setTrue,
setFalse,
toggle
};
}
108 changes: 108 additions & 0 deletions packages/hooks/src/use-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { inject, provide } from 'vue';
import type { InjectionKey } from 'vue';

type ContextFn<T> = () => T;

/**
* use context
* @param contextName context name
* @param fn context function
* @example
* ```ts
* // there are three vue files: A.vue, B.vue, C.vue, and A.vue is the parent component of B.vue and C.vue
*
* // context.ts
* import { ref } from 'vue';
* import { useContext } from '@sa/hooks';
*
* export const { setupStore, useStore } = useContext('demo', () => {
* const count = ref(0);
*
* function increment() {
* count.value++;
* }
*
* function decrement() {
* count.value--;
* }
*
* return {
* count,
* increment,
* decrement
* };
* })
* ```
*
* // A.vue
* ```vue
* <template>
* <div>A</div>
* </template>
* <script setup lang="ts">
* import { setupStore } from './context';
*
* setupStore();
* // const { increment } = setupStore(); // also can control the store in the parent component
* </script>
* ```
* // B.vue
* ```vue
* <template>
* <div>B</div>
* </template>
* <script setup lang="ts">
* import { useStore } from './context';
*
* const { count, increment } = useStore();
* </script>
* ```
*
* // C.vue is same as B.vue
*/
export default function useContext<T>(contextName: string, fn: ContextFn<T>) {
const { useProvide, useInject } = createContext<T>(contextName);

const context = fn();

/**
* setup store in the parent component
*/
function setupStore() {
return useProvide(context);
}

/**
* use store in the child component
*/
function useStore() {
return useInject();
}

return {
setupStore,
useStore
};
}

/**
* create context
*/
function createContext<T>(contextName: string) {
const injectKey: InjectionKey<T> = Symbol(contextName);

function useProvide(context: T) {
provide(injectKey, context);

return context;
}

function useInject() {
return inject(injectKey) as T;
}

return {
useProvide,
useInject
};
}
15 changes: 15 additions & 0 deletions packages/hooks/src/use-loading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import useBoolean from './use-boolean';

/**
* loading
* @param initValue init value
*/
export default function useLoading(initValue = false) {
const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(initValue);

return {
loading,
startLoading,
endLoading
};
}
20 changes: 20 additions & 0 deletions packages/hooks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"baseUrl": ".",
"module": "ESNext",
"target": "ESNext",
"lib": ["DOM", "ESNext"],
"strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"moduleResolution": "node",
"resolveJsonModule": true,
"noUnusedLocals": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"types": ["node"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
1 change: 1 addition & 0 deletions src/router/elegant/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const views: Record<LastLevelRouteKey, RouteComponent | (() => Promise<Ro
home: () => import("@/views/home/index.vue"),
"multi-menu_first_child": () => import("@/views/multi-menu/first_child/index.vue"),
"multi-menu_second_child_home": () => import("@/views/multi-menu/second_child_home/index.vue"),
"user-center": () => import("@/views/user-center/index.vue"),
user_detail: () => import("@/views/user/detail/[id].vue"),
user_list: () => import("@/views/user/list/index.vue"),
};
14 changes: 14 additions & 0 deletions src/router/elegant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,19 @@ export const autoRoutes: ElegantRoute[] = [
}
}
]
},
{
path: '/user-center',
component: 'layout.base',
children: [
{
name: 'user-center',
path: '',
component: 'view.user-center',
meta: {
title: 'user-center'
}
}
]
}
];
3 changes: 3 additions & 0 deletions src/typings/elegant-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare module "@elegant-router/types" {
"user": "/user";
"user_detail": "/user/detail/:id";
"user_list": "/user/list";
"user-center": "/user-center";
};

/**
Expand Down Expand Up @@ -64,6 +65,7 @@ declare module "@elegant-router/types" {
| "login"
| "multi-menu"
| "user"
| "user-center"
>;

/**
Expand All @@ -78,6 +80,7 @@ declare module "@elegant-router/types" {
| "home"
| "multi-menu_first_child"
| "multi-menu_second_child_home"
| "user-center"
| "user_detail"
| "user_list"
>;
Expand Down
7 changes: 7 additions & 0 deletions src/views/user-center/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts"></script>

<template>
<LookForward />
</template>

<style scoped></style>

0 comments on commit 7677af5

Please sign in to comment.