@maxax/hooks 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +899 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +443 -0
- package/dist/index.d.ts +443 -0
- package/dist/index.mjs +846 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { ComputedRef, Ref, InjectionKey, CSSProperties, WritableComputedRef, ShallowRef, ComputedGetter, DebuggerOptions, WritableComputedOptions, Component } from 'vue';
|
|
3
|
+
import { VisibleDomRect } from '@maxax/utils';
|
|
4
|
+
import { AxiosError, ResponseType, CustomAxiosRequestConfig, MappedType } from '@maxax/axios';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Boolean
|
|
8
|
+
*
|
|
9
|
+
* @param initValue Init value
|
|
10
|
+
*/
|
|
11
|
+
declare function useBoolean(initValue?: boolean): {
|
|
12
|
+
bool: vue.Ref<boolean, boolean>;
|
|
13
|
+
setBool: (value: boolean) => void;
|
|
14
|
+
setTrue: () => void;
|
|
15
|
+
setFalse: () => void;
|
|
16
|
+
toggle: () => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
declare enum SizeEnum {
|
|
20
|
+
XS = "XS",
|
|
21
|
+
SM = "SM",
|
|
22
|
+
MD = "MD",
|
|
23
|
+
LG = "LG",
|
|
24
|
+
XL = "XL",
|
|
25
|
+
XXL = "XXL"
|
|
26
|
+
}
|
|
27
|
+
declare enum ScreenEnum {
|
|
28
|
+
XS = 320,
|
|
29
|
+
SM = 640,
|
|
30
|
+
MD = 768,
|
|
31
|
+
LG = 960,
|
|
32
|
+
XL = 1280,
|
|
33
|
+
XXL = 1536
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface CreateCallbackParams {
|
|
37
|
+
screen: ComputedRef<SizeEnum | undefined>;
|
|
38
|
+
width: ComputedRef<number>;
|
|
39
|
+
realWidth: ComputedRef<number>;
|
|
40
|
+
screenEnum: typeof ScreenEnum;
|
|
41
|
+
screenMap: Map<SizeEnum, number>;
|
|
42
|
+
sizeEnum: typeof SizeEnum;
|
|
43
|
+
}
|
|
44
|
+
declare function useBreakpoint(): {
|
|
45
|
+
screenRef: ComputedRef<SizeEnum | undefined>;
|
|
46
|
+
widthRef: ComputedRef<number>;
|
|
47
|
+
screenEnum: typeof ScreenEnum;
|
|
48
|
+
realWidthRef: ComputedRef<number>;
|
|
49
|
+
};
|
|
50
|
+
declare function createBreakpointListen(fn?: (opt: CreateCallbackParams) => void): {
|
|
51
|
+
screenRef: ComputedRef<SizeEnum | undefined>;
|
|
52
|
+
screenEnum: typeof ScreenEnum;
|
|
53
|
+
widthRef: ComputedRef<number>;
|
|
54
|
+
realWidthRef: ComputedRef<number>;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
interface WindowLocation {
|
|
58
|
+
hash?: string;
|
|
59
|
+
host?: string;
|
|
60
|
+
hostname?: string;
|
|
61
|
+
href?: string;
|
|
62
|
+
origin?: string;
|
|
63
|
+
pathname?: string;
|
|
64
|
+
port?: string;
|
|
65
|
+
protocol?: string;
|
|
66
|
+
search?: string;
|
|
67
|
+
}
|
|
68
|
+
declare function useBrowserLocation(customWindow?: (Window & typeof globalThis) | null): Ref<WindowLocation>;
|
|
69
|
+
|
|
70
|
+
declare function useInjectionInstanceCollection(injectionName: string | InjectionKey<unknown>, collectionKey: string, registerKeyRef: Ref<string | undefined>): void;
|
|
71
|
+
declare function useInjectionCollection(injectionName: string | InjectionKey<unknown>, collectionKey: string, valueRef: Ref<any>): void;
|
|
72
|
+
declare function useInjectionElementCollection(injectionName: string | InjectionKey<unknown>, collectionKey: string, getElement: () => HTMLElement | null): void;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Use context
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // there are three vue files: A.vue, B.vue, C.vue, and A.vue is the parent component of B.vue and C.vue
|
|
80
|
+
*
|
|
81
|
+
* // context.ts
|
|
82
|
+
* import { ref } from 'vue';
|
|
83
|
+
* import { useContext } from '@max/hooks';
|
|
84
|
+
*
|
|
85
|
+
* export const { setupStore, useStore } = useContext('demo', () => {
|
|
86
|
+
* const count = ref(0);
|
|
87
|
+
*
|
|
88
|
+
* function increment() {
|
|
89
|
+
* count.value++;
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* function decrement() {
|
|
93
|
+
* count.value--;
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* return {
|
|
97
|
+
* count,
|
|
98
|
+
* increment,
|
|
99
|
+
* decrement
|
|
100
|
+
* };
|
|
101
|
+
* })
|
|
102
|
+
* ``` // A.vue
|
|
103
|
+
* ```vue
|
|
104
|
+
* <template>
|
|
105
|
+
* <div>A</div>
|
|
106
|
+
* </template>
|
|
107
|
+
* <script setup lang="ts">
|
|
108
|
+
* import { setupStore } from './context';
|
|
109
|
+
*
|
|
110
|
+
* setupStore();
|
|
111
|
+
* // const { increment } = setupStore(); // also can control the store in the parent component
|
|
112
|
+
* </script>
|
|
113
|
+
* ``` // B.vue
|
|
114
|
+
* ```vue
|
|
115
|
+
* <template>
|
|
116
|
+
* <div>B</div>
|
|
117
|
+
* </template>
|
|
118
|
+
* <script setup lang="ts">
|
|
119
|
+
* import { useStore } from './context';
|
|
120
|
+
*
|
|
121
|
+
* const { count, increment } = useStore();
|
|
122
|
+
* </script>
|
|
123
|
+
* ```;
|
|
124
|
+
*
|
|
125
|
+
* // C.vue is same as B.vue
|
|
126
|
+
*
|
|
127
|
+
* @param contextName Context name
|
|
128
|
+
* @param fn Context function
|
|
129
|
+
*/
|
|
130
|
+
declare function useContext<T extends (...args: any[]) => any>(contextName: string, fn: T): {
|
|
131
|
+
/** Setup store in the parent component */
|
|
132
|
+
setupStore: (...args: Parameters<T>) => ReturnType<T>;
|
|
133
|
+
/** Use store in the child component */
|
|
134
|
+
useStore: () => ReturnType<T>;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Count down
|
|
139
|
+
*
|
|
140
|
+
* @param seconds - count down seconds
|
|
141
|
+
*/
|
|
142
|
+
declare function useCountDown(seconds: number): {
|
|
143
|
+
count: vue.ComputedRef<number>;
|
|
144
|
+
isCounting: vue.ComputedRef<boolean>;
|
|
145
|
+
start: (updateSeconds?: number) => void;
|
|
146
|
+
stop: () => void;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
declare function useDeferredTrue(valueRef: Ref<any>, delay: number, shouldDelayRef: Ref<boolean>): Ref<boolean>;
|
|
150
|
+
|
|
151
|
+
type RemoveEventFn = () => void;
|
|
152
|
+
interface UseEventParams {
|
|
153
|
+
el?: Element | Ref<Element | undefined> | Window | any;
|
|
154
|
+
name: string;
|
|
155
|
+
listener: EventListener;
|
|
156
|
+
options?: boolean | AddEventListenerOptions;
|
|
157
|
+
autoRemove?: boolean;
|
|
158
|
+
isDebounce?: boolean;
|
|
159
|
+
wait?: number;
|
|
160
|
+
}
|
|
161
|
+
declare function useEventListener({ el, name, listener, options, autoRemove, isDebounce, wait }: UseEventParams): {
|
|
162
|
+
removeEvent: RemoveEventFn;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
declare function useIsComposing(): Ref<boolean>;
|
|
166
|
+
|
|
167
|
+
declare function useLayoutContentStyle(diffHeight?: number): {
|
|
168
|
+
contentElement: vue.Ref<HTMLDivElement | null, HTMLDivElement | null>;
|
|
169
|
+
overlayStyle: vue.ComputedRef<CSSProperties>;
|
|
170
|
+
visibleDomRect: vue.Ref<{
|
|
171
|
+
bottom: number;
|
|
172
|
+
height: number;
|
|
173
|
+
left: number;
|
|
174
|
+
right: number;
|
|
175
|
+
top: number;
|
|
176
|
+
width: number;
|
|
177
|
+
} | null, VisibleDomRect | {
|
|
178
|
+
bottom: number;
|
|
179
|
+
height: number;
|
|
180
|
+
left: number;
|
|
181
|
+
right: number;
|
|
182
|
+
top: number;
|
|
183
|
+
width: number;
|
|
184
|
+
} | null>;
|
|
185
|
+
};
|
|
186
|
+
declare function useLayoutHeaderStyle(): {
|
|
187
|
+
getLayoutHeaderHeight: () => number;
|
|
188
|
+
setLayoutHeaderHeight: (height: number) => void;
|
|
189
|
+
};
|
|
190
|
+
declare function useLayoutFooterStyle(): {
|
|
191
|
+
getLayoutFooterHeight: () => number;
|
|
192
|
+
setLayoutFooterHeight: (height: number) => void;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Loading
|
|
197
|
+
*
|
|
198
|
+
* @param initValue Init value
|
|
199
|
+
*/
|
|
200
|
+
declare function useLoading(initValue?: boolean): {
|
|
201
|
+
loading: vue.Ref<boolean, boolean>;
|
|
202
|
+
startLoading: () => void;
|
|
203
|
+
endLoading: () => void;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
declare function useLocalPage(list: any[], pageSize: number): {
|
|
207
|
+
setCurrentPage: (page: number) => void;
|
|
208
|
+
getTotal: vue.ComputedRef<number>;
|
|
209
|
+
setPageSize: (size: number) => void;
|
|
210
|
+
getPaginationList: vue.ComputedRef<any[]>;
|
|
211
|
+
setPagination: (page: {
|
|
212
|
+
currentPage: number;
|
|
213
|
+
pageSize: number;
|
|
214
|
+
}) => void;
|
|
215
|
+
getPageList: () => any[];
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
declare const lockHtmlScrollRightCompensationRef: Ref<string, string>;
|
|
219
|
+
declare function useLockHtmlScroll(lockRef: Ref<boolean>): void;
|
|
220
|
+
|
|
221
|
+
type MemoGetter<T> = () => T;
|
|
222
|
+
type MemoSetter<T> = (v: T) => void;
|
|
223
|
+
interface WritableMemoOptions<T> {
|
|
224
|
+
get: MemoGetter<T>;
|
|
225
|
+
set: MemoSetter<T>;
|
|
226
|
+
}
|
|
227
|
+
declare function useMemo<T>(getter: MemoGetter<T>): ComputedRef<T>;
|
|
228
|
+
declare function useMemo<T>(options: WritableMemoOptions<T>): WritableComputedRef<T>;
|
|
229
|
+
|
|
230
|
+
declare function useMergedState<T>(controlledStateRef: Ref<T | undefined>, uncontrolledStateRef: Ref<T>): ComputedRef<T>;
|
|
231
|
+
|
|
232
|
+
interface HookRequestInstanceResponseSuccessData<T = any> {
|
|
233
|
+
data: Ref<T>;
|
|
234
|
+
error: Ref<null>;
|
|
235
|
+
}
|
|
236
|
+
interface HookRequestInstanceResponseFailData<ResponseData = any> {
|
|
237
|
+
data: Ref<null>;
|
|
238
|
+
error: Ref<AxiosError<ResponseData>>;
|
|
239
|
+
}
|
|
240
|
+
type HookRequestInstanceResponseData<T = any, ResponseData = any> = {
|
|
241
|
+
loading: Ref<boolean>;
|
|
242
|
+
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<ResponseData>);
|
|
243
|
+
interface HookRequestInstance<ResponseData = any> {
|
|
244
|
+
<T = any, R extends ResponseType = 'json'>(config: CustomAxiosRequestConfig): HookRequestInstanceResponseData<MappedType<R, T>, ResponseData>;
|
|
245
|
+
cancelRequest: (requestId: string) => void;
|
|
246
|
+
cancelAllRequest: () => void;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
interface ScriptOptions {
|
|
250
|
+
src: string;
|
|
251
|
+
}
|
|
252
|
+
declare function useScript(opts: ScriptOptions): {
|
|
253
|
+
isLoading: vue.Ref<boolean, boolean>;
|
|
254
|
+
error: vue.Ref<boolean, boolean>;
|
|
255
|
+
success: vue.Ref<boolean, boolean>;
|
|
256
|
+
toPromise: () => Promise<unknown>;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
declare function useScroll(refEl: Ref<Element | Window | null>, options?: {
|
|
260
|
+
wait?: number;
|
|
261
|
+
leading?: boolean;
|
|
262
|
+
trailing?: boolean;
|
|
263
|
+
}): {
|
|
264
|
+
refX: Ref<number, number>;
|
|
265
|
+
refY: Ref<number, number>;
|
|
266
|
+
stop: () => void;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
type Updater<T> = (value: T) => T;
|
|
270
|
+
type Mutator<T> = (value: T) => void;
|
|
271
|
+
/**
|
|
272
|
+
* Signal is a reactive value that can be set, updated or mutated
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* const count = useSignal(0);
|
|
277
|
+
*
|
|
278
|
+
* // `watchEffect`
|
|
279
|
+
* watchEffect(() => {
|
|
280
|
+
* console.log(count());
|
|
281
|
+
* });
|
|
282
|
+
*
|
|
283
|
+
* // watch
|
|
284
|
+
* watch(count, value => {
|
|
285
|
+
* console.log(value);
|
|
286
|
+
* });
|
|
287
|
+
*
|
|
288
|
+
* // useComputed
|
|
289
|
+
* const double = useComputed(() => count() * 2);
|
|
290
|
+
* const writeableDouble = useComputed({
|
|
291
|
+
* get: () => count() * 2,
|
|
292
|
+
* set: value => count.set(value / 2)
|
|
293
|
+
* });
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
interface Signal<T> {
|
|
297
|
+
(): Readonly<T>;
|
|
298
|
+
/**
|
|
299
|
+
* Set the value of the signal
|
|
300
|
+
*
|
|
301
|
+
* It recommend use `set` for primitive values
|
|
302
|
+
*
|
|
303
|
+
* @param value
|
|
304
|
+
*/
|
|
305
|
+
set: (value: T) => void;
|
|
306
|
+
/**
|
|
307
|
+
* Update the value of the signal using an updater function
|
|
308
|
+
*
|
|
309
|
+
* It recommend use `update` for non-primitive values, only the first level of the object will be reactive.
|
|
310
|
+
*
|
|
311
|
+
* @param updater
|
|
312
|
+
*/
|
|
313
|
+
update: (updater: Updater<T>) => void;
|
|
314
|
+
/**
|
|
315
|
+
* Mutate the value of the signal using a mutator function
|
|
316
|
+
*
|
|
317
|
+
* this action will call `triggerRef`, so the value will be tracked on `watchEffect`.
|
|
318
|
+
*
|
|
319
|
+
* It recommend use `mutate` for non-primitive values, all levels of the object will be reactive.
|
|
320
|
+
*
|
|
321
|
+
* @param mutator
|
|
322
|
+
*/
|
|
323
|
+
mutate: (mutator: Mutator<T>) => void;
|
|
324
|
+
/**
|
|
325
|
+
* Get the reference of the signal
|
|
326
|
+
*
|
|
327
|
+
* Sometimes it can be useful to make `v-model` work with the signal
|
|
328
|
+
*
|
|
329
|
+
* ```vue
|
|
330
|
+
* <template>
|
|
331
|
+
* <input v-model="model.count" />
|
|
332
|
+
* </template>;
|
|
333
|
+
*
|
|
334
|
+
* <script setup lang="ts">
|
|
335
|
+
* const state = useSignal({ count: 0 }, { useRef: true });
|
|
336
|
+
*
|
|
337
|
+
* const model = state.getRef();
|
|
338
|
+
* </script>
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
getRef: () => Readonly<ShallowRef<Readonly<T>>>;
|
|
342
|
+
}
|
|
343
|
+
interface ReadonlySignal<T> {
|
|
344
|
+
(): Readonly<T>;
|
|
345
|
+
}
|
|
346
|
+
interface SignalOptions {
|
|
347
|
+
/**
|
|
348
|
+
* Whether to use `ref` to store the value
|
|
349
|
+
*
|
|
350
|
+
* @default false use `sharedRef` to store the value
|
|
351
|
+
*/
|
|
352
|
+
useRef?: boolean;
|
|
353
|
+
}
|
|
354
|
+
declare function useSignal<T>(initialValue: T, options?: SignalOptions): Signal<T>;
|
|
355
|
+
declare function useComputed<T>(getter: ComputedGetter<T>, debugOptions?: DebuggerOptions): ReadonlySignal<T>;
|
|
356
|
+
declare function useComputed<T>(options: WritableComputedOptions<T>, debugOptions?: DebuggerOptions): Signal<T>;
|
|
357
|
+
|
|
358
|
+
interface IconConfig {
|
|
359
|
+
/** Iconify icon name */
|
|
360
|
+
icon?: string;
|
|
361
|
+
/** Local icon name */
|
|
362
|
+
localIcon?: string;
|
|
363
|
+
/** Icon color */
|
|
364
|
+
color?: string;
|
|
365
|
+
/** Icon size */
|
|
366
|
+
fontSize?: number;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Svg icon render hook
|
|
370
|
+
*
|
|
371
|
+
* @param SvgIcon Svg icon component
|
|
372
|
+
*/
|
|
373
|
+
declare function useSvgIconRender(SvgIcon: Component): {
|
|
374
|
+
SvgIconVNode: (config: IconConfig) => (() => vue.VNode<vue.RendererNode, vue.RendererElement, {
|
|
375
|
+
[key: string]: any;
|
|
376
|
+
}>) | undefined;
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
380
|
+
type ApiFn = (args: any) => Promise<unknown>;
|
|
381
|
+
interface TableColumnCheck {
|
|
382
|
+
key: string;
|
|
383
|
+
title: string;
|
|
384
|
+
checked: boolean;
|
|
385
|
+
}
|
|
386
|
+
type TableDataWithIndex<T> = T & {
|
|
387
|
+
index: number;
|
|
388
|
+
};
|
|
389
|
+
interface TransformedData<T> {
|
|
390
|
+
data: TableDataWithIndex<T>[];
|
|
391
|
+
pageNum: number;
|
|
392
|
+
pageSize: number;
|
|
393
|
+
total: number;
|
|
394
|
+
}
|
|
395
|
+
type Transformer<T, Response> = (response: Response) => TransformedData<T>;
|
|
396
|
+
interface TableConfig<A extends ApiFn, T, C> {
|
|
397
|
+
/** Api function to get table data */
|
|
398
|
+
apiFn: A;
|
|
399
|
+
/** Api params */
|
|
400
|
+
apiParams?: Parameters<A>[0];
|
|
401
|
+
/** Transform api response to table data */
|
|
402
|
+
transformer: Transformer<T, Awaited<ReturnType<A>>>;
|
|
403
|
+
/** Columns factory */
|
|
404
|
+
columns: () => C[];
|
|
405
|
+
/**
|
|
406
|
+
* Get column checks
|
|
407
|
+
*
|
|
408
|
+
* @param columns
|
|
409
|
+
*/
|
|
410
|
+
getColumnChecks: (columns: C[]) => TableColumnCheck[];
|
|
411
|
+
/**
|
|
412
|
+
* Get columns
|
|
413
|
+
*
|
|
414
|
+
* @param columns
|
|
415
|
+
*/
|
|
416
|
+
getColumns: (columns: C[], checks: TableColumnCheck[]) => C[];
|
|
417
|
+
/**
|
|
418
|
+
* Callback when response fetched
|
|
419
|
+
*
|
|
420
|
+
* @param transformed transformed data
|
|
421
|
+
*/
|
|
422
|
+
onFetched?: (transformed: TransformedData<T>) => MaybePromise<void>;
|
|
423
|
+
/**
|
|
424
|
+
* Whether to get data immediately
|
|
425
|
+
*
|
|
426
|
+
* @default true
|
|
427
|
+
*/
|
|
428
|
+
immediate?: boolean;
|
|
429
|
+
}
|
|
430
|
+
declare function useHookTable<A extends ApiFn, T, C>(config: TableConfig<A, T, C>): {
|
|
431
|
+
loading: Ref<boolean, boolean>;
|
|
432
|
+
empty: Ref<boolean, boolean>;
|
|
433
|
+
data: Ref<TableDataWithIndex<T>[], TableDataWithIndex<T>[]>;
|
|
434
|
+
columns: vue.ComputedRef<C[]>;
|
|
435
|
+
columnChecks: Ref<TableColumnCheck[], TableColumnCheck[]>;
|
|
436
|
+
reloadColumns: () => void;
|
|
437
|
+
getData: () => Promise<void>;
|
|
438
|
+
searchParams: NonNullable<Parameters<A>[0]>;
|
|
439
|
+
updateSearchParams: (params: Partial<Parameters<A>[0]>) => void;
|
|
440
|
+
resetSearchParams: () => void;
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
export { type ApiFn, type CreateCallbackParams, type HookRequestInstance, type HookRequestInstanceResponseData, type HookRequestInstanceResponseFailData, type HookRequestInstanceResponseSuccessData, type MaybePromise, type ReadonlySignal, type RemoveEventFn, type Signal, type SignalOptions, type TableColumnCheck, type TableConfig, type TableDataWithIndex, type TransformedData, type Transformer, type UseEventParams, type WindowLocation, createBreakpointListen, lockHtmlScrollRightCompensationRef, useBoolean, useBreakpoint, useBrowserLocation, useComputed, useContext, useCountDown, useDeferredTrue, useEventListener, useHookTable, useInjectionCollection, useInjectionElementCollection, useInjectionInstanceCollection, useIsComposing, useLayoutContentStyle, useLayoutFooterStyle, useLayoutHeaderStyle, useLoading, useLocalPage, useLockHtmlScroll, useMemo, useMergedState, useScript, useScroll, useSignal, useSvgIconRender };
|