react-hook-toolkit 0.0.3 → 0.0.4

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.
@@ -0,0 +1,363 @@
1
+ import * as react from 'react';
2
+ import { RefObject } from 'react';
3
+ import { AxiosRequestConfig } from 'axios';
4
+ import * as Yup from 'yup';
5
+
6
+ interface UseAxiosResponse<T> {
7
+ data: T | null;
8
+ loading: boolean;
9
+ error: string | null;
10
+ makeRequest: (url: string, method?: string, requestData?: any) => void;
11
+ cancelRequest: () => void;
12
+ }
13
+ interface UseAxiosConfig extends AxiosRequestConfig {
14
+ baseURL?: string;
15
+ headers?: {
16
+ [key: string]: string;
17
+ };
18
+ }
19
+ declare const useAxios: <T>(config?: UseAxiosConfig) => UseAxiosResponse<T>;
20
+ declare const useAdvReducer: <State, Action extends {
21
+ type: string;
22
+ }>(initialState: State, actions: Record<string, (state: State, action: Action) => State>) => (state: State | undefined, action: Action) => State;
23
+ interface FetchState<T> {
24
+ data: T | null;
25
+ loading: boolean;
26
+ error: string | null;
27
+ }
28
+ declare function useFetch<T>(url: string): FetchState<T>;
29
+ declare function useLocalStorage<T>(key: string, initialValue: T): [T, (value: T) => void];
30
+ declare function useToggle(initialValue?: boolean): [boolean, () => void];
31
+ declare function useDebounce<T>(value: T, delay: number): T;
32
+ declare function useThrottle<T>(value: T, limit: number): T;
33
+ declare function usePrevious<T>(value: T): T | undefined;
34
+ declare function useMediaQuery(query: string): boolean;
35
+ declare function useClipboard(): [boolean, (text: string) => Promise<void>];
36
+ declare function useInterval(callback: () => void, delay: number | null): void;
37
+ interface WindowSize {
38
+ width: number;
39
+ height: number;
40
+ }
41
+ declare function useWindowSize(): WindowSize;
42
+ declare function useKeyPress(targetKey: string): boolean;
43
+ declare function useOnlineStatus(): boolean;
44
+ interface ScrollPosition {
45
+ x: number;
46
+ y: number;
47
+ }
48
+ declare function useScrollPosition(): ScrollPosition;
49
+ declare function useTimeout(callback: () => void, delay: number | null): void;
50
+ declare function useDarkMode(): [boolean, () => void];
51
+ declare function useForm<T extends Record<string, any>>(initialValues: T): {
52
+ values: T;
53
+ errors: Partial<Record<keyof T, string>>;
54
+ handleChange: (name: keyof T, value: any) => void;
55
+ validate: (validators: Partial<Record<keyof T, (value: any) => string | null>>) => boolean;
56
+ };
57
+ declare function useArray<T>(initialArray: T[]): {
58
+ array: T[];
59
+ set: react.Dispatch<react.SetStateAction<T[]>>;
60
+ push: (item: T) => void;
61
+ removeByIndex: (index: number) => void;
62
+ clear: () => void;
63
+ };
64
+ declare function useStep<T>(steps: T[]): {
65
+ currentStep: T;
66
+ currentStepIndex: number;
67
+ next: () => void;
68
+ previous: () => void;
69
+ reset: () => void;
70
+ isFirstStep: boolean;
71
+ isLastStep: boolean;
72
+ };
73
+ declare function useTimeoutFn(callback: () => void, delay: number | null): {
74
+ reset: () => void;
75
+ clear: () => void;
76
+ };
77
+ declare function useDebouncedCallback(callback: () => void, delay: number): () => void;
78
+ declare function useScrollLock(lock: boolean): void;
79
+ declare function useResizeObserver<T extends HTMLElement>(ref: RefObject<T>): WindowSize | null;
80
+ interface MousePosition {
81
+ x: number;
82
+ y: number;
83
+ }
84
+ declare function useMousePosition(): MousePosition;
85
+ type ScrollDirection = 'up' | 'down';
86
+ declare function useScrollDirection(): ScrollDirection;
87
+ declare function useImageLoader(src: string): {
88
+ loaded: boolean;
89
+ error: boolean;
90
+ };
91
+ declare function usePersistedState<T>(key: string, initialValue: T): [T, React.Dispatch<React.SetStateAction<T>>];
92
+ declare function useReducedMotion(): boolean;
93
+ declare function useCookie(key: string): [string | null, (value: string, options?: any) => void, () => void];
94
+ declare function useFetchRetry<T>(url: string, options: RequestInit, retries?: number): {
95
+ data: T | null;
96
+ error: string | null;
97
+ loading: boolean;
98
+ };
99
+ declare function useDelay<T>(value: T, delay: number): T;
100
+ declare function useVisibilityChange(): boolean;
101
+ declare function useDebouncedValue<T>(value: T, delay: number): T;
102
+ type Status = 'idle' | 'pending' | 'success' | 'error';
103
+ type AsyncFunction<T> = () => Promise<T>;
104
+ interface UseAsyncReturn<T> {
105
+ execute: () => void;
106
+ status: Status;
107
+ value: T | null;
108
+ error: Error | null;
109
+ }
110
+ declare function useAsync<T>(asyncFunction: AsyncFunction<T>, immediate?: boolean): UseAsyncReturn<T>;
111
+ type ScriptStatus = 'loading' | 'ready' | 'error' | 'unknown';
112
+ declare function useScript(src: string, removeOnUnmount?: boolean): ScriptStatus;
113
+ declare function useIndexedDB<T>(dbName: string, storeName: string): {
114
+ data: T | null;
115
+ error: Error | null;
116
+ };
117
+ declare function useGeoLocation(): {
118
+ position: GeolocationPosition | null;
119
+ error: Error | null;
120
+ };
121
+ declare function useTimer(initialTime: number): {
122
+ time: number;
123
+ error: Error | null;
124
+ };
125
+ declare function useIsMounted(): boolean;
126
+ declare function useCss(css: string): {
127
+ error: Error | null;
128
+ };
129
+ declare function useSpeak(text: string): {
130
+ speak: () => void;
131
+ error: Error | null;
132
+ };
133
+ declare function useCountUp(target: number, duration: number): {
134
+ count: number;
135
+ error: Error | null;
136
+ };
137
+ declare function useCountDown(start: number): {
138
+ count: number;
139
+ error: Error | null;
140
+ };
141
+ interface BatteryState {
142
+ supported: boolean;
143
+ loading: boolean;
144
+ level: number | null;
145
+ charging: boolean | null;
146
+ chargingTime: number | null;
147
+ dischargingTime: number | null;
148
+ }
149
+ declare const useBattery: () => BatteryState;
150
+ declare const useEventListener: (eventName: string, handler: (event: Event) => void, elementRef?: React.RefObject<HTMLElement>, options?: boolean | AddEventListenerOptions) => void;
151
+ interface HistoryState {
152
+ [key: string]: any;
153
+ }
154
+ interface UseHistory {
155
+ history: History;
156
+ state: HistoryState | null;
157
+ push: (path: string, state?: HistoryState) => void;
158
+ replace: (path: string, state?: HistoryState) => void;
159
+ goBack: () => void;
160
+ goForward: () => void;
161
+ }
162
+ declare const useHistory: () => UseHistory;
163
+ interface UsePreferredLanguage {
164
+ language: string;
165
+ languages: Array<string>;
166
+ isSupported: boolean;
167
+ }
168
+ declare const usePreferredLanguage: () => UsePreferredLanguage;
169
+ interface UseSessionStorage<T> {
170
+ (key: string, initialValue: T): [T, (value: T) => void];
171
+ }
172
+ declare const useSessionStorage: UseSessionStorage<any>;
173
+ interface UseSound {
174
+ play: () => void;
175
+ pause: () => void;
176
+ stop: () => void;
177
+ setVolume: (volume: number) => void;
178
+ isPlaying: boolean;
179
+ error: Error | null;
180
+ }
181
+ declare const useSound: (url: string) => UseSound;
182
+ interface TouchPosition {
183
+ x: number | null;
184
+ y: number | null;
185
+ }
186
+ interface UseTouch {
187
+ (elementRef: React.RefObject<HTMLElement>): {
188
+ touchStart: TouchPosition;
189
+ touchMove: TouchPosition;
190
+ touchEnd: TouchPosition;
191
+ };
192
+ }
193
+ declare const useTouch: UseTouch;
194
+ declare const useUpdateEffect: (effect: React.EffectCallback, deps: React.DependencyList) => void;
195
+
196
+ interface FormRef {
197
+ current: {
198
+ validateForm: () => Promise<Record<string, any>>;
199
+ } | null;
200
+ }
201
+ type ValidationResult = {
202
+ isValid: boolean;
203
+ errors: Record<string, any> | [];
204
+ };
205
+ type MenuItem = {
206
+ path?: string;
207
+ data?: MenuItem[];
208
+ [key: string]: any;
209
+ };
210
+ type ReducerActionHandler<State, Action> = (state: State, action: Action) => State;
211
+ type StorageType = 'local' | 'session';
212
+ type StorageAction = 'SET' | 'GET' | 'REMOVE' | 'CLEAR';
213
+ interface UseStorageParams {
214
+ action: StorageAction;
215
+ type: StorageType;
216
+ key: string;
217
+ value?: any;
218
+ }
219
+ type FlattenedObject = {
220
+ [key: string]: any;
221
+ };
222
+
223
+ declare const isWindowReady: () => boolean;
224
+ declare const _encryptJson: (decrypted: any, secretKey: string) => string | null;
225
+ declare const _decryptJson: (encrypted: string, secretKey: string) => any;
226
+ declare const _encryptString: (text: string, secretKey: string, isRandom?: boolean) => {
227
+ ciphertext: string;
228
+ iv: string;
229
+ };
230
+ declare const _decryptString: (ciphertext: string, ivBase64: string, secretKey: string) => string;
231
+ declare const _encryptPayload: (jsonData: any, secretKey: string) => {
232
+ data: string;
233
+ header: string;
234
+ iv: string;
235
+ } | undefined;
236
+ declare const _generatePassword: (length?: number) => string;
237
+ declare const _getStartStopTime: (sdate: Date, format?: string) => "Invalid Date" | {
238
+ startTime: string;
239
+ stopTime: string;
240
+ };
241
+ declare const _isEmptyArray: (value: unknown) => boolean;
242
+ declare const _isEmptyObject: (obj: object) => boolean;
243
+ declare const _isValueInArray: <T>(array: unknown, value: T) => boolean;
244
+ declare const _exportToExcel: (jsonData: object[], fileName: string, fileExtension?: string) => void;
245
+ declare const _promise: (time: number) => Promise<unknown>;
246
+ declare const _hasItem: (menuItems: MenuItem[], path: string, key?: string) => any | null;
247
+ declare const _hasElement: (arr: MenuItem[], key: string, value: any) => any | null;
248
+ declare const _flattenArray: (arr: any[]) => any[];
249
+ declare const _dynamicReducer: (initialState: any) => (state: any, action: any) => any;
250
+ declare const _genericReducer: <State, Action extends {
251
+ type: string;
252
+ }>(initialState: State, actions: Record<string, ReducerActionHandler<State, Action>>) => (state: State | undefined, action: Action) => State;
253
+ declare const _thousandSeparator: (num: number) => {
254
+ allDecimal: string;
255
+ twoDecimal: string;
256
+ };
257
+ declare const _getFinancialYear: () => string;
258
+ declare const _getStorage: ({ action, type, key, value }: UseStorageParams) => any | null;
259
+ declare const _deepClone: <T>(obj: T) => T;
260
+ declare const _getCookie: (name: string) => string | null;
261
+ declare const _setCookie: (name: string, value: string, days: number) => void;
262
+ declare const _mergeObjects: <T extends Record<string, any>>(obj1: T, obj2: T) => T;
263
+ declare const _mapObject: <T>(obj: {
264
+ [key: string]: any;
265
+ }, callback: (key: string, value: any) => T) => T[];
266
+ declare const _isEqual: <T>(value1: T, value2: T) => boolean;
267
+ declare const _capitalize: (str: string | null | undefined) => string;
268
+ declare const _convertToCurrency: (amount: number, currency?: string) => string;
269
+ declare const _getBrowserInfo: () => string;
270
+ declare const _isMobile: () => boolean;
271
+ declare const _parseJSON: <T>(json: string) => T | null;
272
+ declare const _stringifyJSON: <T>(value: T) => string | null;
273
+ declare const _scrollToTop: () => void;
274
+ declare const _batchProcess: <T>(data: T[], batchSize: number, processFn: (item: T) => Promise<void>) => Promise<void>;
275
+ declare const _flattenObject: (obj: any, prefix?: string) => FlattenedObject;
276
+ declare const _deepMerge: (target: any, source: any) => any;
277
+ declare const _chunk: <T>(arr: T[], size: number) => T[][];
278
+ declare const _asyncDebounce: (fn: (...args: any[]) => Promise<void>, delay: number) => (...args: any[]) => Promise<void>;
279
+ declare const _deepCloneArray: <T>(arr: T[]) => T[];
280
+ declare const _getMaxMinValue: (arr: number[]) => {
281
+ max: number;
282
+ min: number;
283
+ };
284
+ declare const _asyncMap: <T, U>(arr: T[], asyncFn: (item: T) => Promise<U>) => Promise<U[]>;
285
+ declare const _transformAsyncData: <T, U>(arr: T[], transformer: (item: T) => Promise<U>) => Promise<U[]>;
286
+ declare const _getNestedProperty: (obj: Record<string, any>, path: string) => Record<string, any>;
287
+ declare const _deepEqual: (obj1: any, obj2: any) => boolean;
288
+ declare const _mergeArrays: <T>(arr1: T[], arr2: T[]) => T[];
289
+ declare const _filterDuplicates: <T>(arr: T[], uniqueKey: keyof T) => T[];
290
+ declare const _sortByKey: <T>(arr: T[], key: keyof T) => T[];
291
+ declare const _mapAsync: <T, U>(arr: T[], asyncFn: (item: T) => Promise<U>) => Promise<U[]>;
292
+ declare const _formatDate: (date: Date, format: string) => string;
293
+ declare const _calPercentage: (value: number, total: number) => number;
294
+ declare const _sum: (numbers: number[]) => number;
295
+ declare const _average: (numbers: number[]) => number;
296
+ declare const _getPriceAfterTax: (price: number, taxRate: number) => number;
297
+ declare const _calculateTimeDifference: (startDate: Date, endDate: Date) => string;
298
+ declare const _arrayIncludesObject: (arr: any[], obj: any) => boolean;
299
+ declare const _toCamelCase: (str: string) => string;
300
+ declare const _freeze: <T extends Record<string, unknown>>(obj: T) => Readonly<T> | null;
301
+ declare const _isFreeze: <T extends Record<string, unknown>>(obj: T | null) => boolean;
302
+ declare const _seal: <T extends Record<string, unknown>>(obj: T) => T | null;
303
+ declare const _isSeal: <T extends Record<string, unknown>>(obj: T | null) => boolean;
304
+ declare const _arrayToObject: (arr: [string, any][]) => {
305
+ [key: string]: any;
306
+ };
307
+ declare const _objectToArray: (obj: any) => any[][];
308
+ declare const _arrayToObjectByKey: (arr: any[], key: string) => any;
309
+ declare const _isInArray: (arr: any[], value: any) => boolean;
310
+ declare const _getObjectValues: (obj: object) => any[];
311
+ declare const _swapArrayElements: (arr: any[], index1: number, index2: number) => void;
312
+ declare const _filterObjectByKey: (obj: object, keys: string[]) => {
313
+ [k: string]: any;
314
+ };
315
+ declare const _getScrollPosition: () => {
316
+ scrollX: number;
317
+ scrollY: number;
318
+ };
319
+ declare const _arrayIntersection: (arr1: any[], arr2: any[]) => any[];
320
+ declare const _getArrayOfObjectsByProperty: (arr: any[], key: string, value: any) => any[];
321
+ declare const _downloadBlob: (blob: Blob, filename: string) => void;
322
+ declare const _base64ToBlob: (base64: string) => Blob;
323
+ declare const _initializeFormValues: (fields: string[]) => {
324
+ [key: string]: any;
325
+ };
326
+ declare const _dynamicRequiredValidation: (requiredFields: string[]) => Yup.ObjectSchema<any>;
327
+ declare const _generateYupValidation: (fields: string[]) => Yup.ObjectSchema<any>;
328
+ declare const _initializeFormikFields: (fields: string[]) => {
329
+ [key: string]: string;
330
+ };
331
+ declare const _setNestedProperty: (obj: any, path: string, value: any) => any;
332
+ declare const _transformArray: <T, R>(arr: T[], transformFn: (item: T) => R) => R[];
333
+ declare const _findObjectById: <T>(arr: T[], id: string) => T | undefined;
334
+ declare const _getUniqueValues: (arr: any[]) => any[];
335
+ declare const _mergeArraysByKey: (arr1: any, arr2: any, key: string) => any[];
336
+ declare const _removeDuplicates: (arr: any, key: string) => any;
337
+ declare const _groupBy: (arr: any, key: string) => any;
338
+ declare const _arrayDiff: <T>(arr1: T[], arr2: T[]) => T[];
339
+ declare const _deepCompareArrays: <T>(arr1: T[], arr2: T[]) => boolean;
340
+ declare const _updateObjectInArray: (arr: [], key: string, value: any, update: any) => any[];
341
+ declare const _getKeyByValue: (obj: Record<string, any> | null | undefined, value: any) => string | null;
342
+ declare const _getValueByKey: (obj: Record<string, any> | null | undefined, key: string) => any;
343
+ declare const _getKeysByValue: (obj: Record<string, any> | null | undefined, value: any) => string[];
344
+ declare const _dateFormat: (date: Date, format?: string) => string;
345
+ declare const _dateTransformer: (data: any) => any;
346
+ declare const _escapeRegExpMatch: (s: string) => string;
347
+ declare const _isExactMatch: (str: string, match: string) => boolean;
348
+ declare const _bytesToSize: (bytes: number, decimals?: number) => string;
349
+ declare const _swapArrayByKey: (arr: any, key: string) => any;
350
+ declare const _setTouchedFields: (formRef: any, errors: any) => void;
351
+ declare const _isValidForm: (formRef: any) => Promise<boolean>;
352
+ declare const _validateFormRef: (formRef: FormRef) => Promise<ValidationResult>;
353
+ declare const _handleParentsMenu: (currentArr: any, menuID: string) => void;
354
+ declare const _handleParentNode: (currentArr: any, menuID: string, permissionKey: string) => void;
355
+ declare const _handleChildrenMenu: (currentArr: any, parentKey: any, checked: boolean) => void;
356
+ declare const _handlePermissions: (currentArr: any, currentObj: any, permissionKey: string, isChecked: boolean) => any;
357
+ declare const _allowDecimalKeys: (event: any) => void;
358
+ declare const _allowAlphaKeys: (event: any) => void;
359
+ declare const _handlePasteDecimalKeys: (a: any) => void;
360
+ declare const _handlePasteAlphabetKeys: (event: any) => void;
361
+ declare const _allowAlphaNumericKeys: (event: KeyboardEvent) => void;
362
+
363
+ export { type AsyncFunction, type ScriptStatus, type Status, type UseAsyncReturn, _allowAlphaKeys, _allowAlphaNumericKeys, _allowDecimalKeys, _arrayDiff, _arrayIncludesObject, _arrayIntersection, _arrayToObject, _arrayToObjectByKey, _asyncDebounce, _asyncMap, _average, _base64ToBlob, _batchProcess, _bytesToSize, _calPercentage, _calculateTimeDifference, _capitalize, _chunk, _convertToCurrency, _dateFormat, _dateTransformer, _decryptJson, _decryptString, _deepClone, _deepCloneArray, _deepCompareArrays, _deepEqual, _deepMerge, _downloadBlob, _dynamicReducer, _dynamicRequiredValidation, _encryptJson, _encryptPayload, _encryptString, _escapeRegExpMatch, _exportToExcel, _filterDuplicates, _filterObjectByKey, _findObjectById, _flattenArray, _flattenObject, _formatDate, _freeze, _generatePassword, _generateYupValidation, _genericReducer, _getArrayOfObjectsByProperty, _getBrowserInfo, _getCookie, _getFinancialYear, _getKeyByValue, _getKeysByValue, _getMaxMinValue, _getNestedProperty, _getObjectValues, _getPriceAfterTax, _getScrollPosition, _getStartStopTime, _getStorage, _getUniqueValues, _getValueByKey, _groupBy, _handleChildrenMenu, _handleParentNode, _handleParentsMenu, _handlePasteAlphabetKeys, _handlePasteDecimalKeys, _handlePermissions, _hasElement, _hasItem, _initializeFormValues, _initializeFormikFields, _isEmptyArray, _isEmptyObject, _isEqual, _isExactMatch, _isFreeze, _isInArray, _isMobile, _isSeal, _isValidForm, _isValueInArray, _mapAsync, _mapObject, _mergeArrays, _mergeArraysByKey, _mergeObjects, _objectToArray, _parseJSON, _promise, _removeDuplicates, _scrollToTop, _seal, _setCookie, _setNestedProperty, _setTouchedFields, _sortByKey, _stringifyJSON, _sum, _swapArrayByKey, _swapArrayElements, _thousandSeparator, _toCamelCase, _transformArray, _transformAsyncData, _updateObjectInArray, _validateFormRef, isWindowReady, useAdvReducer, useArray, useAsync, useAxios, useBattery, useClipboard, useCookie, useCountDown, useCountUp, useCss, useDarkMode, useDebounce, useDebouncedCallback, useDebouncedValue, useDelay, useEventListener, useFetch, useFetchRetry, useForm, useGeoLocation, useHistory, useImageLoader, useIndexedDB, useInterval, useIsMounted, useKeyPress, useLocalStorage, useMediaQuery, useMousePosition, useOnlineStatus, usePersistedState, usePreferredLanguage, usePrevious, useReducedMotion, useResizeObserver, useScript, useScrollDirection, useScrollLock, useScrollPosition, useSessionStorage, useSound, useSpeak, useStep, useThrottle, useTimeout, useTimeoutFn, useTimer, useToggle, useTouch, useUpdateEffect, useVisibilityChange, useWindowSize };