react-store-input 0.2.6 → 0.5.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/README.md +211 -199
  3. package/dist/index.d.mts +127 -11
  4. package/dist/index.d.ts +127 -11
  5. package/dist/index.js +542 -280
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +529 -272
  8. package/dist/index.mjs.map +1 -0
  9. package/dist/text-editor.d.mts +11 -0
  10. package/dist/text-editor.d.ts +11 -0
  11. package/dist/text-editor.js +203 -0
  12. package/dist/text-editor.js.map +1 -0
  13. package/dist/text-editor.mjs +178 -0
  14. package/dist/text-editor.mjs.map +1 -0
  15. package/package.json +78 -18
  16. package/dist/create_render.d.mts +0 -9
  17. package/dist/create_render.d.ts +0 -9
  18. package/dist/create_render.js +0 -51
  19. package/dist/create_render.mjs +0 -25
  20. package/dist/text_editor.d.mts +0 -13
  21. package/dist/text_editor.d.ts +0 -13
  22. package/dist/text_editor.js +0 -135
  23. package/dist/text_editor.mjs +0 -112
  24. package/dist/use_form_store.d.mts +0 -16
  25. package/dist/use_form_store.d.ts +0 -16
  26. package/dist/use_form_store.js +0 -365
  27. package/dist/use_form_store.mjs +0 -340
  28. package/dist/use_store_component.d.mts +0 -23
  29. package/dist/use_store_component.d.ts +0 -23
  30. package/dist/use_store_component.js +0 -337
  31. package/dist/use_store_component.mjs +0 -311
  32. package/dist/use_store_controller.d.mts +0 -11
  33. package/dist/use_store_controller.d.ts +0 -11
  34. package/dist/use_store_controller.js +0 -57
  35. package/dist/use_store_controller.mjs +0 -32
  36. package/dist/use_store_input.d.mts +0 -22
  37. package/dist/use_store_input.d.ts +0 -22
  38. package/dist/use_store_input.js +0 -151
  39. package/dist/use_store_input.mjs +0 -126
  40. package/dist/use_store_input_with_name.d.mts +0 -17
  41. package/dist/use_store_input_with_name.d.ts +0 -17
  42. package/dist/use_store_input_with_name.js +0 -177
  43. package/dist/use_store_input_with_name.mjs +0 -150
  44. package/tsup.config.ts +0 -9
