@gateweb/react-utils 1.7.0 → 1.8.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/index.d.ts +328 -130
- package/dist/cjs/index.js +638 -403
- package/dist/cjs/queryStore-client-q_SLGgYH.js +77 -0
- package/dist/cjs/{useCountdown-client-CNjGBIUB.js → useCountdown-client-uiqhgllY.js} +8 -8
- package/dist/es/index.d.mts +328 -130
- package/dist/es/index.mjs +627 -399
- package/dist/es/queryStore-client-vG-bXFYm.mjs +72 -0
- package/package.json +2 -2
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React, { useRef, useContext, createContext } from 'react';
|
|
3
|
+
import { createStore } from 'zustand';
|
|
4
|
+
import { useStoreWithEqualityFn } from 'zustand/traditional';
|
|
5
|
+
|
|
6
|
+
const createQueryStore = ({ query, handleChangeQuery })=>createStore()((set)=>({
|
|
7
|
+
query: {
|
|
8
|
+
...query
|
|
9
|
+
},
|
|
10
|
+
changeQuery: (newQuery)=>{
|
|
11
|
+
set((pre)=>{
|
|
12
|
+
if (handleChangeQuery) {
|
|
13
|
+
return {
|
|
14
|
+
query: handleChangeQuery(pre.query, newQuery)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
query: {
|
|
19
|
+
...pre.query,
|
|
20
|
+
...newQuery
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}));
|
|
26
|
+
const QueryContext = /*#__PURE__*/ createContext(null);
|
|
27
|
+
/**
|
|
28
|
+
* Provider to provide the store to the context
|
|
29
|
+
*/ const QueryProvider = ({ children, query, handleChangeQuery })=>{
|
|
30
|
+
const storeRef = useRef(null);
|
|
31
|
+
if (!storeRef.current) {
|
|
32
|
+
storeRef.current = createQueryStore({
|
|
33
|
+
query,
|
|
34
|
+
handleChangeQuery
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return /*#__PURE__*/ React.createElement(QueryContext.Provider, {
|
|
38
|
+
value: storeRef.current
|
|
39
|
+
}, children);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* hook to get the store from the context
|
|
43
|
+
*
|
|
44
|
+
* because we want the return type of `selector` to be inferred by ts, we use HOF to implement the hook
|
|
45
|
+
*
|
|
46
|
+
* so you should use it like this:
|
|
47
|
+
*
|
|
48
|
+
* ```tsx
|
|
49
|
+
* const useQuery = useQueryContext<MyObject>(); // => will return the store hook
|
|
50
|
+
* const result = useQuery(q => q.query); // => will return the query object
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
*
|
|
55
|
+
* ```tsx
|
|
56
|
+
* const result1 = useQueryContext<MyObject>()(q => '1234');
|
|
57
|
+
* const result2 = useQueryContext<MyObject>()(q => q.changeQuery);
|
|
58
|
+
* const result3 = useQueryContext<MyObject>()(q => q.query);
|
|
59
|
+
* ```
|
|
60
|
+
*/ const useQueryContext = ()=>{
|
|
61
|
+
const store = useContext(QueryContext);
|
|
62
|
+
if (!store) throw new Error('Missing QueryContext.Provider in the tree');
|
|
63
|
+
/**
|
|
64
|
+
* the hook to get the store
|
|
65
|
+
*
|
|
66
|
+
* @param selector - the selector to get the state from the store
|
|
67
|
+
* @param equalityFn - the equality function to compare the previous and next state, if it returns `true`, the component will not re-render
|
|
68
|
+
*/ const useStore = (selector, equalityFn)=>useStoreWithEqualityFn(store, selector, equalityFn);
|
|
69
|
+
return useStore;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { QueryProvider as Q, useQueryContext as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gateweb/react-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "React Utils for GateWeb",
|
|
5
5
|
"homepage": "https://github.com/GatewebSolutions/react-utils",
|
|
6
6
|
"files": [
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"run-code": "ts-node src/period.ts",
|
|
68
|
-
"test": "vitest",
|
|
68
|
+
"test": "vitest run",
|
|
69
69
|
"test:coverage": "vitest run --coverage",
|
|
70
70
|
"test:ui": "vitest --ui --coverage.enabled=true",
|
|
71
71
|
"build": "bunchee",
|