react-store-input 0.2.3 → 0.2.5

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 -31
  4. package/dist/create_render.mjs +1 -27
  5. package/dist/index.d.mts +3 -3
  6. package/dist/index.d.ts +3 -3
  7. package/dist/index.js +118 -104
  8. package/dist/index.mjs +110 -92
  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 +113 -95
  16. package/dist/use_form_store.mjs +105 -85
  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 +98 -26
  20. package/dist/use_store_component.mjs +105 -31
  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 +10 -18
  24. package/dist/use_store_controller.mjs +11 -19
  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 +10 -18
  28. package/dist/use_store_input.mjs +11 -19
  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 +10 -18
  32. package/dist/use_store_input_with_name.mjs +11 -19
  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 -54
  37. package/dist/use_selector.mjs +0 -29
  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
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { Store } from 'gw-store';
2
3
  import { ReactNode } from 'react';
3
- import { Store } from './use_store.mjs';
4
4
 
5
5
  type CreateRender<TState> = (selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean) => ReactNode;
6
6
  declare function createRender<TState>(store: Store<TState>, selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean): react_jsx_runtime.JSX.Element;
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { Store } from 'gw-store';
2
3
  import { ReactNode } from 'react';
3
- import { Store } from './use_store.js';
4
4
 
5
5
  type CreateRender<TState> = (selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean) => ReactNode;
6
6
  declare function createRender<TState>(store: Store<TState>, selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean): react_jsx_runtime.JSX.Element;
@@ -24,39 +24,11 @@ __export(create_render_exports, {
24
24
  createRenderWithStore: () => createRenderWithStore
25
25
  });
26
26
  module.exports = __toCommonJS(create_render_exports);
27
-
28
- // src/use_selector.tsx
29
- var import_react2 = require("react");
30
-
31
- // src/use_subscribe.tsx
32
- var import_react = require("react");
33
- function useSubscribe(store, subscriber) {
34
- (0, import_react.useEffect)(() => {
35
- const unsubscribe = store.subscribe(subscriber);
36
- return () => {
37
- unsubscribe();
38
- };
39
- }, [store]);
40
- }
41
-
42
- // src/use_selector.tsx
43
- function useSelector(store, selector, compare = (a, b) => a === b) {
44
- const [state, setState] = (0, import_react2.useState)(selector(store.state));
45
- useSubscribe(store, (newState) => {
46
- const result = selector(newState);
47
- if (compare(state, result)) {
48
- return;
49
- }
50
- setState(result);
51
- });
52
- return state;
53
- }
54
-
55
- // src/create_render.tsx
27
+ var import_gw_store = require("gw-store");
56
28
  var import_jsx_runtime = require("react/jsx-runtime");
57
29
  function createRender(store, selector, compare) {
58
30
  function Component() {
59
- const result = useSelector(store, (state) => state, compare);
31
+ const result = (0, import_gw_store.useSelector)(store, (state) => state, compare);
60
32
  const content = selector(result);
61
33
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: content });
62
34
  }