@@ -1,365 +0,0 @@
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/use_form_store.tsx
21
- var use_form_store_exports = {};
22
- __export(use_form_store_exports, {
23
- useFormStore: () => useFormStore
24
- });
25
- module.exports = __toCommonJS(use_form_store_exports);
26
-
27
- // src/create_render.tsx
28
- var import_gw_store = require("gw-store");
29
- var import_jsx_runtime = require("react/jsx-runtime");
30
- function createRenderWithStore(store) {
31
- return function createRender(selector, compare) {
32
- function Component() {
33
- const result = (0, import_gw_store.useSelector)(store, (state) => state, compare);
34
- const content = selector(result);
35
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: content });
36
- }
37
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, {});
38
- };
39
- }
40
-
41
- // src/use_form_store.tsx
42
- var import_gw_store2 = require("gw-store");
43
-
44
- // src/use_store_component.tsx
45
- var import_react3 = require("react");
46
-
47
- // src/use_store_input.tsx
48
- var import_date_fns = require("date-fns");
49
-
50
- // src/use_store_controller.tsx
51
- var import_react = require("react");
52
- function useStoreController(store, props) {
53
- const dispatchKey = (0, import_react.useId)();
54
- (0, import_react.useEffect)(() => {
55
- const unsub = store.subscribe((state, key) => {
56
- if (key === dispatchKey) {
57
- return;
58
- }
59
- props.onSubscribe(state);
60
- });
61
- return () => {
62
- unsub();
63
- };
64
- }, []);
65
- const dispatch = () => {
66
- store.dispatch(
67
- (state) => {
68
- props.onDispatch(state);
69
- },
70
- {
71
- key: dispatchKey
72
- }
73
- );
74
- };
75
- return {
76
- dispatch
77
- };
78
- }
79
-
80
- // src/use_store_input.tsx
81
- function useStoreInput(ref, store, props) {
82
- const toInputValue = props.toInputValue || ((value) => {
83
- if (value === void 0 || value === null) {
84
- return "";
85
- }
86
- if (props.type === "datetime-local") {
87
- const date = new Date(value);
88
- if (!isNaN(date.getTime())) {
89
- return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
90
- }
91
- }
92
- return String(value);
93
- });
94
- const getDefaultValue = () => {
95
- if (props.defaultValue !== void 0) {
96
- return props.defaultValue;
97
- }
98
- if (props.type === "checkbox" || props.type === "radio") {
99
- return void 0;
100
- }
101
- return toInputValue(props.getter(store.state));
102
- };
103
- const toInputChecked = (value) => {
104
- if (props.type === "radio") {
105
- return value !== void 0 && value === props.value;
106
- }
107
- return Boolean(value);
108
- };
109
- const getDefaultChecked = () => {
110
- if (props.defaultChecked) {
111
- return props.defaultChecked;
112
- }
113
- if (props.type !== "checkbox" && props.type !== "radio") {
114
- return void 0;
115
- }
116
- return toInputChecked(props.getter(store.state));
117
- };
118
- const toStateValue = props.toStateValue || ((value) => {
119
- if (props.type === "number" || props.type === "range") {
120
- return Number(value);
121
- }
122
- if (props.type === "datetime-local") {
123
- return new Date(value);
124
- }
125
- return value;
126
- });
127
- const inputProps = useStoreController(store, {
128
- onSubscribe: (state) => {
129
- const element = ref.current;
130
- if (!element) {
131
- return;
132
- }
133
- if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
134
- const checked = toInputChecked(props.getter(state));
135
- if (element.checked === checked) {
136
- return;
137
- }
138
- element.checked = checked;
139
- } else {
140
- const value = toInputValue(props.getter(state));
141
- if (element.value === value) {
142
- return;
143
- }
144
- element.value = value;
145
- }
146
- const event = new Event("input", { bubbles: true });
147
- element.dispatchEvent(event);
148
- },
149
- onDispatch: (state) => {
150
- const element = ref.current;
151
- if (!element) {
152
- return;
153
- }
154
- if ("checked" in element && props.type === "checkbox") {
155
- props.setter(state, element.checked);
156
- } else {
157
- props.setter(state, toStateValue(element.value));
158
- }
159
- }
160
- });
161
- return {
162
- defaultValue: getDefaultValue(),
163
- defaultChecked: getDefaultChecked(),
164
- onChange: (event) => {
165
- inputProps.dispatch();
166
- props.onChange?.(event);
167
- }
168
- };
169
- }
170
-
171
- // src/use_store_input_with_name.tsx
172
- function useStoreInputWithName(ref, store, props) {
173
- const inputProps = useStoreInput(ref, store, {
174
- ...props,
175
- getter: (state) => {
176
- if (props.getter) {
177
- return props.getter(state);
178
- }
179
- return state[props.name];
180
- },
181
- setter: (state, value) => {
182
- if (props.setter) {
183
- props.setter(state, value);
184
- return;
185
- }
186
- state[props.name] = value;
187
- }
188
- });
189
- return {
190
- ...inputProps,
191
- name: "name" in props ? String(props.name) : void 0
192
- };
193
- }
194
-
195
- // src/text_editor.tsx
196
- var import_gw_react_text_editor = require("gw-react-text-editor");
197
- var import_react2 = require("react");
198
- var import_jsx_runtime2 = require("react/jsx-runtime");
199
- function TextEditor({
200
- store,
201
- name,
202
- getter,
203
- setter,
204
- defaultValue,
205
- ref,
206
- ...props
207
- }) {
208
- const controllerRef = (0, import_react2.useRef)(null);
209
- (0, import_react2.useImperativeHandle)(
210
- ref,
211
- () => controllerRef.current,
212
- []
213
- );
214
- const { dispatch } = useStoreController(store, {
215
- onSubscribe: (state) => {
216
- const controller = controllerRef.current;
217
- if (!controller) {
218
- return;
219
- }
220
- const getResult = () => {
221
- if (getter) {
222
- return getter(state) || "";
223
- }
224
- if (name) {
225
- return state[name] || "";
226
- }
227
- return "";
228
- };
229
- const result = getResult();
230
- if (controller.value !== result) {
231
- controller.value = result;
232
- }
233
- },
234
- onDispatch: (state) => {
235
- const controller = controllerRef.current;
236
- if (!controller) {
237
- return;
238
- }
239
- if (setter) {
240
- setter(state, controller.value);
241
- return;
242
- }
243
- if (name) {
244
- state[name] = controller.value;
245
- }
246
- }
247
- });
248
- const getDefaultValue = () => {
249
- if (getter) {
250
- return getter(store.state);
251
- }
252
- if (name) {
253
- return store.state[name];
254
- }
255
- return void 0;
256
- };
257
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
258
- import_gw_react_text_editor.TextEditor,
259
- {
260
- ...props,
261
- ref: controllerRef,
262
- defaultValue: defaultValue ?? getDefaultValue(),
263
- onChange: (e) => {
264
- dispatch();
265
- props.onChange?.(e);
266
- }
267
- }
268
- );
269
- }
270
-
271
- // src/use_store_component.tsx
272
- var import_jsx_runtime3 = require("react/jsx-runtime");
273
- function useStoreComponent(store) {
274
- const input = (0, import_react3.useCallback)(function Component(props) {
275
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Input, { store, ...props });
276
- }, []);
277
- const select = (0, import_react3.useCallback)(function Component(props) {
278
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Select, { store, ...props });
279
- }, []);
280
- const textarea = (0, import_react3.useCallback)(function Component(props) {
281
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Textarea, { store, ...props });
282
- }, []);
283
- const textEditor = (0, import_react3.useCallback)(function Component(props) {
284
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(TextEditor, { store, ...props });
285
- }, []);
286
- return {
287
- input,
288
- select,
289
- textarea,
290
- textEditor
291
- };
292
- }
293
- function Input({
294
- store,
295
- getter,
296
- setter,
297
- toInputValue,
298
- toStateValue,
299
- ...props
300
- }) {
301
- const ref = (0, import_react3.useRef)(null);
302
- const storeProps = useStoreInputWithName(ref, store, {
303
- ...props,
304
- getter,
305
- setter,
306
- toInputValue,
307
- toStateValue
308
- });
309
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { ref, ...props, ...storeProps });
310
- }
311
- function Select({
312
- store,
313
- getter,
314
- setter,
315
- toInputValue,
316
- toStateValue,
317
- ...props
318
- }) {
319
- const ref = (0, import_react3.useRef)(null);
320
- const storeProps = useStoreInputWithName(ref, store, {
321
- ...props,
322
- getter,
323
- setter,
324
- toInputValue,
325
- toStateValue
326
- });
327
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("select", { ref, ...props, ...storeProps });
328
- }
329
- function Textarea({
330
- store,
331
- getter,
332
- setter,
333
- toInputValue,
334
- toStateValue,
335
- ...props
336
- }) {
337
- const ref = (0, import_react3.useRef)(null);
338
- const storeProps = useStoreInputWithName(ref, store, {
339
- ...props,
340
- getter,
341
- setter,
342
- toInputValue,
343
- toStateValue
344
- });
345
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("textarea", { ref, ...props, ...storeProps });
346
- }
347
-
348
- // src/use_form_store.tsx
349
- function useFormStore(initialState) {
350
- const store = (0, import_gw_store2.useStore)(initialState);
351
- const storeComponent = useStoreComponent(store);
352
- return {
353
- get state() {
354
- return store.state;
355
- },
356
- dispatch: store.dispatch,
357
- subscribe: store.subscribe,
358
- render: createRenderWithStore(store),
359
- ...storeComponent
360
- };
361
- }
362
- // Annotate the CommonJS export names for ESM import in node:
363
- 0 && (module.exports = {
364
- useFormStore
365
- });
@@ -1,340 +0,0 @@
1
- // src/create_render.tsx
2
- import { useSelector } from "gw-store";
3
- import { Fragment, jsx } from "react/jsx-runtime";
4
- function createRenderWithStore(store) {
5
- return function createRender(selector, compare) {
6
- function Component() {
7
- const result = useSelector(store, (state) => state, compare);
8
- const content = selector(result);
9
- return /* @__PURE__ */ jsx(Fragment, { children: content });
10
- }
11
- return /* @__PURE__ */ jsx(Component, {});
12
- };
13
- }
14
-
15
- // src/use_form_store.tsx
16
- import { useStore } from "gw-store";
17
-
18
- // src/use_store_component.tsx
19
- import { useCallback, useRef as useRef2 } from "react";
20
-
21
- // src/use_store_input.tsx
22
- import { format } from "date-fns";
23
-
24
- // src/use_store_controller.tsx
25
- import { useEffect, useId } from "react";
26
- function useStoreController(store, props) {
27
- const dispatchKey = useId();
28
- useEffect(() => {
29
- const unsub = store.subscribe((state, key) => {
30
- if (key === dispatchKey) {
31
- return;
32
- }
33
- props.onSubscribe(state);
34
- });
35
- return () => {
36
- unsub();
37
- };
38
- }, []);
39
- const dispatch = () => {
40
- store.dispatch(
41
- (state) => {
42
- props.onDispatch(state);
43
- },
44
- {
45
- key: dispatchKey
46
- }
47
- );
48
- };
49
- return {
50
- dispatch
51
- };
52
- }
53
-
54
- // src/use_store_input.tsx
55
- function useStoreInput(ref, store, props) {
56
- const toInputValue = props.toInputValue || ((value) => {
57
- if (value === void 0 || value === null) {
58
- return "";
59
- }
60
- if (props.type === "datetime-local") {
61
- const date = new Date(value);
62
- if (!isNaN(date.getTime())) {
63
- return format(date, "yyyy-MM-dd'T'HH:mm:ss");
64
- }
65
- }
66
- return String(value);
67
- });
68
- const getDefaultValue = () => {
69
- if (props.defaultValue !== void 0) {
70
- return props.defaultValue;
71
- }
72
- if (props.type === "checkbox" || props.type === "radio") {
73
- return void 0;
74
- }
75
- return toInputValue(props.getter(store.state));
76
- };
77
- const toInputChecked = (value) => {
78
- if (props.type === "radio") {
79
- return value !== void 0 && value === props.value;
80
- }
81
- return Boolean(value);
82
- };
83
- const getDefaultChecked = () => {
84
- if (props.defaultChecked) {
85
- return props.defaultChecked;
86
- }
87
- if (props.type !== "checkbox" && props.type !== "radio") {
88
- return void 0;
89
- }
90
- return toInputChecked(props.getter(store.state));
91
- };
92
- const toStateValue = props.toStateValue || ((value) => {
93
- if (props.type === "number" || props.type === "range") {
94
- return Number(value);
95
- }
96
- if (props.type === "datetime-local") {
97
- return new Date(value);
98
- }
99
- return value;
100
- });
101
- const inputProps = useStoreController(store, {
102
- onSubscribe: (state) => {
103
- const element = ref.current;
104
- if (!element) {
105
- return;
106
- }
107
- if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
108
- const checked = toInputChecked(props.getter(state));
109
- if (element.checked === checked) {
110
- return;
111
- }
112
- element.checked = checked;
113
- } else {
114
- const value = toInputValue(props.getter(state));
115
- if (element.value === value) {
116
- return;
117
- }
118
- element.value = value;
119
- }
120
- const event = new Event("input", { bubbles: true });
121
- element.dispatchEvent(event);
122
- },
123
- onDispatch: (state) => {
124
- const element = ref.current;
125
- if (!element) {
126
- return;
127
- }
128
- if ("checked" in element && props.type === "checkbox") {
129
- props.setter(state, element.checked);
130
- } else {
131
- props.setter(state, toStateValue(element.value));
132
- }
133
- }
134
- });
135
- return {
136
- defaultValue: getDefaultValue(),
137
- defaultChecked: getDefaultChecked(),
138
- onChange: (event) => {
139
- inputProps.dispatch();
140
- props.onChange?.(event);
141
- }
142
- };
143
- }
144
-
145
- // src/use_store_input_with_name.tsx
146
- function useStoreInputWithName(ref, store, props) {
147
- const inputProps = useStoreInput(ref, store, {
148
- ...props,
149
- getter: (state) => {
150
- if (props.getter) {
151
- return props.getter(state);
152
- }
153
- return state[props.name];
154
- },
155
- setter: (state, value) => {
156
- if (props.setter) {
157
- props.setter(state, value);
158
- return;
159
- }
160
- state[props.name] = value;
161
- }
162
- });
163
- return {
164
- ...inputProps,
165
- name: "name" in props ? String(props.name) : void 0
166
- };
167
- }
168
-
169
- // src/text_editor.tsx
170
- import {
171
- TextEditor as GwTextEditor
172
- } from "gw-react-text-editor";
173
- import { useImperativeHandle, useRef } from "react";
174
- import { jsx as jsx2 } from "react/jsx-runtime";
175
- function TextEditor({
176
- store,
177
- name,
178
- getter,
179
- setter,
180
- defaultValue,
181
- ref,
182
- ...props
183
- }) {
184
- const controllerRef = useRef(null);
185
- useImperativeHandle(
186
- ref,
187
- () => controllerRef.current,
188
- []
189
- );
190
- const { dispatch } = useStoreController(store, {
191
- onSubscribe: (state) => {
192
- const controller = controllerRef.current;
193
- if (!controller) {
194
- return;
195
- }
196
- const getResult = () => {
197
- if (getter) {
198
- return getter(state) || "";
199
- }
200
- if (name) {
201
- return state[name] || "";
202
- }
203
- return "";
204
- };
205
- const result = getResult();
206
- if (controller.value !== result) {
207
- controller.value = result;
208
- }
209
- },
210
- onDispatch: (state) => {
211
- const controller = controllerRef.current;
212
- if (!controller) {
213
- return;
214
- }
215
- if (setter) {
216
- setter(state, controller.value);
217
- return;
218
- }
219
- if (name) {
220
- state[name] = controller.value;
221
- }
222
- }
223
- });
224
- const getDefaultValue = () => {
225
- if (getter) {
226
- return getter(store.state);
227
- }
228
- if (name) {
229
- return store.state[name];
230
- }
231
- return void 0;
232
- };
233
- return /* @__PURE__ */ jsx2(
234
- GwTextEditor,
235
- {
236
- ...props,
237
- ref: controllerRef,
238
- defaultValue: defaultValue ?? getDefaultValue(),
239
- onChange: (e) => {
240
- dispatch();
241
- props.onChange?.(e);
242
- }
243
- }
244
- );
245
- }
246
-
247
- // src/use_store_component.tsx
248
- import { jsx as jsx3 } from "react/jsx-runtime";
249
- function useStoreComponent(store) {
250
- const input = useCallback(function Component(props) {
251
- return /* @__PURE__ */ jsx3(Input, { store, ...props });
252
- }, []);
253
- const select = useCallback(function Component(props) {
254
- return /* @__PURE__ */ jsx3(Select, { store, ...props });
255
- }, []);
256
- const textarea = useCallback(function Component(props) {
257
- return /* @__PURE__ */ jsx3(Textarea, { store, ...props });
258
- }, []);
259
- const textEditor = useCallback(function Component(props) {
260
- return /* @__PURE__ */ jsx3(TextEditor, { store, ...props });
261
- }, []);
262
- return {
263
- input,
264
- select,
265
- textarea,
266
- textEditor
267
- };
268
- }
269
- function Input({
270
- store,
271
- getter,
272
- setter,
273
- toInputValue,
274
- toStateValue,
275
- ...props
276
- }) {
277
- const ref = useRef2(null);
278
- const storeProps = useStoreInputWithName(ref, store, {
279
- ...props,
280
- getter,
281
- setter,
282
- toInputValue,
283
- toStateValue
284
- });
285
- return /* @__PURE__ */ jsx3("input", { ref, ...props, ...storeProps });
286
- }
287
- function Select({
288
- store,
289
- getter,
290
- setter,
291
- toInputValue,
292
- toStateValue,
293
- ...props
294
- }) {
295
- const ref = useRef2(null);
296
- const storeProps = useStoreInputWithName(ref, store, {
297
- ...props,
298
- getter,
299
- setter,
300
- toInputValue,
301
- toStateValue
302
- });
303
- return /* @__PURE__ */ jsx3("select", { ref, ...props, ...storeProps });
304
- }
305
- function Textarea({
306
- store,
307
- getter,
308
- setter,
309
- toInputValue,
310
- toStateValue,
311
- ...props
312
- }) {
313
- const ref = useRef2(null);
314
- const storeProps = useStoreInputWithName(ref, store, {
315
- ...props,
316
- getter,
317
- setter,
318
- toInputValue,
319
- toStateValue
320
- });
321
- return /* @__PURE__ */ jsx3("textarea", { ref, ...props, ...storeProps });
322
- }
323
-
324
- // src/use_form_store.tsx
325
- function useFormStore(initialState) {
326
- const store = useStore(initialState);
327
- const storeComponent = useStoreComponent(store);
328
- return {
329
- get state() {
330
- return store.state;
331
- },
332
- dispatch: store.dispatch,
333
- subscribe: store.subscribe,
334
- render: createRenderWithStore(store),
335
- ...storeComponent
336
- };
337
- }
338
- export {
339
- useFormStore
340
- };