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.
- package/dist/create_render.d.mts +1 -1
- package/dist/create_render.d.ts +1 -1
- package/dist/create_render.js +3 -27
- package/dist/create_render.mjs +1 -23
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +119 -107
- package/dist/index.mjs +113 -99
- package/dist/text_editor.d.mts +13 -0
- package/dist/text_editor.d.ts +13 -0
- package/dist/text_editor.js +135 -0
- package/dist/text_editor.mjs +112 -0
- package/dist/use_form_store.d.mts +3 -1
- package/dist/use_form_store.d.ts +3 -1
- package/dist/use_form_store.js +111 -102
- package/dist/use_form_store.mjs +108 -97
- package/dist/use_store_component.d.mts +4 -1
- package/dist/use_store_component.d.ts +4 -1
- package/dist/use_store_component.js +97 -25
- package/dist/use_store_component.mjs +104 -30
- package/dist/use_store_controller.d.mts +1 -1
- package/dist/use_store_controller.d.ts +1 -1
- package/dist/use_store_controller.js +9 -17
- package/dist/use_store_controller.mjs +10 -18
- package/dist/use_store_input.d.mts +1 -1
- package/dist/use_store_input.d.ts +1 -1
- package/dist/use_store_input.js +9 -17
- package/dist/use_store_input.mjs +10 -18
- package/dist/use_store_input_with_name.d.mts +1 -1
- package/dist/use_store_input_with_name.d.ts +1 -1
- package/dist/use_store_input_with_name.js +9 -17
- package/dist/use_store_input_with_name.mjs +10 -18
- package/package.json +6 -4
- package/dist/use_selector.d.mts +0 -5
- package/dist/use_selector.d.ts +0 -5
- package/dist/use_selector.js +0 -50
- package/dist/use_selector.mjs +0 -25
- package/dist/use_store.d.mts +0 -12
- package/dist/use_store.d.ts +0 -12
- package/dist/use_store.js +0 -71
- package/dist/use_store.mjs +0 -46
- package/dist/use_subscribe.d.mts +0 -5
- package/dist/use_subscribe.d.ts +0 -5
- package/dist/use_subscribe.js +0 -38
- package/dist/use_subscribe.mjs +0 -13
package/dist/use_form_store.js
CHANGED
|
@@ -24,35 +24,13 @@ __export(use_form_store_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(use_form_store_exports);
|
|
26
26
|
|
|
27
|
-
// src/use_selector.tsx
|
|
28
|
-
var import_react = require("react");
|
|
29
|
-
function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
30
|
-
const [state, setState] = (0, import_react.useState)(selector(store.state));
|
|
31
|
-
const stateRef = (0, import_react.useRef)(state);
|
|
32
|
-
(0, import_react.useEffect)(() => {
|
|
33
|
-
stateRef.current = state;
|
|
34
|
-
}, [state]);
|
|
35
|
-
(0, import_react.useEffect)(() => {
|
|
36
|
-
const unsubscribe = store.subscribe((newState) => {
|
|
37
|
-
const result = selector(newState);
|
|
38
|
-
if (compare(stateRef.current, result)) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
setState(result);
|
|
42
|
-
});
|
|
43
|
-
return () => {
|
|
44
|
-
unsubscribe();
|
|
45
|
-
};
|
|
46
|
-
}, []);
|
|
47
|
-
return state;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
27
|
// src/create_render.tsx
|
|
28
|
+
var import_gw_store = require("gw-store");
|
|
51
29
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
52
30
|
function createRenderWithStore(store) {
|
|
53
31
|
return function createRender(selector, compare) {
|
|
54
32
|
function Component() {
|
|
55
|
-
const result = useSelector(store, (state) => state, compare);
|
|
33
|
+
const result = (0, import_gw_store.useSelector)(store, (state) => state, compare);
|
|
56
34
|
const content = selector(result);
|
|
57
35
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: content });
|
|
58
36
|
}
|
|
@@ -60,79 +38,30 @@ function createRenderWithStore(store) {
|
|
|
60
38
|
};
|
|
61
39
|
}
|
|
62
40
|
|
|
63
|
-
// src/
|
|
64
|
-
var
|
|
65
|
-
var import_immer = require("immer");
|
|
66
|
-
function useStore(initialState) {
|
|
67
|
-
const stateRef = (0, import_react2.useRef)(initialState);
|
|
68
|
-
const subscribers = (0, import_react2.useRef)([]);
|
|
69
|
-
const dispatch = (recipe, options = {}) => {
|
|
70
|
-
if (typeof recipe === "function") {
|
|
71
|
-
stateRef.current = (0, import_immer.produce)(stateRef.current, recipe);
|
|
72
|
-
} else {
|
|
73
|
-
stateRef.current = (0, import_immer.produce)(
|
|
74
|
-
stateRef.current,
|
|
75
|
-
(draft) => {
|
|
76
|
-
for (const key in recipe) {
|
|
77
|
-
draft[key] = recipe[key];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
notify(options.key);
|
|
83
|
-
};
|
|
84
|
-
const subscribe = (subscriber) => {
|
|
85
|
-
subscribers.current.push(subscriber);
|
|
86
|
-
return () => {
|
|
87
|
-
const index = subscribers.current.indexOf(subscriber);
|
|
88
|
-
if (index !== -1) {
|
|
89
|
-
subscribers.current.splice(index, 1);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
const notify = (key) => {
|
|
94
|
-
for (const listener of subscribers.current) {
|
|
95
|
-
listener(stateRef.current, key);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
return {
|
|
99
|
-
get state() {
|
|
100
|
-
return stateRef.current;
|
|
101
|
-
},
|
|
102
|
-
dispatch,
|
|
103
|
-
subscribe
|
|
104
|
-
};
|
|
105
|
-
}
|
|
41
|
+
// src/use_form_store.tsx
|
|
42
|
+
var import_gw_store2 = require("gw-store");
|
|
106
43
|
|
|
107
44
|
// src/use_store_component.tsx
|
|
108
|
-
var
|
|
45
|
+
var import_react3 = require("react");
|
|
109
46
|
|
|
110
47
|
// src/use_store_input.tsx
|
|
111
48
|
var import_date_fns = require("date-fns");
|
|
112
49
|
|
|
113
50
|
// src/use_store_controller.tsx
|
|
114
|
-
var
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
+
});
|
|
121
61
|
return () => {
|
|
122
|
-
|
|
62
|
+
unsub();
|
|
123
63
|
};
|
|
124
64
|
}, []);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// src/use_store_controller.tsx
|
|
128
|
-
function useStoreController(store, props) {
|
|
129
|
-
const dispatchKey = (0, import_react4.useId)();
|
|
130
|
-
useSubscribe(store, (state, key) => {
|
|
131
|
-
if (key === dispatchKey) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
props.onSubscribe(state);
|
|
135
|
-
});
|
|
136
65
|
const dispatch = () => {
|
|
137
66
|
store.dispatch(
|
|
138
67
|
(state) => {
|
|
@@ -263,22 +192,102 @@ function useStoreInputWithName(ref, store, props) {
|
|
|
263
192
|
};
|
|
264
193
|
}
|
|
265
194
|
|
|
266
|
-
// src/
|
|
195
|
+
// src/text_editor.tsx
|
|
196
|
+
var import_gw_react_text_editor = require("gw-react-text-editor");
|
|
197
|
+
var import_react2 = require("react");
|
|
267
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");
|
|
268
273
|
function useStoreComponent(store) {
|
|
269
|
-
const input = (0,
|
|
270
|
-
return /* @__PURE__ */ (0,
|
|
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 });
|
|
271
279
|
}, []);
|
|
272
|
-
const
|
|
273
|
-
return /* @__PURE__ */ (0,
|
|
280
|
+
const textarea = (0, import_react3.useCallback)(function Component(props) {
|
|
281
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Textarea, { store, ...props });
|
|
274
282
|
}, []);
|
|
275
|
-
const
|
|
276
|
-
return /* @__PURE__ */ (0,
|
|
283
|
+
const textEditor = (0, import_react3.useCallback)(function Component(props) {
|
|
284
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(TextEditor, { store, ...props });
|
|
277
285
|
}, []);
|
|
278
286
|
return {
|
|
279
287
|
input,
|
|
280
288
|
select,
|
|
281
|
-
textarea
|
|
289
|
+
textarea,
|
|
290
|
+
textEditor
|
|
282
291
|
};
|
|
283
292
|
}
|
|
284
293
|
function Input({
|
|
@@ -289,7 +298,7 @@ function Input({
|
|
|
289
298
|
toStateValue,
|
|
290
299
|
...props
|
|
291
300
|
}) {
|
|
292
|
-
const ref = (0,
|
|
301
|
+
const ref = (0, import_react3.useRef)(null);
|
|
293
302
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
294
303
|
...props,
|
|
295
304
|
getter,
|
|
@@ -297,7 +306,7 @@ function Input({
|
|
|
297
306
|
toInputValue,
|
|
298
307
|
toStateValue
|
|
299
308
|
});
|
|
300
|
-
return /* @__PURE__ */ (0,
|
|
309
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { ref, ...props, ...storeProps });
|
|
301
310
|
}
|
|
302
311
|
function Select({
|
|
303
312
|
store,
|
|
@@ -307,7 +316,7 @@ function Select({
|
|
|
307
316
|
toStateValue,
|
|
308
317
|
...props
|
|
309
318
|
}) {
|
|
310
|
-
const ref = (0,
|
|
319
|
+
const ref = (0, import_react3.useRef)(null);
|
|
311
320
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
312
321
|
...props,
|
|
313
322
|
getter,
|
|
@@ -315,7 +324,7 @@ function Select({
|
|
|
315
324
|
toInputValue,
|
|
316
325
|
toStateValue
|
|
317
326
|
});
|
|
318
|
-
return /* @__PURE__ */ (0,
|
|
327
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("select", { ref, ...props, ...storeProps });
|
|
319
328
|
}
|
|
320
329
|
function Textarea({
|
|
321
330
|
store,
|
|
@@ -325,7 +334,7 @@ function Textarea({
|
|
|
325
334
|
toStateValue,
|
|
326
335
|
...props
|
|
327
336
|
}) {
|
|
328
|
-
const ref = (0,
|
|
337
|
+
const ref = (0, import_react3.useRef)(null);
|
|
329
338
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
330
339
|
...props,
|
|
331
340
|
getter,
|
|
@@ -333,12 +342,12 @@ function Textarea({
|
|
|
333
342
|
toInputValue,
|
|
334
343
|
toStateValue
|
|
335
344
|
});
|
|
336
|
-
return /* @__PURE__ */ (0,
|
|
345
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("textarea", { ref, ...props, ...storeProps });
|
|
337
346
|
}
|
|
338
347
|
|
|
339
348
|
// src/use_form_store.tsx
|
|
340
349
|
function useFormStore(initialState) {
|
|
341
|
-
const store = useStore(initialState);
|
|
350
|
+
const store = (0, import_gw_store2.useStore)(initialState);
|
|
342
351
|
const storeComponent = useStoreComponent(store);
|
|
343
352
|
return {
|
|
344
353
|
get state() {
|
package/dist/use_form_store.mjs
CHANGED
|
@@ -1,27 +1,5 @@
|
|
|
1
|
-
// src/use_selector.tsx
|
|
2
|
-
import { useEffect, useRef, useState } from "react";
|
|
3
|
-
function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
4
|
-
const [state, setState] = useState(selector(store.state));
|
|
5
|
-
const stateRef = useRef(state);
|
|
6
|
-
useEffect(() => {
|
|
7
|
-
stateRef.current = state;
|
|
8
|
-
}, [state]);
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
const unsubscribe = store.subscribe((newState) => {
|
|
11
|
-
const result = selector(newState);
|
|
12
|
-
if (compare(stateRef.current, result)) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
setState(result);
|
|
16
|
-
});
|
|
17
|
-
return () => {
|
|
18
|
-
unsubscribe();
|
|
19
|
-
};
|
|
20
|
-
}, []);
|
|
21
|
-
return state;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
1
|
// src/create_render.tsx
|
|
2
|
+
import { useSelector } from "gw-store";
|
|
25
3
|
import { Fragment, jsx } from "react/jsx-runtime";
|
|
26
4
|
function createRenderWithStore(store) {
|
|
27
5
|
return function createRender(selector, compare) {
|
|
@@ -34,79 +12,30 @@ function createRenderWithStore(store) {
|
|
|
34
12
|
};
|
|
35
13
|
}
|
|
36
14
|
|
|
37
|
-
// src/
|
|
38
|
-
import {
|
|
39
|
-
import { produce } from "immer";
|
|
40
|
-
function useStore(initialState) {
|
|
41
|
-
const stateRef = useRef2(initialState);
|
|
42
|
-
const subscribers = useRef2([]);
|
|
43
|
-
const dispatch = (recipe, options = {}) => {
|
|
44
|
-
if (typeof recipe === "function") {
|
|
45
|
-
stateRef.current = produce(stateRef.current, recipe);
|
|
46
|
-
} else {
|
|
47
|
-
stateRef.current = 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);
|
|
64
|
-
}
|
|
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
|
-
}
|
|
15
|
+
// src/use_form_store.tsx
|
|
16
|
+
import { useStore } from "gw-store";
|
|
80
17
|
|
|
81
18
|
// src/use_store_component.tsx
|
|
82
|
-
import { useCallback, useRef as
|
|
19
|
+
import { useCallback, useRef as useRef2 } from "react";
|
|
83
20
|
|
|
84
21
|
// src/use_store_input.tsx
|
|
85
22
|
import { format } from "date-fns";
|
|
86
23
|
|
|
87
24
|
// src/use_store_controller.tsx
|
|
88
|
-
import { useId } from "react";
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
});
|
|
95
35
|
return () => {
|
|
96
|
-
|
|
36
|
+
unsub();
|
|
97
37
|
};
|
|
98
38
|
}, []);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/use_store_controller.tsx
|
|
102
|
-
function useStoreController(store, props) {
|
|
103
|
-
const dispatchKey = useId();
|
|
104
|
-
useSubscribe(store, (state, key) => {
|
|
105
|
-
if (key === dispatchKey) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
props.onSubscribe(state);
|
|
109
|
-
});
|
|
110
39
|
const dispatch = () => {
|
|
111
40
|
store.dispatch(
|
|
112
41
|
(state) => {
|
|
@@ -237,22 +166,104 @@ function useStoreInputWithName(ref, store, props) {
|
|
|
237
166
|
};
|
|
238
167
|
}
|
|
239
168
|
|
|
240
|
-
// src/
|
|
169
|
+
// src/text_editor.tsx
|
|
170
|
+
import {
|
|
171
|
+
TextEditor as GwTextEditor
|
|
172
|
+
} from "gw-react-text-editor";
|
|
173
|
+
import { useImperativeHandle, useRef } from "react";
|
|
241
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";
|
|
242
249
|
function useStoreComponent(store) {
|
|
243
250
|
const input = useCallback(function Component(props) {
|
|
244
|
-
return /* @__PURE__ */
|
|
251
|
+
return /* @__PURE__ */ jsx3(Input, { store, ...props });
|
|
245
252
|
}, []);
|
|
246
253
|
const select = useCallback(function Component(props) {
|
|
247
|
-
return /* @__PURE__ */
|
|
254
|
+
return /* @__PURE__ */ jsx3(Select, { store, ...props });
|
|
248
255
|
}, []);
|
|
249
256
|
const textarea = useCallback(function Component(props) {
|
|
250
|
-
return /* @__PURE__ */
|
|
257
|
+
return /* @__PURE__ */ jsx3(Textarea, { store, ...props });
|
|
258
|
+
}, []);
|
|
259
|
+
const textEditor = useCallback(function Component(props) {
|
|
260
|
+
return /* @__PURE__ */ jsx3(TextEditor, { store, ...props });
|
|
251
261
|
}, []);
|
|
252
262
|
return {
|
|
253
263
|
input,
|
|
254
264
|
select,
|
|
255
|
-
textarea
|
|
265
|
+
textarea,
|
|
266
|
+
textEditor
|
|
256
267
|
};
|
|
257
268
|
}
|
|
258
269
|
function Input({
|
|
@@ -263,7 +274,7 @@ function Input({
|
|
|
263
274
|
toStateValue,
|
|
264
275
|
...props
|
|
265
276
|
}) {
|
|
266
|
-
const ref =
|
|
277
|
+
const ref = useRef2(null);
|
|
267
278
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
268
279
|
...props,
|
|
269
280
|
getter,
|
|
@@ -271,7 +282,7 @@ function Input({
|
|
|
271
282
|
toInputValue,
|
|
272
283
|
toStateValue
|
|
273
284
|
});
|
|
274
|
-
return /* @__PURE__ */
|
|
285
|
+
return /* @__PURE__ */ jsx3("input", { ref, ...props, ...storeProps });
|
|
275
286
|
}
|
|
276
287
|
function Select({
|
|
277
288
|
store,
|
|
@@ -281,7 +292,7 @@ function Select({
|
|
|
281
292
|
toStateValue,
|
|
282
293
|
...props
|
|
283
294
|
}) {
|
|
284
|
-
const ref =
|
|
295
|
+
const ref = useRef2(null);
|
|
285
296
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
286
297
|
...props,
|
|
287
298
|
getter,
|
|
@@ -289,7 +300,7 @@ function Select({
|
|
|
289
300
|
toInputValue,
|
|
290
301
|
toStateValue
|
|
291
302
|
});
|
|
292
|
-
return /* @__PURE__ */
|
|
303
|
+
return /* @__PURE__ */ jsx3("select", { ref, ...props, ...storeProps });
|
|
293
304
|
}
|
|
294
305
|
function Textarea({
|
|
295
306
|
store,
|
|
@@ -299,7 +310,7 @@ function Textarea({
|
|
|
299
310
|
toStateValue,
|
|
300
311
|
...props
|
|
301
312
|
}) {
|
|
302
|
-
const ref =
|
|
313
|
+
const ref = useRef2(null);
|
|
303
314
|
const storeProps = useStoreInputWithName(ref, store, {
|
|
304
315
|
...props,
|
|
305
316
|
getter,
|
|
@@ -307,7 +318,7 @@ function Textarea({
|
|
|
307
318
|
toInputValue,
|
|
308
319
|
toStateValue
|
|
309
320
|
});
|
|
310
|
-
return /* @__PURE__ */
|
|
321
|
+
return /* @__PURE__ */ jsx3("textarea", { ref, ...props, ...storeProps });
|
|
311
322
|
}
|
|
312
323
|
|
|
313
324
|
// src/use_form_store.tsx
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { DetailedHTMLProps } from 'react';
|
|
3
|
-
import { Store } from '
|
|
3
|
+
import { Store } from 'gw-store';
|
|
4
4
|
import { StoreInputWithNameProps } from './use_store_input_with_name.mjs';
|
|
5
|
+
import { TextEditorProps } from './text_editor.mjs';
|
|
5
6
|
import './use_store_input.mjs';
|
|
7
|
+
import 'gw-react-text-editor';
|
|
6
8
|
|
|
7
9
|
type StoreInputWithNameComponentProps<TElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & Omit<DetailedHTMLProps<React.InputHTMLAttributes<TElement>, TElement>, keyof StoreInputWithNameProps<TElement, TState, TName, TValue>>;
|
|
8
10
|
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
9
11
|
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLInputElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
10
12
|
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLSelectElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
11
13
|
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLTextAreaElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
textEditor: (props: Omit<TextEditorProps<TState>, "store">) => react_jsx_runtime.JSX.Element;
|
|
12
15
|
};
|
|
13
16
|
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameComponentProps<TElement, TState, TName, TValue> & {
|
|
14
17
|
store: Store<TState>;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { DetailedHTMLProps } from 'react';
|
|
3
|
-
import { Store } from '
|
|
3
|
+
import { Store } from 'gw-store';
|
|
4
4
|
import { StoreInputWithNameProps } from './use_store_input_with_name.js';
|
|
5
|
+
import { TextEditorProps } from './text_editor.js';
|
|
5
6
|
import './use_store_input.js';
|
|
7
|
+
import 'gw-react-text-editor';
|
|
6
8
|
|
|
7
9
|
type StoreInputWithNameComponentProps<TElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & Omit<DetailedHTMLProps<React.InputHTMLAttributes<TElement>, TElement>, keyof StoreInputWithNameProps<TElement, TState, TName, TValue>>;
|
|
8
10
|
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
9
11
|
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLInputElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
10
12
|
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLSelectElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
11
13
|
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLTextAreaElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
14
|
+
textEditor: (props: Omit<TextEditorProps<TState>, "store">) => react_jsx_runtime.JSX.Element;
|
|
12
15
|
};
|
|
13
16
|
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameComponentProps<TElement, TState, TName, TValue> & {
|
|
14
17
|
store: Store<TState>;
|