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
package/dist/index.mjs CHANGED
@@ -1,84 +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 { useState } from "react";
47
-
48
- // src/use_subscribe.tsx
49
- import { useEffect } from "react";
50
- function useSubscribe(store, subscriber) {
51
- useEffect(() => {
52
- const unsubscribe = store.subscribe(subscriber);
53
- return () => {
54
- unsubscribe();
55
- };
56
- }, [store]);
57
- }
58
-
59
- // src/use_selector.tsx
60
- function useSelector(store, selector, compare = (a, b) => a === b) {
61
- const [state, setState] = useState(selector(store.state));
62
- useSubscribe(store, (newState) => {
63
- const result = selector(newState);
64
- if (compare(state, result)) {
65
- return;
66
- }
67
- setState(result);
68
- });
69
- return state;
70
- }
71
-
72
1
  // src/use_store_controller.tsx
73
- import { useId } from "react";
2
+ import { useEffect, useId } from "react";
74
3
  function useStoreController(store, props) {
75
4
  const dispatchKey = useId();
76
- useSubscribe(store, (state, key) => {
77
- if (key === dispatchKey) {
78
- return;
79
- }
80
- props.onSubscribe(state);
81
- });
5
+ useEffect(() => {
6
+ const unsub = store.subscribe((state, key) => {
7
+ if (key === dispatchKey) {
8
+ return;
9
+ }
10
+ props.onSubscribe(state);
11
+ });
12
+ return () => {
13
+ unsub();
14
+ };
15
+ }, []);
82
16
  const dispatch = () => {
83
17
  store.dispatch(
84
18
  (state) => {
@@ -212,21 +146,105 @@ function useStoreInputWithName(ref, store, props) {
212
146
 
213
147
  // src/use_store_component.tsx
214
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";
215
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";
216
230
  function useStoreComponent(store) {
217
231
  const input = useCallback(function Component(props) {
218
- return /* @__PURE__ */ jsx(Input, { store, ...props });
232
+ return /* @__PURE__ */ jsx2(Input, { store, ...props });
219
233
  }, []);
220
234
  const select = useCallback(function Component(props) {
221
- return /* @__PURE__ */ jsx(Select, { store, ...props });
235
+ return /* @__PURE__ */ jsx2(Select, { store, ...props });
222
236
  }, []);
223
237
  const textarea = useCallback(function Component(props) {
224
- 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 });
225
242
  }, []);
226
243
  return {
227
244
  input,
228
245
  select,
229
- textarea
246
+ textarea,
247
+ textEditor
230
248
  };
231
249
  }
232
250
  function Input({
@@ -245,7 +263,7 @@ function Input({
245
263
  toInputValue,
246
264
  toStateValue
247
265
  });
248
- return /* @__PURE__ */ jsx("input", { ref, ...props, ...storeProps });
266
+ return /* @__PURE__ */ jsx2("input", { ref, ...props, ...storeProps });
249
267
  }
250
268
  function Select({
251
269
  store,
@@ -263,7 +281,7 @@ function Select({
263
281
  toInputValue,
264
282
  toStateValue
265
283
  });
266
- return /* @__PURE__ */ jsx("select", { ref, ...props, ...storeProps });
284
+ return /* @__PURE__ */ jsx2("select", { ref, ...props, ...storeProps });
267
285
  }
268
286
  function Textarea({
269
287
  store,
@@ -281,23 +299,25 @@ function Textarea({
281
299
  toInputValue,
282
300
  toStateValue
283
301
  });
284
- return /* @__PURE__ */ jsx("textarea", { ref, ...props, ...storeProps });
302
+ return /* @__PURE__ */ jsx2("textarea", { ref, ...props, ...storeProps });
285
303
  }
286
304
 
287
305
  // src/create_render.tsx
288
- 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";
289
308
  function createRenderWithStore(store) {
290
309
  return function createRender(selector, compare) {
291
310
  function Component() {
292
311
  const result = useSelector(store, (state) => state, compare);
293
312
  const content = selector(result);
294
- return /* @__PURE__ */ jsx2(Fragment, { children: content });
313
+ return /* @__PURE__ */ jsx3(Fragment, { children: content });
295
314
  }
296
- return /* @__PURE__ */ jsx2(Component, {});
315
+ return /* @__PURE__ */ jsx3(Component, {});
297
316
  };
298
317
  }
299
318
 
300
319
  // src/use_form_store.tsx
320
+ import { useStore } from "gw-store";
301
321
  function useFormStore(initialState) {
302
322
  const store = useStore(initialState);
303
323
  const storeComponent = useStoreComponent(store);
@@ -314,13 +334,11 @@ function useFormStore(initialState) {
314
334
  export {
315
335
  Input,
316
336
  Select,
337
+ TextEditor,
317
338
  Textarea,
318
339
  useFormStore,
319
- useSelector,
320
- useStore,
321
340
  useStoreComponent,
322
341
  useStoreController,
323
342
  useStoreInput,
324
- useStoreInputWithName,
325
- useSubscribe
343
+ useStoreInputWithName
326
344
  };
@@ -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>;