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