define-zustand 3.0.4 → 3.1.1

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/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # define-zustand
2
+
3
+ > Quickly define [zustand](https://github.com/pmndrs/zustand) state
4
+
5
+ ## Install
6
+
7
+ ```shell
8
+ pnpm i define-zustand
9
+ npm i define-zustand
10
+ ```
11
+
12
+ ## Use
13
+
14
+ ```tsx
15
+ import { defineStore, defineContext, defineStateFactory } from 'define-zustand'
16
+
17
+ /**
18
+ * @return zustand hooks
19
+ */
20
+ const useStore = defineStore({
21
+ state: () => ({
22
+ a: 1,
23
+ b: 1
24
+ }),
25
+ action: () => ({}),
26
+ getter: {
27
+ count: state => state.a + state.b
28
+ }
29
+ })
30
+
31
+ // Use defineModelFactory
32
+ const stateFactory = defineStateFactory({
33
+ state: () => ({
34
+ a: 1,
35
+ b: 1
36
+ }),
37
+ action: () => ({}),
38
+ getter: {
39
+ count: state => state.a + state.b
40
+ }
41
+ })
42
+ const useStore = defineStore(stateFactory())
43
+ const { Provider, useContext } = defineContext(stateFactory())
44
+
45
+ function Child() {
46
+ const count = useContext(state => state.count)
47
+ return <div>{count}</div>
48
+ }
49
+ function ReactComponent() {
50
+ const count = useStore(state => state.count)
51
+ const setState = useStore(state => state.setState)
52
+ const reset = useStore(state => state.reset)
53
+
54
+ useEffect(() => {
55
+ return () => {
56
+ reset()
57
+ }
58
+ }, [])
59
+
60
+ return (
61
+ <Provider>
62
+ <div>
63
+ <button onClick={() => setState({ a: 2 })}>setA</button>
64
+ <button
65
+ onClick={() =>
66
+ setState(state => {
67
+ state.b += 1
68
+ })
69
+ }
70
+ >
71
+ setB
72
+ </button>
73
+ <div>{count}</div>
74
+ </div>
75
+ <Child />
76
+ </Provider>
77
+ )
78
+ }
79
+ ```
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { creator, defineContext, defineStore } from './utils.js';
1
+ export { creator, defineContext, defineStateFactory, defineStore } from './utils.js';
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { FunctionComponent, ReactNode } from 'react';
2
2
  import { StateCreator } from 'zustand';
3
3
  import { defGetterStateType, optionsType, ExtraGetterState, insideActionsType } from './types';
4
+ export declare function defineStateFactory<Options extends optionsType<any, any, any>>(options: Options): () => Options;
4
5
  export declare function creator<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): StateCreator<S & ExtraGetterState<S, G> & insideActionsType<S> & OptionActions>;
5
6
  export declare function defineStore<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): import("zustand").UseBoundStore<import("zustand").StoreApi<S & ExtraGetterState<S, G> & insideActionsType<S> & OptionActions>>;
6
7
  export declare function defineContext<S extends Record<string, any>, G extends defGetterStateType<S>, OptionActions extends Record<string, any>>(options: optionsType<S, G, OptionActions>): {
package/dist/utils.js CHANGED
@@ -4,6 +4,9 @@ import { produce } from 'immer';
4
4
  import { keys, isEqualWith, omit, some, pick, forEach, isEqual } from 'lodash-es';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
 
7
+ function defineStateFactory(options) {
8
+ return () => options;
9
+ }
7
10
  function creator(options) {
8
11
  function createDefState() {
9
12
  const state = options.state();
@@ -77,4 +80,4 @@ function defineContext(options) {
77
80
  };
78
81
  }
79
82
 
80
- export { creator, defineContext, defineStore };
83
+ export { creator, defineContext, defineStateFactory, defineStore };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "define-zustand",
3
- "version": "3.0.4",
3
+ "version": "3.1.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,7 +9,6 @@
9
9
  ],
10
10
  "license": "ISC",
11
11
  "devDependencies": {
12
- "1k-tasks": "^3.6.0",
13
12
  "@types/jest": "^29.5.2",
14
13
  "@types/lodash-es": "^4.17.8",
15
14
  "@typescript-eslint/eslint-plugin": "5.60.1",
@@ -20,21 +19,21 @@
20
19
  "eslint-plugin-prettier": "4.2.1",
21
20
  "jest": "^29.5.0",
22
21
  "prettier": "2.8.8",
23
- "react": "~18.2.0",
22
+ "react": "~18.3.1",
24
23
  "stylelint": "15.9.0",
25
24
  "stylelint-config-standard": "33.0.0",
26
25
  "ts-jest": "29.0.5",
27
26
  "typescript": "5.1.3",
28
- "zustand": "^4.4.1"
27
+ "zustand": "4.4.1"
29
28
  },
30
29
  "dependencies": {
31
- "@types/react": "~18.2.28",
32
- "immer": "^10.0.2",
33
- "lodash-es": "^4.17.21"
30
+ "@types/react": "18.3.3",
31
+ "immer": "10.0.2",
32
+ "lodash-es": "4.17.21"
34
33
  },
35
34
  "peerDependencies": {
36
- "react": "~18.2.0",
37
- "zustand": "^4.4.1"
35
+ "react": "~18.3.1",
36
+ "zustand": "~4.4.1"
38
37
  },
39
38
  "scripts": {
40
39
  "build": "node scripts/build.js",