react-store-input 0.2.4 → 0.2.6

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.
Files changed (45) hide show
  1. package/dist/create_render.d.mts +1 -1
  2. package/dist/create_render.d.ts +1 -1
  3. package/dist/create_render.js +3 -27
  4. package/dist/create_render.mjs +1 -23
  5. package/dist/index.d.mts +3 -3
  6. package/dist/index.d.ts +3 -3
  7. package/dist/index.js +119 -107
  8. package/dist/index.mjs +113 -99
  9. package/dist/text_editor.d.mts +13 -0
  10. package/dist/text_editor.d.ts +13 -0
  11. package/dist/text_editor.js +135 -0
  12. package/dist/text_editor.mjs +112 -0
  13. package/dist/use_form_store.d.mts +3 -1
  14. package/dist/use_form_store.d.ts +3 -1
  15. package/dist/use_form_store.js +111 -102
  16. package/dist/use_form_store.mjs +108 -97
  17. package/dist/use_store_component.d.mts +4 -1
  18. package/dist/use_store_component.d.ts +4 -1
  19. package/dist/use_store_component.js +97 -25
  20. package/dist/use_store_component.mjs +104 -30
  21. package/dist/use_store_controller.d.mts +1 -1
  22. package/dist/use_store_controller.d.ts +1 -1
  23. package/dist/use_store_controller.js +9 -17
  24. package/dist/use_store_controller.mjs +10 -18
  25. package/dist/use_store_input.d.mts +1 -1
  26. package/dist/use_store_input.d.ts +1 -1
  27. package/dist/use_store_input.js +9 -17
  28. package/dist/use_store_input.mjs +10 -18
  29. package/dist/use_store_input_with_name.d.mts +1 -1
  30. package/dist/use_store_input_with_name.d.ts +1 -1
  31. package/dist/use_store_input_with_name.js +9 -17
  32. package/dist/use_store_input_with_name.mjs +10 -18
  33. package/package.json +6 -4
  34. package/dist/use_selector.d.mts +0 -5
  35. package/dist/use_selector.d.ts +0 -5
  36. package/dist/use_selector.js +0 -50
  37. package/dist/use_selector.mjs +0 -25
  38. package/dist/use_store.d.mts +0 -12
  39. package/dist/use_store.d.ts +0 -12
  40. package/dist/use_store.js +0 -71
  41. package/dist/use_store.mjs +0 -46
  42. package/dist/use_subscribe.d.mts +0 -5
  43. package/dist/use_subscribe.d.ts +0 -5
  44. package/dist/use_subscribe.js +0 -38
  45. package/dist/use_subscribe.mjs +0 -13
