@gateweb/react-utils 1.17.0 → 2.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/cjs/client.d.ts +289 -0
- package/dist/cjs/client.js +119 -0
- package/dist/cjs/index.d.ts +2 -193
- package/dist/cjs/index.js +858 -142
- package/dist/cjs/{queryStore-12s-q_SLGgYH.js → queryStore-12s-JzzXvjJC.js} +4 -0
- package/dist/cjs/store-12s-s9lu_ZPn.js +122 -0
- package/dist/cjs/webStorage-12s-0RtNO_uc.js +136 -0
- package/dist/es/client.d.mts +289 -0
- package/dist/es/client.mjs +103 -0
- package/dist/es/index.d.mts +2 -193
- package/dist/es/index.mjs +828 -101
- package/dist/es/{queryStore-12s-CFQTVwrg.mjs → queryStore-12s-Dkb-Af1n.mjs} +4 -0
- package/dist/es/store-12s-DsXc3Uo0.mjs +118 -0
- package/dist/es/webStorage-12s-Bo7x8q5t.mjs +135 -0
- package/package.json +11 -1
- package/dist/cjs/webStorage-12s-P8HpaTRP.js +0 -911
- package/dist/es/webStorage-12s-gvD4qbeR.mjs +0 -879
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { AtLeastOne } from './types.js';
|
|
3
|
+
|
|
4
|
+
type TStoreProps<S> = {
|
|
5
|
+
/**
|
|
6
|
+
* the store object
|
|
7
|
+
*/
|
|
8
|
+
store: Partial<S>;
|
|
9
|
+
};
|
|
10
|
+
type TStoreObject<S> = TStoreProps<S>['store'];
|
|
11
|
+
type TStoreState<S> = {
|
|
12
|
+
/**
|
|
13
|
+
* trigger the change of store
|
|
14
|
+
*
|
|
15
|
+
* @param updater - the new store or a update function
|
|
16
|
+
* @param options - the options for changing the store
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* // update with new store object
|
|
21
|
+
* changeStore({ key: 'newValue' });
|
|
22
|
+
* // update with a function
|
|
23
|
+
* changeStore(preStore => ({ ...preStore, key: 'newValue' }));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
changeStore: (updater: TStoreObject<S> | ((preStore: TStoreObject<S>) => TStoreObject<S>), options?: {
|
|
27
|
+
merge: TInitialProps$1<S>['defaultMerge'];
|
|
28
|
+
}) => void;
|
|
29
|
+
} & TStoreProps<S>;
|
|
30
|
+
type TInitialProps$1<S> = TStoreProps<S> & {
|
|
31
|
+
/**
|
|
32
|
+
* handle the change of store when calling `changeStore`
|
|
33
|
+
*
|
|
34
|
+
* @param preStore - the previous store
|
|
35
|
+
* @param newStore - the new store
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* ```tsx
|
|
40
|
+
* <StoreProvider store={{ key: 'value' }} handleChangeStore={(preStore, newStore) => {
|
|
41
|
+
* // Special handling: reset key when changing anotherKey
|
|
42
|
+
* if (newStore.anotherKey && newStore.anotherKey !== preStore.anotherKey) {
|
|
43
|
+
* return {
|
|
44
|
+
* ...preStore,
|
|
45
|
+
* ...newStore,
|
|
46
|
+
* key: 'resetValue',
|
|
47
|
+
* };
|
|
48
|
+
* }
|
|
49
|
+
* return {
|
|
50
|
+
* ...preStore,
|
|
51
|
+
* ...newStore,
|
|
52
|
+
* };
|
|
53
|
+
* }}>
|
|
54
|
+
* <App />
|
|
55
|
+
* </StoreProvider>
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
handleChangeStore?: (preStore: TStoreObject<S>, newStore: TStoreObject<S>) => TStoreObject<S>;
|
|
59
|
+
/**
|
|
60
|
+
* the default merge strategy when calling `changeStore`
|
|
61
|
+
*
|
|
62
|
+
* @default 'shallow'
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
*
|
|
66
|
+
* ```tsx
|
|
67
|
+
* // shallow merge
|
|
68
|
+
* <StoreProvider store={{ key: 'value', anotherKey: { nestedKey: 'nestedValue' } }}>
|
|
69
|
+
* // deep merge
|
|
70
|
+
* <StoreProvider store={{ key: 'value', anotherKey: { nestedKey: 'nestedValue' } }} defaultMerge="deep">
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
defaultMerge: 'shallow' | 'deep';
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* create a store context with provider and hooks
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
*
|
|
80
|
+
* ```tsx
|
|
81
|
+
* // create a store context
|
|
82
|
+
* const { StoreProvider, useStore, useStoreApi } = createStoreContext<MyObject>();
|
|
83
|
+
*
|
|
84
|
+
* // use the StoreProvider to provide the store to the context
|
|
85
|
+
* <StoreProvider store={{ key: 'value' }}>
|
|
86
|
+
* <App />
|
|
87
|
+
* </StoreProvider>
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
declare const createStoreContext: <S>() => {
|
|
91
|
+
StoreProvider: ({ children, store, handleChangeStore, defaultMerge, }: React.PropsWithChildren<Partial<TInitialProps$1<S>>>) => React.JSX.Element;
|
|
92
|
+
useStore: <T>(selector: (state: TStoreState<S>) => T, equalityFn?: (left: T, right: T) => boolean) => T;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
type TQueryProps<Q> = {
|
|
96
|
+
/**
|
|
97
|
+
* the query object
|
|
98
|
+
*/
|
|
99
|
+
query: Partial<Q>;
|
|
100
|
+
};
|
|
101
|
+
type TQueryState<Q> = {
|
|
102
|
+
/**
|
|
103
|
+
* trigger the change of query
|
|
104
|
+
*
|
|
105
|
+
* @param query - the new query
|
|
106
|
+
*/
|
|
107
|
+
changeQuery: (query: TQueryProps<Q>['query']) => void;
|
|
108
|
+
} & TQueryProps<Q>;
|
|
109
|
+
type TInitialProps<Q> = Partial<TQueryProps<Q> & {
|
|
110
|
+
/**
|
|
111
|
+
* handle the change of query when calling `changeQuery`
|
|
112
|
+
*
|
|
113
|
+
* @param preQuery - the previous query
|
|
114
|
+
* @param newQuery - the new query
|
|
115
|
+
* @returns the custom new query
|
|
116
|
+
*/
|
|
117
|
+
handleChangeQuery: (preQuery: TQueryProps<Q>['query'], newQuery: TQueryProps<Q>['query']) => TQueryProps<Q>['query'];
|
|
118
|
+
}>;
|
|
119
|
+
/**
|
|
120
|
+
* Provider to provide the store to the context
|
|
121
|
+
*
|
|
122
|
+
* @deprecated use `StoreProvider` from `core/store` instead
|
|
123
|
+
*/
|
|
124
|
+
declare const QueryProvider: <Q>({ children, query, handleChangeQuery, }: React.PropsWithChildren<TInitialProps<Q>>) => React.JSX.Element;
|
|
125
|
+
/**
|
|
126
|
+
* hook to get the store from the context
|
|
127
|
+
*
|
|
128
|
+
* because we want the return type of `selector` to be inferred by ts, we use HOF to implement the hook
|
|
129
|
+
*
|
|
130
|
+
* so you should use it like this:
|
|
131
|
+
*
|
|
132
|
+
* ```tsx
|
|
133
|
+
* const useQuery = useQueryContext<MyObject>(); // => will return the store hook
|
|
134
|
+
* const result = useQuery(q => q.query); // => will return the query object
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
*
|
|
139
|
+
* ```tsx
|
|
140
|
+
* const result1 = useQueryContext<MyObject>()(q => '1234');
|
|
141
|
+
* const result2 = useQueryContext<MyObject>()(q => q.changeQuery);
|
|
142
|
+
* const result3 = useQueryContext<MyObject>()(q => q.query);
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @deprecated use `useStoreContext` from `core/store` instead
|
|
146
|
+
*/
|
|
147
|
+
declare const useQueryContext: <Q>() => <T>(selector: (state: TQueryState<Q>) => T, equalityFn?: (left: T, right: T) => boolean) => T;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
151
|
+
*
|
|
152
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
153
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
154
|
+
* and a Provider component for supplying context values.
|
|
155
|
+
*
|
|
156
|
+
* @template T - The value type for the context.
|
|
157
|
+
* @returns {object} An object containing:
|
|
158
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
159
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* // Example usage:
|
|
163
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
164
|
+
*
|
|
165
|
+
* function Counter() {
|
|
166
|
+
* const { count } = useDataContext();
|
|
167
|
+
* return <span>{count}</span>;
|
|
168
|
+
* }
|
|
169
|
+
*
|
|
170
|
+
* function App() {
|
|
171
|
+
* return (
|
|
172
|
+
* <DataProvider value={{ count: 42 }}>
|
|
173
|
+
* <Counter />
|
|
174
|
+
* </DataProvider>
|
|
175
|
+
* );
|
|
176
|
+
* }
|
|
177
|
+
*/
|
|
178
|
+
declare const createDataContext: <T>() => {
|
|
179
|
+
readonly useDataContext: () => T & ({} | null);
|
|
180
|
+
readonly DataProvider: ({ children, value }: {
|
|
181
|
+
children: ReactNode;
|
|
182
|
+
value: T;
|
|
183
|
+
}) => React.JSX.Element;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
type TCountdownActions = {
|
|
187
|
+
/** 目前秒數 */
|
|
188
|
+
countdown: number;
|
|
189
|
+
/** 是否正在倒數計時 */
|
|
190
|
+
isCounting: boolean;
|
|
191
|
+
/** 開始倒數計時 */
|
|
192
|
+
start: () => void;
|
|
193
|
+
/** 停止倒數計時 */
|
|
194
|
+
stop: () => void;
|
|
195
|
+
/** 重置倒數計時 */
|
|
196
|
+
reset: () => void;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* 倒數計時器
|
|
200
|
+
*
|
|
201
|
+
* 可以透過 start() 來啟動倒數計時器
|
|
202
|
+
* 可以透過 stop() 來停止倒數計時器
|
|
203
|
+
* 可以透過 reset() 來重置倒數計時器
|
|
204
|
+
*
|
|
205
|
+
* @param initialCountdown 倒數計時器初始值
|
|
206
|
+
* @param enableReinitialize 允許重設初始值
|
|
207
|
+
*/
|
|
208
|
+
declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
|
|
209
|
+
|
|
210
|
+
type UseDisclosureReturn = {
|
|
211
|
+
/** Whether the disclosure is currently open. */
|
|
212
|
+
isOpen: boolean;
|
|
213
|
+
/** Open the disclosure (sets isOpen = true). */
|
|
214
|
+
open: () => void;
|
|
215
|
+
/** Close the disclosure (sets isOpen = false). */
|
|
216
|
+
close: () => void;
|
|
217
|
+
/** Toggle the disclosure state (open -> close or close -> open). */
|
|
218
|
+
toggle: () => void;
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* A small hook to control open/close state.
|
|
222
|
+
*
|
|
223
|
+
* Supports an optional controlled pattern by passing `isOpen` and `onChange`.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* const { isOpen, open, close, toggle } = useDisclosure();
|
|
227
|
+
*/
|
|
228
|
+
declare function useDisclosure(initialState?: boolean): UseDisclosureReturn;
|
|
229
|
+
|
|
230
|
+
type TValueOptions<T> = AtLeastOne<{
|
|
231
|
+
/**
|
|
232
|
+
* The controlled value.
|
|
233
|
+
*/
|
|
234
|
+
value?: T;
|
|
235
|
+
/**
|
|
236
|
+
* The default value.
|
|
237
|
+
*/
|
|
238
|
+
defaultValue?: T;
|
|
239
|
+
}>;
|
|
240
|
+
/**
|
|
241
|
+
* A hook to manage a value.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
*
|
|
245
|
+
* ```tsx
|
|
246
|
+
* const MyComponent = ({ value }: { value?: number }) => {
|
|
247
|
+
* const [currentValue, setCurrentValue] = useValue({ value });
|
|
248
|
+
* };
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare const useValue: <T>({ value, defaultValue }: TValueOptions<T>) => readonly [T, (newValue: T) => void];
|
|
252
|
+
|
|
253
|
+
declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Downloads a file from a given source.
|
|
257
|
+
*
|
|
258
|
+
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
259
|
+
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
260
|
+
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
264
|
+
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
265
|
+
*/
|
|
266
|
+
declare const downloadFile: (source: string | Blob, filename?: string, fileExtension?: string) => void;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 從 localStorage 取得資料,支援槽狀取值(Json 物件)
|
|
270
|
+
*
|
|
271
|
+
* @param key 鍵值
|
|
272
|
+
* @param deCode 是否解碼
|
|
273
|
+
* @returns 取得的資料
|
|
274
|
+
* @example
|
|
275
|
+
* const data = getLocalStorage('key');
|
|
276
|
+
*
|
|
277
|
+
* const data = getLocalStorage('key.subKey');
|
|
278
|
+
*/
|
|
279
|
+
declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefined;
|
|
280
|
+
/**
|
|
281
|
+
* 將資料(Json 物件)存入 localStorage
|
|
282
|
+
* @param key 鍵值
|
|
283
|
+
* @param value 可序列化的資料
|
|
284
|
+
* @param enCode 是否編碼
|
|
285
|
+
*/
|
|
286
|
+
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
287
|
+
|
|
288
|
+
export { QueryProvider, createDataContext, createStoreContext, downloadFile, getLocalStorage, mergeRefs, setLocalStorage, useCountdown, useDisclosure, useQueryContext, useValue };
|
|
289
|
+
export type { TCountdownActions, UseDisclosureReturn };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var store12s = require('./store-12s-s9lu_ZPn.js');
|
|
4
|
+
var queryStore12s = require('./queryStore-12s-JzzXvjJC.js');
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var useCountdown12s = require('./useCountdown-12s-uiqhgllY.js');
|
|
7
|
+
var useDisclosure12s = require('./useDisclosure-12s-SZtbSE4A.js');
|
|
8
|
+
var download12s = require('./download-12s-DKxkL92w.js');
|
|
9
|
+
var webStorage12s = require('./webStorage-12s-0RtNO_uc.js');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
17
|
+
*
|
|
18
|
+
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
19
|
+
* context with strict type inference. It returns a custom hook for consuming the context
|
|
20
|
+
* and a Provider component for supplying context values.
|
|
21
|
+
*
|
|
22
|
+
* @template T - The value type for the context.
|
|
23
|
+
* @returns {object} An object containing:
|
|
24
|
+
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
25
|
+
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Example usage:
|
|
29
|
+
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
30
|
+
*
|
|
31
|
+
* function Counter() {
|
|
32
|
+
* const { count } = useDataContext();
|
|
33
|
+
* return <span>{count}</span>;
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* function App() {
|
|
37
|
+
* return (
|
|
38
|
+
* <DataProvider value={{ count: 42 }}>
|
|
39
|
+
* <Counter />
|
|
40
|
+
* </DataProvider>
|
|
41
|
+
* );
|
|
42
|
+
* }
|
|
43
|
+
*/ const createDataContext = ()=>{
|
|
44
|
+
const Context = /*#__PURE__*/ React.createContext(undefined);
|
|
45
|
+
const useDataContext = ()=>{
|
|
46
|
+
const context = React.useContext(Context);
|
|
47
|
+
if (context === undefined) {
|
|
48
|
+
throw new Error(`useDataContext must be used within a DataProvider`);
|
|
49
|
+
}
|
|
50
|
+
return context;
|
|
51
|
+
};
|
|
52
|
+
const DataProvider = ({ children, value })=>{
|
|
53
|
+
const memoized = React.useMemo(()=>value, [
|
|
54
|
+
value
|
|
55
|
+
]);
|
|
56
|
+
return /*#__PURE__*/ React__default.default.createElement(Context.Provider, {
|
|
57
|
+
value: memoized
|
|
58
|
+
}, children);
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
useDataContext,
|
|
62
|
+
DataProvider
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A hook to manage a value.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
*
|
|
71
|
+
* ```tsx
|
|
72
|
+
* const MyComponent = ({ value }: { value?: number }) => {
|
|
73
|
+
* const [currentValue, setCurrentValue] = useValue({ value });
|
|
74
|
+
* };
|
|
75
|
+
* ```
|
|
76
|
+
*/ const useValue = ({ value, defaultValue })=>{
|
|
77
|
+
if (value === undefined && defaultValue === undefined) {
|
|
78
|
+
throw new Error('Either `value` or `defaultValue` must be provided.');
|
|
79
|
+
}
|
|
80
|
+
const isControlled = value !== undefined;
|
|
81
|
+
const [internalValue, setInternalValue] = React.useState(defaultValue);
|
|
82
|
+
const setValue = React.useCallback((newValue)=>{
|
|
83
|
+
if (!isControlled) {
|
|
84
|
+
setInternalValue(newValue);
|
|
85
|
+
}
|
|
86
|
+
}, [
|
|
87
|
+
isControlled
|
|
88
|
+
]);
|
|
89
|
+
const currentValue = isControlled ? value : internalValue;
|
|
90
|
+
return [
|
|
91
|
+
currentValue,
|
|
92
|
+
setValue
|
|
93
|
+
];
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
function mergeRefs(refs) {
|
|
97
|
+
return (value)=>{
|
|
98
|
+
refs.forEach((ref)=>{
|
|
99
|
+
if (typeof ref === 'function') {
|
|
100
|
+
ref(value);
|
|
101
|
+
} else if (ref != null) {
|
|
102
|
+
// eslint-disable-next-line no-param-reassign
|
|
103
|
+
ref.current = value;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
exports.createStoreContext = store12s.createStoreContext;
|
|
110
|
+
exports.QueryProvider = queryStore12s.QueryProvider;
|
|
111
|
+
exports.useQueryContext = queryStore12s.useQueryContext;
|
|
112
|
+
exports.useCountdown = useCountdown12s.useCountdown;
|
|
113
|
+
exports.useDisclosure = useDisclosure12s.useDisclosure;
|
|
114
|
+
exports.downloadFile = download12s.downloadFile;
|
|
115
|
+
exports.getLocalStorage = webStorage12s.getLocalStorage;
|
|
116
|
+
exports.setLocalStorage = webStorage12s.setLocalStorage;
|
|
117
|
+
exports.createDataContext = createDataContext;
|
|
118
|
+
exports.mergeRefs = mergeRefs;
|
|
119
|
+
exports.useValue = useValue;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import React, { ReactNode } from 'react';
|
|
2
|
-
import { AtLeastOne } from './types.js';
|
|
3
1
|
export * from './types.js';
|
|
4
2
|
|
|
5
3
|
/**
|
|
@@ -1183,162 +1181,6 @@ declare const validateDateString: (dateString: string, format: string) => boolea
|
|
|
1183
1181
|
*/
|
|
1184
1182
|
declare const isNil: (value: unknown) => value is null | undefined;
|
|
1185
1183
|
|
|
1186
|
-
type TQueryProps<Q> = {
|
|
1187
|
-
/**
|
|
1188
|
-
* the query object
|
|
1189
|
-
*/
|
|
1190
|
-
query: Partial<Q>;
|
|
1191
|
-
};
|
|
1192
|
-
type TQueryState<Q> = {
|
|
1193
|
-
/**
|
|
1194
|
-
* trigger the change of query
|
|
1195
|
-
*
|
|
1196
|
-
* @param query - the new query
|
|
1197
|
-
*/
|
|
1198
|
-
changeQuery: (query: TQueryProps<Q>['query']) => void;
|
|
1199
|
-
} & TQueryProps<Q>;
|
|
1200
|
-
type TInitialProps<Q> = Partial<TQueryProps<Q> & {
|
|
1201
|
-
/**
|
|
1202
|
-
* handle the change of query when calling `changeQuery`
|
|
1203
|
-
*
|
|
1204
|
-
* @param preQuery - the previous query
|
|
1205
|
-
* @param newQuery - the new query
|
|
1206
|
-
* @returns the custom new query
|
|
1207
|
-
*/
|
|
1208
|
-
handleChangeQuery: (preQuery: TQueryProps<Q>['query'], newQuery: TQueryProps<Q>['query']) => TQueryProps<Q>['query'];
|
|
1209
|
-
}>;
|
|
1210
|
-
/**
|
|
1211
|
-
* Provider to provide the store to the context
|
|
1212
|
-
*/
|
|
1213
|
-
declare const QueryProvider: <Q>({ children, query, handleChangeQuery, }: React.PropsWithChildren<TInitialProps<Q>>) => React.JSX.Element;
|
|
1214
|
-
/**
|
|
1215
|
-
* hook to get the store from the context
|
|
1216
|
-
*
|
|
1217
|
-
* because we want the return type of `selector` to be inferred by ts, we use HOF to implement the hook
|
|
1218
|
-
*
|
|
1219
|
-
* so you should use it like this:
|
|
1220
|
-
*
|
|
1221
|
-
* ```tsx
|
|
1222
|
-
* const useQuery = useQueryContext<MyObject>(); // => will return the store hook
|
|
1223
|
-
* const result = useQuery(q => q.query); // => will return the query object
|
|
1224
|
-
* ```
|
|
1225
|
-
*
|
|
1226
|
-
* @example
|
|
1227
|
-
*
|
|
1228
|
-
* ```tsx
|
|
1229
|
-
* const result1 = useQueryContext<MyObject>()(q => '1234');
|
|
1230
|
-
* const result2 = useQueryContext<MyObject>()(q => q.changeQuery);
|
|
1231
|
-
* const result3 = useQueryContext<MyObject>()(q => q.query);
|
|
1232
|
-
* ```
|
|
1233
|
-
*/
|
|
1234
|
-
declare const useQueryContext: <Q>() => <T>(selector: (state: TQueryState<Q>) => T, equalityFn?: (left: T, right: T) => boolean) => T;
|
|
1235
|
-
|
|
1236
|
-
/**
|
|
1237
|
-
* Creates a strongly-typed React Context and Provider pair for data sharing.
|
|
1238
|
-
*
|
|
1239
|
-
* This utility helps you avoid prop drilling by providing a reusable way to define
|
|
1240
|
-
* context with strict type inference. It returns a custom hook for consuming the context
|
|
1241
|
-
* and a Provider component for supplying context values.
|
|
1242
|
-
*
|
|
1243
|
-
* @template T - The value type for the context.
|
|
1244
|
-
* @returns {object} An object containing:
|
|
1245
|
-
* - useDataContext: A custom hook to access the context value. Throws an error if used outside the provider.
|
|
1246
|
-
* - DataProvider: A Provider component to wrap your component tree and supply the context value.
|
|
1247
|
-
*
|
|
1248
|
-
* @example
|
|
1249
|
-
* // Example usage:
|
|
1250
|
-
* const { useDataContext, DataProvider } = createDataContext<{ count: number }>();
|
|
1251
|
-
*
|
|
1252
|
-
* function Counter() {
|
|
1253
|
-
* const { count } = useDataContext();
|
|
1254
|
-
* return <span>{count}</span>;
|
|
1255
|
-
* }
|
|
1256
|
-
*
|
|
1257
|
-
* function App() {
|
|
1258
|
-
* return (
|
|
1259
|
-
* <DataProvider value={{ count: 42 }}>
|
|
1260
|
-
* <Counter />
|
|
1261
|
-
* </DataProvider>
|
|
1262
|
-
* );
|
|
1263
|
-
* }
|
|
1264
|
-
*/
|
|
1265
|
-
declare const createDataContext: <T>() => {
|
|
1266
|
-
readonly useDataContext: () => T & ({} | null);
|
|
1267
|
-
readonly DataProvider: ({ children, value }: {
|
|
1268
|
-
children: ReactNode;
|
|
1269
|
-
value: T;
|
|
1270
|
-
}) => React.JSX.Element;
|
|
1271
|
-
};
|
|
1272
|
-
|
|
1273
|
-
type TCountdownActions = {
|
|
1274
|
-
/** 目前秒數 */
|
|
1275
|
-
countdown: number;
|
|
1276
|
-
/** 是否正在倒數計時 */
|
|
1277
|
-
isCounting: boolean;
|
|
1278
|
-
/** 開始倒數計時 */
|
|
1279
|
-
start: () => void;
|
|
1280
|
-
/** 停止倒數計時 */
|
|
1281
|
-
stop: () => void;
|
|
1282
|
-
/** 重置倒數計時 */
|
|
1283
|
-
reset: () => void;
|
|
1284
|
-
};
|
|
1285
|
-
/**
|
|
1286
|
-
* 倒數計時器
|
|
1287
|
-
*
|
|
1288
|
-
* 可以透過 start() 來啟動倒數計時器
|
|
1289
|
-
* 可以透過 stop() 來停止倒數計時器
|
|
1290
|
-
* 可以透過 reset() 來重置倒數計時器
|
|
1291
|
-
*
|
|
1292
|
-
* @param initialCountdown 倒數計時器初始值
|
|
1293
|
-
* @param enableReinitialize 允許重設初始值
|
|
1294
|
-
*/
|
|
1295
|
-
declare const useCountdown: (initialCountdown: number, enableReinitialize?: boolean) => TCountdownActions;
|
|
1296
|
-
|
|
1297
|
-
type UseDisclosureReturn = {
|
|
1298
|
-
/** Whether the disclosure is currently open. */
|
|
1299
|
-
isOpen: boolean;
|
|
1300
|
-
/** Open the disclosure (sets isOpen = true). */
|
|
1301
|
-
open: () => void;
|
|
1302
|
-
/** Close the disclosure (sets isOpen = false). */
|
|
1303
|
-
close: () => void;
|
|
1304
|
-
/** Toggle the disclosure state (open -> close or close -> open). */
|
|
1305
|
-
toggle: () => void;
|
|
1306
|
-
};
|
|
1307
|
-
/**
|
|
1308
|
-
* A small hook to control open/close state.
|
|
1309
|
-
*
|
|
1310
|
-
* Supports an optional controlled pattern by passing `isOpen` and `onChange`.
|
|
1311
|
-
*
|
|
1312
|
-
* @example
|
|
1313
|
-
* const { isOpen, open, close, toggle } = useDisclosure();
|
|
1314
|
-
*/
|
|
1315
|
-
declare function useDisclosure(initialState?: boolean): UseDisclosureReturn;
|
|
1316
|
-
|
|
1317
|
-
type TValueOptions<T> = AtLeastOne<{
|
|
1318
|
-
/**
|
|
1319
|
-
* The controlled value.
|
|
1320
|
-
*/
|
|
1321
|
-
value?: T;
|
|
1322
|
-
/**
|
|
1323
|
-
* The default value.
|
|
1324
|
-
*/
|
|
1325
|
-
defaultValue?: T;
|
|
1326
|
-
}>;
|
|
1327
|
-
/**
|
|
1328
|
-
* A hook to manage a value.
|
|
1329
|
-
*
|
|
1330
|
-
* @example
|
|
1331
|
-
*
|
|
1332
|
-
* ```tsx
|
|
1333
|
-
* const MyComponent = ({ value }: { value?: number }) => {
|
|
1334
|
-
* const [currentValue, setCurrentValue] = useValue({ value });
|
|
1335
|
-
* };
|
|
1336
|
-
* ```
|
|
1337
|
-
*/
|
|
1338
|
-
declare const useValue: <T>({ value, defaultValue }: TValueOptions<T>) => readonly [T, (newValue: T) => void];
|
|
1339
|
-
|
|
1340
|
-
declare function mergeRefs<T = any>(refs: Array<React.MutableRefObject<T> | React.LegacyRef<T> | undefined | null>): React.RefCallback<T>;
|
|
1341
|
-
|
|
1342
1184
|
/**
|
|
1343
1185
|
* 民國年轉西元年
|
|
1344
1186
|
* @param dateString 日期字串
|
|
@@ -1390,38 +1232,5 @@ declare const generatePeriodArray: () => string[];
|
|
|
1390
1232
|
*/
|
|
1391
1233
|
declare const getCurrentPeriod: () => string;
|
|
1392
1234
|
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
*
|
|
1396
|
-
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
1397
|
-
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
1398
|
-
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
1399
|
-
*
|
|
1400
|
-
* @example
|
|
1401
|
-
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
1402
|
-
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
1403
|
-
*/
|
|
1404
|
-
declare const downloadFile: (source: string | Blob, filename?: string, fileExtension?: string) => void;
|
|
1405
|
-
|
|
1406
|
-
/**
|
|
1407
|
-
* 從 localStorage 取得資料,支援槽狀取值(Json 物件)
|
|
1408
|
-
*
|
|
1409
|
-
* @param key 鍵值
|
|
1410
|
-
* @param deCode 是否解碼
|
|
1411
|
-
* @returns 取得的資料
|
|
1412
|
-
* @example
|
|
1413
|
-
* const data = getLocalStorage('key');
|
|
1414
|
-
*
|
|
1415
|
-
* const data = getLocalStorage('key.subKey');
|
|
1416
|
-
*/
|
|
1417
|
-
declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefined;
|
|
1418
|
-
/**
|
|
1419
|
-
* 將資料(Json 物件)存入 localStorage
|
|
1420
|
-
* @param key 鍵值
|
|
1421
|
-
* @param value 可序列化的資料
|
|
1422
|
-
* @param enCode 是否編碼
|
|
1423
|
-
*/
|
|
1424
|
-
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
1425
|
-
|
|
1426
|
-
export { ByteSize, MimeTypeMap, OtherMimeType, QueryProvider, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createDataContext, createEnumLikeObject, debounce, decodeBase64, decodeJson, deepClone, deepMerge, downloadFile, encodeBase64, encodeJson, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getLocalStorage, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, mergeRefs, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, renameKey, rocEraToAd, searchParamsToObject, setLocalStorage, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, useCountdown, useDisclosure, useQueryContext, useValue, validTaxId, validateDateString, validateFileType, wait };
|
|
1427
|
-
export type { MimeTypeExtension, MimeTypeValue, PartialBy, RequiredBy, TCountdownActions, UseDisclosureReturn };
|
|
1235
|
+
export { ByteSize, MimeTypeMap, OtherMimeType, adToRocEra, camelCase2PascalCase, camelCase2SnakeCase, camelString2PascalString, camelString2SnakeString, convertBytes, createEnumLikeObject, debounce, decodeBase64, decodeJson, deepClone, deepMerge, encodeBase64, encodeJson, extractEnumLikeObject, fakeApi, formatAmount, formatBytes, formatStarMask, generatePeriodArray, getCurrentPeriod, getMimeType, invariant, isChinese, isDateString, isDateTimeString, isEmail, isEnglish, isEqual, isNil, isNonZeroStart, isNumber, isNumberAtLeastN, isNumberN, isNumberNM, isServer, isTWMobile, isTWPhone, isTimeString, isValidPassword, maskString, mergeConfig, objectToSearchParams, omit, omitByValue, parseFileInfoFromFilename, parseFilenameFromDisposition, pascalCase2CamelCase, pascalCase2SnakeCase, pascalString2CamelString, pascalString2SnakeString, pick, pickByValue, renameKey, rocEraToAd, searchParamsToObject, snakeCase2CamelCase, snakeCase2PascalCase, snakeString2CamelString, snakeString2PascalString, throttle, validTaxId, validateDateString, validateFileType, wait };
|
|
1236
|
+
export type { MimeTypeExtension, MimeTypeValue, PartialBy, RequiredBy };
|