@@ -65,7 +37,7 @@ function createRender(store, selector, compare) {
65
37
  function createRenderWithStore(store) {
66
38
  return function createRender2(selector, compare) {
67
39
  function Component() {
68
- const result = useSelector(store, (state) => state, compare);
40
+ const result = (0, import_gw_store.useSelector)(store, (state) => state, compare);
69
41
  const content = selector(result);
70
42
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: content });
71
43
  }
@@ -1,31 +1,5 @@
1
- // src/use_selector.tsx
2
- import { useState } from "react";
3
-
4
- // src/use_subscribe.tsx
5
- import { useEffect } from "react";
6
- function useSubscribe(store, subscriber) {
7
- useEffect(() => {
8
- const unsubscribe = store.subscribe(subscriber);
9
- return () => {
10
- unsubscribe();
11
- };
12
- }, [store]);
13
- }
14
-
15
- // src/use_selector.tsx
16
- function useSelector(store, selector, compare = (a, b) => a === b) {
17
- const [state, setState] = useState(selector(store.state));
18
- useSubscribe(store, (newState) => {
19
- const result = selector(newState);
20
- if (compare(state, result)) {
21
- return;
22
- }
23
- setState(result);
24
- });
25
- return state;
26
- }
27
-
28
1
  // src/create_render.tsx
2
+ import { useSelector } from "gw-store";
29
3
  import { Fragment, jsx } from "react/jsx-runtime";
30
4
  function createRender(store, selector, compare) {
31
5
  function Component() {
package/dist/index.d.mts CHANGED
@@ -1,11 +1,11 @@
1
- export { Store, StoreSubscriber, useStore } from './use_store.mjs';
2
- export { useSelector } from './use_selector.mjs';
3
- export { useSubscribe } from './use_subscribe.mjs';
4
1
  export { StoreControllerProps, useStoreController } from './use_store_controller.mjs';
5
2
  export { InferNameFromProps, StoreInputWithNameProps, useStoreInputWithName } from './use_store_input_with_name.mjs';
6
3
  export { StoreInputProps, useStoreInput } from './use_store_input.mjs';
7
4
  export { Input, Select, StoreComponentPropsWithStore, StoreInputWithNameComponentProps, Textarea, useStoreComponent } from './use_store_component.mjs';
8
5
  export { FormStore, useFormStore } from './use_form_store.mjs';
6
+ export { TextEditor, TextEditorProps } from './text_editor.mjs';
7
+ import 'gw-store';
9
8
  import 'react';
10
9
  import 'react/jsx-runtime';
11
10
  import './create_render.mjs';
11
+ import 'gw-react-text-editor';
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- export { Store, StoreSubscriber, useStore } from './use_store.js';
2
- export { useSelector } from './use_selector.js';
3
- export { useSubscribe } from './use_subscribe.js';
4
1
  export { StoreControllerProps, useStoreController } from './use_store_controller.js';
5
2
  export { InferNameFromProps, StoreInputWithNameProps, useStoreInputWithName } from './use_store_input_with_name.js';
6
3
  export { StoreInputProps, useStoreInput } from './use_store_input.js';
7
4
  export { Input, Select, StoreComponentPropsWithStore, StoreInputWithNameComponentProps, Textarea, useStoreComponent } from './use_store_component.js';
8
5
  export { FormStore, useFormStore } from './use_form_store.js';
6
+ export { TextEditor, TextEditorProps } from './text_editor.js';
7
+ import 'gw-store';
9
8
  import 'react';
10
9
  import 'react/jsx-runtime';
11
10
  import './create_render.js';
11
+ import 'gw-react-text-editor';
package/dist/index.js CHANGED
@@ -22,99 +22,31 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  Input: () => Input,
24
24
  Select: () => Select,
25
+ TextEditor: () => TextEditor,
25
26
  Textarea: () => Textarea,
26
27
  useFormStore: () => useFormStore,
27
- useSelector: () => useSelector,
28
- useStore: () => useStore,
29
28
  useStoreComponent: () => useStoreComponent,
30
29
  useStoreController: () => useStoreController,
31
30
  useStoreInput: () => useStoreInput,
32
- useStoreInputWithName: () => useStoreInputWithName,
33
- useSubscribe: () => useSubscribe
31
+ useStoreInputWithName: () => useStoreInputWithName
34
32
  });
35
33
  module.exports = __toCommonJS(index_exports);
36
34
 
37
- // src/use_store.tsx
35
+ // src/use_store_controller.tsx
38
36
  var import_react = require("react");
39
- var import_immer = require("immer");
40
- function useStore(initialState) {
41
- const stateRef = (0, import_react.useRef)(initialState);
42
- const subscribers = (0, import_react.useRef)([]);
43
- const dispatch = (recipe, options = {}) => {
44
- if (typeof recipe === "function") {
45
- stateRef.current = (0, import_immer.produce)(stateRef.current, recipe);
46
- } else {
47
- stateRef.current = (0, import_immer.produce)(
48
- stateRef.current,
49
- (draft) => {
50
- for (const key in recipe) {
51
- draft[key] = recipe[key];
52
- }
53
- }
54
- );
55
- }
56
- notify(options.key);
57
- };
58
- const subscribe = (subscriber) => {
59
- subscribers.current.push(subscriber);
60
- return () => {
61
- const index = subscribers.current.indexOf(subscriber);
62
- if (index !== -1) {
63
- subscribers.current.splice(index, 1);
37
+ function useStoreController(store, props) {
38
+ const dispatchKey = (0, import_react.useId)();
39
+ (0, import_react.useEffect)(() => {
40
+ const unsub = store.subscribe((state, key) => {
41
+ if (key === dispatchKey) {
42
+ return;
64
43
  }
65
- };
66
- };
67
- const notify = (key) => {
68
- for (const listener of subscribers.current) {
69
- listener(stateRef.current, key);
70
- }
71
- };
72
- return {
73
- get state() {
74
- return stateRef.current;
75
- },
76
- dispatch,
77
- subscribe
78
- };
79
- }
80
-
81
- // src/use_selector.tsx
82
- var import_react3 = require("react");
83
-
84
- // src/use_subscribe.tsx
85
- var import_react2 = require("react");
86
- function useSubscribe(store, subscriber) {
87
- (0, import_react2.useEffect)(() => {
88
- const unsubscribe = store.subscribe(subscriber);
44
+ props.onSubscribe(state);
45
+ });
89
46
  return () => {
90
- unsubscribe();
47
+ unsub();
91
48
  };
92
- }, [store]);
93
- }
94
-
95
- // src/use_selector.tsx
96
- function useSelector(store, selector, compare = (a, b) => a === b) {
97
- const [state, setState] = (0, import_react3.useState)(selector(store.state));
98
- useSubscribe(store, (newState) => {
99
- const result = selector(newState);
100
- if (compare(state, result)) {
101
- return;
102
- }
103
- setState(result);
104
- });
105
- return state;
106
- }
107
-
108
- // src/use_store_controller.tsx
109
- var import_react4 = require("react");
110
- function useStoreController(store, props) {
111
- const dispatchKey = (0, import_react4.useId)();
112
- useSubscribe(store, (state, key) => {
113
- if (key === dispatchKey) {
114
- return;
115
- }
116
- props.onSubscribe(state);
117
- });
49
+ }, []);
118
50
  const dispatch = () => {
119
51
  store.dispatch(
120
52
  (state) => {
@@ -247,22 +179,104 @@ function useStoreInputWithName(ref, store, props) {
247
179
  }
248
180
 
249
181
  // src/use_store_component.tsx
250
- var import_react5 = require("react");
182
+ var import_react3 = require("react");
183
+
184
+ // src/text_editor.tsx
185
+ var import_gw_react_text_editor = require("gw-react-text-editor");
186
+ var import_react2 = require("react");
251
187
  var import_jsx_runtime = require("react/jsx-runtime");
188
+ function TextEditor({
189
+ store,
190
+ name,
191
+ getter,
192
+ setter,
193
+ defaultValue,
194
+ ref,
195
+ ...props
196
+ }) {
197
+ const controllerRef = (0, import_react2.useRef)(null);
198
+ (0, import_react2.useImperativeHandle)(
199
+ ref,
200
+ () => controllerRef.current,
201
+ []
202
+ );
203
+ const { dispatch } = useStoreController(store, {
204
+ onSubscribe: (state) => {
205
+ const controller = controllerRef.current;
206
+ if (!controller) {
207
+ return;
208
+ }
209
+ const getResult = () => {
210
+ if (getter) {
211
+ return getter(state) || "";
212
+ }
213
+ if (name) {
214
+ return state[name] || "";
215
+ }
216
+ return "";
217
+ };
218
+ const result = getResult();
219
+ if (controller.value !== result) {
220
+ controller.value = result;
221
+ }
222
+ },
223
+ onDispatch: (state) => {
224
+ const controller = controllerRef.current;
225
+ if (!controller) {
226
+ return;
227
+ }
228
+ if (setter) {
229
+ setter(state, controller.value);
230
+ return;
231
+ }
232
+ if (name) {
233
+ state[name] = controller.value;
234
+ }
235
+ }
236
+ });
237
+ const getDefaultValue = () => {
238
+ if (getter) {
239
+ return getter(store.state);
240
+ }
241
+ if (name) {
242
+ return store.state[name];
243
+ }
244
+ return void 0;
245
+ };
246
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
247
+ import_gw_react_text_editor.TextEditor,
248
+ {
249
+ ...props,
250
+ ref: controllerRef,
251
+ defaultValue: defaultValue ?? getDefaultValue(),
252
+ onChange: (e) => {
253
+ dispatch();
254
+ props.onChange?.(e);
255
+ }
256
+ }
257
+ );
258
+ }
259
+
260
+ // src/use_store_component.tsx
261
+ var import_jsx_runtime2 = require("react/jsx-runtime");
252
262
  function useStoreComponent(store) {
253
- const input = (0, import_react5.useCallback)(function Component(props) {
254
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Input, { store, ...props });
263
+ const input = (0, import_react3.useCallback)(function Component(props) {
264
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Input, { store, ...props });
255
265
  }, []);
256
- const select = (0, import_react5.useCallback)(function Component(props) {
257
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Select, { store, ...props });
266
+ const select = (0, import_react3.useCallback)(function Component(props) {
267
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Select, { store, ...props });
258
268
  }, []);
259
- const textarea = (0, import_react5.useCallback)(function Component(props) {
260
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Textarea, { store, ...props });
269
+ const textarea = (0, import_react3.useCallback)(function Component(props) {
270
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Textarea, { store, ...props });
271
+ }, []);
272
+ const textEditor = (0, import_react3.useCallback)(function Component(props) {
273
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(TextEditor, { store, ...props });
261
274
  }, []);
262
275
  return {
263
276
  input,
264
277
  select,
265
- textarea
278
+ textarea,
279
+ textEditor
266
280
  };
267
281
  }
268
282
  function Input({
@@ -273,7 +287,7 @@ function Input({
273
287
  toStateValue,
274
288
  ...props
275
289
  }) {
276
- const ref = (0, import_react5.useRef)(null);
290
+ const ref = (0, import_react3.useRef)(null);
277
291
  const storeProps = useStoreInputWithName(ref, store, {
278
292
  ...props,
279
293
  getter,
@@ -281,7 +295,7 @@ function Input({
281
295
  toInputValue,
282
296
  toStateValue
283
297
  });
284
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { ref, ...props, ...storeProps });
298
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { ref, ...props, ...storeProps });
285
299
  }
286
300
  function Select({
287
301
  store,
@@ -291,7 +305,7 @@ function Select({
291
305
  toStateValue,
292
306
  ...props
293
307
  }) {
294
- const ref = (0, import_react5.useRef)(null);
308
+ const ref = (0, import_react3.useRef)(null);
295
309
  const storeProps = useStoreInputWithName(ref, store, {
296
310
  ...props,
297
311
  getter,
@@ -299,7 +313,7 @@ function Select({
299
313
  toInputValue,
300
314
  toStateValue
301
315
  });
302
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("select", { ref, ...props, ...storeProps });
316
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("select", { ref, ...props, ...storeProps });
303
317
  }
304
318
  function Textarea({
305
319
  store,
@@ -309,7 +323,7 @@ function Textarea({
309
323
  toStateValue,
310
324
  ...props
311
325
  }) {
312
- const ref = (0, import_react5.useRef)(null);
326
+ const ref = (0, import_react3.useRef)(null);
313
327
  const storeProps = useStoreInputWithName(ref, store, {
314
328
  ...props,
315
329
  getter,
@@ -317,25 +331,27 @@ function Textarea({
317
331
  toInputValue,
318
332
  toStateValue
319
333
  });
320
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("textarea", { ref, ...props, ...storeProps });
334
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("textarea", { ref, ...props, ...storeProps });
321
335
  }
322
336
 
323
337
  // src/create_render.tsx
324
- var import_jsx_runtime2 = require("react/jsx-runtime");
338
+ var import_gw_store = require("gw-store");
339
+ var import_jsx_runtime3 = require("react/jsx-runtime");
325
340
  function createRenderWithStore(store) {
326
341
  return function createRender(selector, compare) {
327
342
  function Component() {
328
- const result = useSelector(store, (state) => state, compare);
343
+ const result = (0, import_gw_store.useSelector)(store, (state) => state, compare);
329
344
  const content = selector(result);
330
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: content });
345
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: content });
331
346
  }
332
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, {});
347
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Component, {});
333
348
  };
334
349
  }
335
350
 
336
351
  // src/use_form_store.tsx
352
+ var import_gw_store2 = require("gw-store");
337
353
  function useFormStore(initialState) {
338
- const store = useStore(initialState);
354
+ const store = (0, import_gw_store2.useStore)(initialState);
339
355
  const storeComponent = useStoreComponent(store);
340
356
  return {
341
357
  get state() {
@@ -351,13 +367,11 @@ function useFormStore(initialState) {
351
367
  0 && (module.exports = {
352
368
  Input,
353
369
  Select,
370
+ TextEditor,
354
371
  Textarea,
355
372
  useFormStore,
356
- useSelector,
357
- useStore,
358
373
  useStoreComponent,
359
374
  useStoreController,
360
375
  useStoreInput,
361
- useStoreInputWithName,
362
- useSubscribe
376
+ useStoreInputWithName
363
377
  });