package/dist/index.mjs CHANGED
@@ -1,91 +1,18 @@
1
- // src/use_store.tsx
2
- import { useRef } from "react";
3
- import { produce } from "immer";
4
- function useStore(initialState) {
5
- const stateRef = useRef(initialState);
6
- const subscribers = useRef([]);
7
- const dispatch = (recipe, options = {}) => {
8
- if (typeof recipe === "function") {
9
- stateRef.current = produce(stateRef.current, recipe);
10
- } else {
11
- stateRef.current = produce(
12
- stateRef.current,
13
- (draft) => {
14
- for (const key in recipe) {
15
- draft[key] = recipe[key];
16
- }
17
- }
18
- );
19
- }
20
- notify(options.key);
21
- };
22
- const subscribe = (subscriber) => {
23
- subscribers.current.push(subscriber);
24
- return () => {
25
- const index = subscribers.current.indexOf(subscriber);
26
- if (index !== -1) {
27
- subscribers.current.splice(index, 1);
28
- }
29
- };
30
- };
31
- const notify = (key) => {
32
- for (const listener of subscribers.current) {
33
- listener(stateRef.current, key);
34
- }
35
- };
36
- return {
37
- get state() {
38
- return stateRef.current;
39
- },
40
- dispatch,
41
- subscribe
42
- };
43
- }
44
-
45
- // src/use_selector.tsx
46
- import { useEffect, useRef as useRef2, useState } from "react";
47
- function useSelector(store, selector, compare = (a, b) => a === b) {
48
- const [state, setState] = useState(selector(store.state));
49
- const stateRef = useRef2(state);
50
- useEffect(() => {
51
- stateRef.current = state;
52
- }, [state]);
1
+ // src/use_store_controller.tsx
2
+ import { useEffect, useId } from "react";
3
+ function useStoreController(store, props) {
4
+ const dispatchKey = useId();
53
5
  useEffect(() => {
54
- const unsubscribe = store.subscribe((newState) => {
55
- const result = selector(newState);
56
- if (compare(stateRef.current, result)) {
6
+ const unsub = store.subscribe((state, key) => {
7
+ if (key === dispatchKey) {
57
8
  return;
58
9
  }
59
- setState(result);
10
+ props.onSubscribe(state);
60
11
  });
61
12
  return () => {
62
- unsubscribe();
63
- };
64
- }, []);
65
- return state;
66
- }
67
-
68
- // src/use_subscribe.tsx
69
- import { useEffect as useEffect2 } from "react";
70
- function useSubscribe(store, subscriber) {
71
- useEffect2(() => {
72
- const unsubscribe = store.subscribe(subscriber);
73
- return () => {
74
- unsubscribe();
13
+ unsub();
75
14
  };
76
15
  }, []);
77
- }
78
-
79
- // src/use_store_controller.tsx
80
- import { useId } from "react";
81
- function useStoreController(store, props) {
82
- const dispatchKey = useId();
83
- useSubscribe(store, (state, key) => {
84
- if (key === dispatchKey) {
85
- return;
86
- }
87
- props.onSubscribe(state);
88
- });
89
16
  const dispatch = () => {
90
17
  store.dispatch(
91
18
  (state) => {
@@ -218,22 +145,106 @@ function useStoreInputWithName(ref, store, props) {
218
145
  }
219
146
 
220
147
  // src/use_store_component.tsx
221
- import { useCallback, useRef as useRef3 } from "react";
148
+ import { useCallback, useRef as useRef2 } from "react";
149
+
150
+ // src/text_editor.tsx
151
+ import {
152
+ TextEditor as GwTextEditor
153
+ } from "gw-react-text-editor";
154
+ import { useImperativeHandle, useRef } from "react";
222
155
  import { jsx } from "react/jsx-runtime";
156
+ function TextEditor({
157
+ store,
158
+ name,
159
+ getter,
160
+ setter,
161
+ defaultValue,
162
+ ref,
163
+ ...props
164
+ }) {
165
+ const controllerRef = useRef(null);
166
+ useImperativeHandle(
167
+ ref,
168
+ () => controllerRef.current,
169
+ []
170
+ );
171
+ const { dispatch } = useStoreController(store, {
172
+ onSubscribe: (state) => {
173
+ const controller = controllerRef.current;
174
+ if (!controller) {
175
+ return;
176
+ }
177
+ const getResult = () => {
178
+ if (getter) {
179
+ return getter(state) || "";
180
+ }
181
+ if (name) {
182
+ return state[name] || "";
183
+ }
184
+ return "";
185
+ };
186
+ const result = getResult();
187
+ if (controller.value !== result) {
188
+ controller.value = result;
189
+ }
190
+ },
191
+ onDispatch: (state) => {
192
+ const controller = controllerRef.current;
193
+ if (!controller) {
194
+ return;
195
+ }
196
+ if (setter) {
197
+ setter(state, controller.value);
198
+ return;
199
+ }
200
+ if (name) {
201
+ state[name] = controller.value;
202
+ }
203
+ }
204
+ });
205
+ const getDefaultValue = () => {
206
+ if (getter) {
207
+ return getter(store.state);
208
+ }
209
+ if (name) {
210
+ return store.state[name];
211
+ }
212
+ return void 0;
213
+ };
214
+ return /* @__PURE__ */ jsx(
215
+ GwTextEditor,
216
+ {
217
+ ...props,
218
+ ref: controllerRef,
219
+ defaultValue: defaultValue ?? getDefaultValue(),
220
+ onChange: (e) => {
221
+ dispatch();
222
+ props.onChange?.(e);
223
+ }
224
+ }
225
+ );
226
+ }
227
+
228
+ // src/use_store_component.tsx
229
+ import { jsx as jsx2 } from "react/jsx-runtime";
223
230
  function useStoreComponent(store) {
224
231
  const input = useCallback(function Component(props) {
225
- return /* @__PURE__ */ jsx(Input, { store, ...props });
232
+ return /* @__PURE__ */ jsx2(Input, { store, ...props });
226
233
  }, []);
227
234
  const select = useCallback(function Component(props) {
228
- return /* @__PURE__ */ jsx(Select, { store, ...props });
235
+ return /* @__PURE__ */ jsx2(Select, { store, ...props });
229
236
  }, []);
230
237
  const textarea = useCallback(function Component(props) {
231
- return /* @__PURE__ */ jsx(Textarea, { store, ...props });
238
+ return /* @__PURE__ */ jsx2(Textarea, { store, ...props });
239
+ }, []);
240
+ const textEditor = useCallback(function Component(props) {
241
+ return /* @__PURE__ */ jsx2(TextEditor, { store, ...props });
232
242
  }, []);
233
243
  return {
234
244
  input,
235
245
  select,
236
- textarea
246
+ textarea,
247
+ textEditor
237
248
  };
238
249
  }
239
250
  function Input({
@@ -244,7 +255,7 @@ function Input({
244
255
  toStateValue,
245
256
  ...props
246
257
  }) {
247
- const ref = useRef3(null);
258
+ const ref = useRef2(null);
248
259
  const storeProps = useStoreInputWithName(ref, store, {
249
260
  ...props,
250
261
  getter,
@@ -252,7 +263,7 @@ function Input({
252
263
  toInputValue,
253
264
  toStateValue
254
265
  });
255
- return /* @__PURE__ */ jsx("input", { ref, ...props, ...storeProps });
266
+ return /* @__PURE__ */ jsx2("input", { ref, ...props, ...storeProps });
256
267
  }
257
268
  function Select({
258
269
  store,
@@ -262,7 +273,7 @@ function Select({
262
273
  toStateValue,
263
274
  ...props
264
275
  }) {
265
- const ref = useRef3(null);
276
+ const ref = useRef2(null);
266
277
  const storeProps = useStoreInputWithName(ref, store, {
267
278
  ...props,
268
279
  getter,
@@ -270,7 +281,7 @@ function Select({
270
281
  toInputValue,
271
282
  toStateValue
272
283
  });
273
- return /* @__PURE__ */ jsx("select", { ref, ...props, ...storeProps });
284
+ return /* @__PURE__ */ jsx2("select", { ref, ...props, ...storeProps });
274
285
  }
275
286
  function Textarea({
276
287
  store,
@@ -280,7 +291,7 @@ function Textarea({
280
291
  toStateValue,
281
292
  ...props
282
293
  }) {
283
- const ref = useRef3(null);
294
+ const ref = useRef2(null);
284
295
  const storeProps = useStoreInputWithName(ref, store, {
285
296
  ...props,
286
297
  getter,
@@ -288,23 +299,25 @@ function Textarea({
288
299
  toInputValue,
289
300
  toStateValue
290
301
  });
291
- return /* @__PURE__ */ jsx("textarea", { ref, ...props, ...storeProps });
302
+ return /* @__PURE__ */ jsx2("textarea", { ref, ...props, ...storeProps });
292
303
  }
293
304
 
294
305
  // src/create_render.tsx
295
- import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
306
+ import { useSelector } from "gw-store";
307
+ import { Fragment, jsx as jsx3 } from "react/jsx-runtime";
296
308
  function createRenderWithStore(store) {
297
309
  return function createRender(selector, compare) {
298
310
  function Component() {
299
311
  const result = useSelector(store, (state) => state, compare);
300
312
  const content = selector(result);
301
- return /* @__PURE__ */ jsx2(Fragment, { children: content });
313
+ return /* @__PURE__ */ jsx3(Fragment, { children: content });
302
314
  }
303
- return /* @__PURE__ */ jsx2(Component, {});
315
+ return /* @__PURE__ */ jsx3(Component, {});
304
316
  };
305
317
  }
306
318
 
307
319
  // src/use_form_store.tsx
320
+ import { useStore } from "gw-store";
308
321
  function useFormStore(initialState) {
309
322
  const store = useStore(initialState);
310
323
  const storeComponent = useStoreComponent(store);
@@ -318,16 +331,17 @@ function useFormStore(initialState) {
318
331
  ...storeComponent
319
332
  };
320
333
  }
334
+
335
+ // src/index.tsx
336
+ export * from "gw-store";
321
337
  export {
322
338
  Input,
323
339
  Select,
340
+ TextEditor,
324
341
  Textarea,
325
342
  useFormStore,
326
- useSelector,
327
- useStore,
328
343
  useStoreComponent,
329
344
  useStoreController,
330
345
  useStoreInput,
331
- useStoreInputWithName,
332
- useSubscribe
346
+ useStoreInputWithName
333
347
  };
@@ -0,0 +1,13 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { TextEditorProps as TextEditorProps$1 } from 'gw-react-text-editor';
3
+ import { Store } from 'gw-store';
4
+
5
+ type TextEditorProps<TState> = {
6
+ store: Store<TState>;
7
+ name?: keyof TState;
8
+ getter?: (state: TState) => string;
9
+ setter?: (state: TState, value: string) => void;
10
+ } & TextEditorProps$1;
11
+ declare function TextEditor<TState>({ store, name, getter, setter, defaultValue, ref, ...props }: TextEditorProps<TState>): react_jsx_runtime.JSX.Element;
12
+
13
+ export { TextEditor, type TextEditorProps };
@@ -0,0 +1,13 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { TextEditorProps as TextEditorProps$1 } from 'gw-react-text-editor';
3
+ import { Store } from 'gw-store';
4
+
5
+ type TextEditorProps<TState> = {
6
+ store: Store<TState>;
7
+ name?: keyof TState;
8
+ getter?: (state: TState) => string;
9
+ setter?: (state: TState, value: string) => void;
10
+ } & TextEditorProps$1;
11
+ declare function TextEditor<TState>({ store, name, getter, setter, defaultValue, ref, ...props }: TextEditorProps<TState>): react_jsx_runtime.JSX.Element;
12
+
13
+ export { TextEditor, type TextEditorProps };
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/text_editor.tsx
21
+ var text_editor_exports = {};
22
+ __export(text_editor_exports, {
23
+ TextEditor: () => TextEditor
24
+ });
25
+ module.exports = __toCommonJS(text_editor_exports);
26
+ var import_gw_react_text_editor = require("gw-react-text-editor");
27
+ var import_react2 = require("react");
28
+
29
+ // src/use_store_controller.tsx
30
+ var import_react = require("react");
31
+ function useStoreController(store, props) {
32
+ const dispatchKey = (0, import_react.useId)();
33
+ (0, import_react.useEffect)(() => {
34
+ const unsub = store.subscribe((state, key) => {
35
+ if (key === dispatchKey) {
36
+ return;
37
+ }
38
+ props.onSubscribe(state);
39
+ });
40
+ return () => {
41
+ unsub();
42
+ };
43
+ }, []);
44
+ const dispatch = () => {
45
+ store.dispatch(
46
+ (state) => {
47
+ props.onDispatch(state);
48
+ },
49
+ {
50
+ key: dispatchKey
51
+ }
52
+ );
53
+ };
54
+ return {
55
+ dispatch
56
+ };
57
+ }
58
+
59
+ // src/text_editor.tsx
60
+ var import_jsx_runtime = require("react/jsx-runtime");
61
+ function TextEditor({
62
+ store,
63
+ name,
64
+ getter,
65
+ setter,
66
+ defaultValue,
67
+ ref,
68
+ ...props
69
+ }) {
70
+ const controllerRef = (0, import_react2.useRef)(null);
71
+ (0, import_react2.useImperativeHandle)(
72
+ ref,
73
+ () => controllerRef.current,
74
+ []
75
+ );
76
+ const { dispatch } = useStoreController(store, {
77
+ onSubscribe: (state) => {
78
+ const controller = controllerRef.current;
79
+ if (!controller) {
80
+ return;
81
+ }
82
+ const getResult = () => {
83
+ if (getter) {
84
+ return getter(state) || "";
85
+ }
86
+ if (name) {
87
+ return state[name] || "";
88
+ }
89
+ return "";
90
+ };
91
+ const result = getResult();
92
+ if (controller.value !== result) {
93
+ controller.value = result;
94
+ }
95
+ },
96
+ onDispatch: (state) => {
97
+ const controller = controllerRef.current;
98
+ if (!controller) {
99
+ return;
100
+ }
101
+ if (setter) {
102
+ setter(state, controller.value);
103
+ return;
104
+ }
105
+ if (name) {
106
+ state[name] = controller.value;
107
+ }
108
+ }
109
+ });
110
+ const getDefaultValue = () => {
111
+ if (getter) {
112
+ return getter(store.state);
113
+ }
114
+ if (name) {
115
+ return store.state[name];
116
+ }
117
+ return void 0;
118
+ };
119
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
120
+ import_gw_react_text_editor.TextEditor,
121
+ {
122
+ ...props,
123
+ ref: controllerRef,
124
+ defaultValue: defaultValue ?? getDefaultValue(),
125
+ onChange: (e) => {
126
+ dispatch();
127
+ props.onChange?.(e);
128
+ }
129
+ }
130
+ );
131
+ }
132
+ // Annotate the CommonJS export names for ESM import in node:
133
+ 0 && (module.exports = {
134
+ TextEditor
135
+ });
@@ -0,0 +1,112 @@
1
+ // src/text_editor.tsx
2
+ import {
3
+ TextEditor as GwTextEditor
4
+ } from "gw-react-text-editor";
5
+ import { useImperativeHandle, useRef } from "react";
6
+
7
+ // src/use_store_controller.tsx
8
+ import { useEffect, useId } from "react";
9
+ function useStoreController(store, props) {
10
+ const dispatchKey = useId();
11
+ useEffect(() => {
12
+ const unsub = store.subscribe((state, key) => {
13
+ if (key === dispatchKey) {
14
+ return;
15
+ }
16
+ props.onSubscribe(state);
17
+ });
18
+ return () => {
19
+ unsub();
20
+ };
21
+ }, []);
22
+ const dispatch = () => {
23
+ store.dispatch(
24
+ (state) => {
25
+ props.onDispatch(state);
26
+ },
27
+ {
28
+ key: dispatchKey
29
+ }
30
+ );
31
+ };
32
+ return {
33
+ dispatch
34
+ };
35
+ }
36
+
37
+ // src/text_editor.tsx
38
+ import { jsx } from "react/jsx-runtime";
39
+ function TextEditor({
40
+ store,
41
+ name,
42
+ getter,
43
+ setter,
44
+ defaultValue,
45
+ ref,
46
+ ...props
47
+ }) {
48
+ const controllerRef = useRef(null);
49
+ useImperativeHandle(
50
+ ref,
51
+ () => controllerRef.current,
52
+ []
53
+ );
54
+ const { dispatch } = useStoreController(store, {
55
+ onSubscribe: (state) => {
56
+ const controller = controllerRef.current;
57
+ if (!controller) {
58
+ return;
59
+ }
60
+ const getResult = () => {
61
+ if (getter) {
62
+ return getter(state) || "";
63
+ }
64
+ if (name) {
65
+ return state[name] || "";
66
+ }
67
+ return "";
68
+ };
69
+ const result = getResult();
70
+ if (controller.value !== result) {
71
+ controller.value = result;
72
+ }
73
+ },
74
+ onDispatch: (state) => {
75
+ const controller = controllerRef.current;
76
+ if (!controller) {
77
+ return;
78
+ }
79
+ if (setter) {
80
+ setter(state, controller.value);
81
+ return;
82
+ }
83
+ if (name) {
84
+ state[name] = controller.value;
85
+ }
86
+ }
87
+ });
88
+ const getDefaultValue = () => {
89
+ if (getter) {
90
+ return getter(store.state);
91
+ }
92
+ if (name) {
93
+ return store.state[name];
94
+ }
95
+ return void 0;
96
+ };
97
+ return /* @__PURE__ */ jsx(
98
+ GwTextEditor,
99
+ {
100
+ ...props,
101
+ ref: controllerRef,
102
+ defaultValue: defaultValue ?? getDefaultValue(),
103
+ onChange: (e) => {
104
+ dispatch();
105
+ props.onChange?.(e);
106
+ }
107
+ }
108
+ );
109
+ }
110
+ export {
111
+ TextEditor
112
+ };
@@ -1,10 +1,12 @@
1
1
  import { CreateRender } from './create_render.mjs';
2
- import { Store } from './use_store.mjs';
2
+ import { Store } from 'gw-store';
3
3
  import { useStoreComponent } from './use_store_component.mjs';
4
4
  import 'react/jsx-runtime';
5
5
  import 'react';
6
6
  import './use_store_input_with_name.mjs';
7
7
  import './use_store_input.mjs';
8
+ import './text_editor.mjs';
9
+ import 'gw-react-text-editor';
8
10
 
9
11
  type FormStore<TState> = Store<TState> & ReturnType<typeof useStoreComponent<TState>> & {
10
12
  render: CreateRender<TState>;
@@ -1,10 +1,12 @@
1
1
  import { CreateRender } from './create_render.js';
2
- import { Store } from './use_store.js';
2
+ import { Store } from 'gw-store';
3
3
  import { useStoreComponent } from './use_store_component.js';
4
4
  import 'react/jsx-runtime';
5
5
  import 'react';
6
6
  import './use_store_input_with_name.js';
7
7
  import './use_store_input.js';
8
+ import './text_editor.js';
9
+ import 'gw-react-text-editor';
8
10
 
9
11
  type FormStore<TState> = Store<TState> & ReturnType<typeof useStoreComponent<TState>> & {
10
12
  render: CreateRender<TState>;