react-store-input 0.1.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.
- package/LICENSE +7 -0
- package/README.md +39 -0
- package/dist/create_render.d.mts +7 -0
- package/dist/create_render.d.ts +7 -0
- package/dist/create_render.js +79 -0
- package/dist/create_render.mjs +42 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +401 -0
- package/dist/index.mjs +360 -0
- package/dist/serialize.d.mts +4 -0
- package/dist/serialize.d.ts +4 -0
- package/dist/serialize.js +116 -0
- package/dist/serialize.mjs +90 -0
- package/dist/use_form_store.d.mts +11 -0
- package/dist/use_form_store.d.ts +11 -0
- package/dist/use_form_store.js +294 -0
- package/dist/use_form_store.mjs +263 -0
- package/dist/use_selector.d.mts +5 -0
- package/dist/use_selector.d.ts +5 -0
- package/dist/use_selector.js +54 -0
- package/dist/use_selector.mjs +29 -0
- package/dist/use_store.d.mts +12 -0
- package/dist/use_store.d.ts +12 -0
- package/dist/use_store.js +71 -0
- package/dist/use_store.mjs +46 -0
- package/dist/use_store_input.d.mts +19 -0
- package/dist/use_store_input.d.ts +19 -0
- package/dist/use_store_input.js +199 -0
- package/dist/use_store_input.mjs +167 -0
- package/dist/use_store_input_props.d.mts +23 -0
- package/dist/use_store_input_props.d.ts +23 -0
- package/dist/use_store_input_props.js +143 -0
- package/dist/use_store_input_props.mjs +122 -0
- package/dist/use_subscribe.d.mts +5 -0
- package/dist/use_subscribe.d.ts +5 -0
- package/dist/use_subscribe.js +38 -0
- package/dist/use_subscribe.mjs +13 -0
- package/package.json +45 -0
- package/tsup.config.ts +9 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/use_form_store.tsx
|
|
31
|
+
var use_form_store_exports = {};
|
|
32
|
+
__export(use_form_store_exports, {
|
|
33
|
+
useFormStore: () => useFormStore
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(use_form_store_exports);
|
|
36
|
+
|
|
37
|
+
// src/use_selector.tsx
|
|
38
|
+
var import_react2 = require("react");
|
|
39
|
+
|
|
40
|
+
// src/use_subscribe.tsx
|
|
41
|
+
var import_react = require("react");
|
|
42
|
+
function useSubscribe(store, subscriber) {
|
|
43
|
+
(0, import_react.useEffect)(() => {
|
|
44
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
45
|
+
return () => {
|
|
46
|
+
unsubscribe();
|
|
47
|
+
};
|
|
48
|
+
}, [store]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/use_selector.tsx
|
|
52
|
+
function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
53
|
+
const [state, setState] = (0, import_react2.useState)(selector(store.state));
|
|
54
|
+
useSubscribe(store, (newState) => {
|
|
55
|
+
const result = selector(newState);
|
|
56
|
+
if (compare(state, result)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
setState(result);
|
|
60
|
+
});
|
|
61
|
+
return state;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/create_render.tsx
|
|
65
|
+
var import_react3 = __toESM(require("react"));
|
|
66
|
+
function createRender(store) {
|
|
67
|
+
return function createRender2(selector, compare) {
|
|
68
|
+
function Component() {
|
|
69
|
+
const result = useSelector(store, (state) => state, compare);
|
|
70
|
+
const content = selector(result);
|
|
71
|
+
return /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Fragment, null, content);
|
|
72
|
+
}
|
|
73
|
+
return /* @__PURE__ */ import_react3.default.createElement(Component, null);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/use_store.tsx
|
|
78
|
+
var import_react4 = require("react");
|
|
79
|
+
var import_immer = require("immer");
|
|
80
|
+
function useStore(initialState) {
|
|
81
|
+
const stateRef = (0, import_react4.useRef)(initialState);
|
|
82
|
+
const subscribers = (0, import_react4.useRef)([]);
|
|
83
|
+
const dispatch = (recipe, options = {}) => {
|
|
84
|
+
if (typeof recipe === "function") {
|
|
85
|
+
stateRef.current = (0, import_immer.produce)(stateRef.current, recipe);
|
|
86
|
+
} else {
|
|
87
|
+
stateRef.current = (0, import_immer.produce)(
|
|
88
|
+
stateRef.current,
|
|
89
|
+
(draft) => {
|
|
90
|
+
for (const key in recipe) {
|
|
91
|
+
draft[key] = recipe[key];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
notify(options.key);
|
|
97
|
+
};
|
|
98
|
+
const subscribe = (subscriber) => {
|
|
99
|
+
subscribers.current.push(subscriber);
|
|
100
|
+
return () => {
|
|
101
|
+
const index = subscribers.current.indexOf(subscriber);
|
|
102
|
+
if (index !== -1) {
|
|
103
|
+
subscribers.current.splice(index, 1);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
const notify = (key) => {
|
|
108
|
+
for (const listener of subscribers.current) {
|
|
109
|
+
listener(stateRef.current, key);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
get state() {
|
|
114
|
+
return stateRef.current;
|
|
115
|
+
},
|
|
116
|
+
dispatch,
|
|
117
|
+
subscribe
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/use_store_input.tsx
|
|
122
|
+
var import_react6 = require("react");
|
|
123
|
+
var import_react7 = __toESM(require("react"));
|
|
124
|
+
|
|
125
|
+
// src/use_store_input_props.tsx
|
|
126
|
+
var import_react5 = require("react");
|
|
127
|
+
var import_date_fns = require("date-fns");
|
|
128
|
+
function useStoreInputProps(store, props) {
|
|
129
|
+
const toInputValue = (value) => {
|
|
130
|
+
if (value === void 0 || value === null) {
|
|
131
|
+
return "";
|
|
132
|
+
}
|
|
133
|
+
if (props.type === "datetime-local") {
|
|
134
|
+
if (value instanceof Date) {
|
|
135
|
+
return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
136
|
+
}
|
|
137
|
+
if (typeof value === "string") {
|
|
138
|
+
return toInputValue(new Date(value));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return String(value);
|
|
142
|
+
};
|
|
143
|
+
const getDefaultValue = () => {
|
|
144
|
+
if (props.defaultValue !== void 0) {
|
|
145
|
+
return props.defaultValue;
|
|
146
|
+
}
|
|
147
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
148
|
+
return void 0;
|
|
149
|
+
}
|
|
150
|
+
return toInputValue(store.state[props.name]);
|
|
151
|
+
};
|
|
152
|
+
const toInputChecked = (value) => {
|
|
153
|
+
if (props.type === "radio") {
|
|
154
|
+
return value !== void 0 && value === props.value;
|
|
155
|
+
}
|
|
156
|
+
return Boolean(value);
|
|
157
|
+
};
|
|
158
|
+
const getDefaultChecked = () => {
|
|
159
|
+
if (props.defaultChecked) {
|
|
160
|
+
return props.defaultChecked;
|
|
161
|
+
}
|
|
162
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
return toInputChecked(store.state[props.name]);
|
|
166
|
+
};
|
|
167
|
+
function useSubscriptionRef() {
|
|
168
|
+
const ref2 = (0, import_react5.useRef)(null);
|
|
169
|
+
(0, import_react5.useEffect)(() => {
|
|
170
|
+
return store.subscribe((state, key) => {
|
|
171
|
+
if (key === dispatchKey) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const input = ref2.current;
|
|
175
|
+
if (!input) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
179
|
+
const checked = toInputChecked(state[props.name]);
|
|
180
|
+
if (input.checked === checked) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
input.checked = checked;
|
|
184
|
+
} else {
|
|
185
|
+
const value = toInputValue(state[props.name]);
|
|
186
|
+
if (input.value === value) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
input.value = value;
|
|
190
|
+
}
|
|
191
|
+
const event = new Event("input", { bubbles: true });
|
|
192
|
+
input.dispatchEvent(event);
|
|
193
|
+
});
|
|
194
|
+
}, []);
|
|
195
|
+
return ref2;
|
|
196
|
+
}
|
|
197
|
+
function toStateValue(value) {
|
|
198
|
+
if (typeof store.state[props.name] === "number") {
|
|
199
|
+
return Number(value);
|
|
200
|
+
}
|
|
201
|
+
if (store.state[props.name] instanceof Date) {
|
|
202
|
+
return new Date(value);
|
|
203
|
+
}
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
function createChangeEventHandler() {
|
|
207
|
+
return (e) => {
|
|
208
|
+
const target = e.target;
|
|
209
|
+
if (props.type === "checkbox") {
|
|
210
|
+
store.dispatch((state) => {
|
|
211
|
+
state[props.name] = target.checked;
|
|
212
|
+
}, {
|
|
213
|
+
key: dispatchKey
|
|
214
|
+
});
|
|
215
|
+
} else {
|
|
216
|
+
store.dispatch((state) => {
|
|
217
|
+
state[props.name] = toStateValue(target.value);
|
|
218
|
+
}, {
|
|
219
|
+
key: dispatchKey
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
props.onChange?.(e);
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const dispatchKey = (0, import_react5.useId)();
|
|
226
|
+
const ref = useSubscriptionRef();
|
|
227
|
+
const name = String(props.name);
|
|
228
|
+
const defaultValue = getDefaultValue();
|
|
229
|
+
const defaultChecked = getDefaultChecked();
|
|
230
|
+
const onChange = createChangeEventHandler();
|
|
231
|
+
return {
|
|
232
|
+
key: dispatchKey,
|
|
233
|
+
ref,
|
|
234
|
+
name,
|
|
235
|
+
defaultValue,
|
|
236
|
+
defaultChecked,
|
|
237
|
+
onChange
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/use_store_input.tsx
|
|
242
|
+
function useStoreInput(store) {
|
|
243
|
+
const input = (0, import_react6.useCallback)(
|
|
244
|
+
function Component(props) {
|
|
245
|
+
return /* @__PURE__ */ import_react7.default.createElement(Input, { store, ...props });
|
|
246
|
+
},
|
|
247
|
+
[]
|
|
248
|
+
);
|
|
249
|
+
const select = (0, import_react6.useCallback)(
|
|
250
|
+
function Component(props) {
|
|
251
|
+
return /* @__PURE__ */ import_react7.default.createElement(Select, { store, ...props });
|
|
252
|
+
},
|
|
253
|
+
[]
|
|
254
|
+
);
|
|
255
|
+
const textarea = (0, import_react6.useCallback)(function Component(props) {
|
|
256
|
+
return /* @__PURE__ */ import_react7.default.createElement(Textarea, { store, ...props });
|
|
257
|
+
}, []);
|
|
258
|
+
return {
|
|
259
|
+
input,
|
|
260
|
+
select,
|
|
261
|
+
textarea
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function Input({ store, ...props }) {
|
|
265
|
+
const storeProps = useStoreInputProps(store, props);
|
|
266
|
+
return /* @__PURE__ */ import_react7.default.createElement("input", { ...props, ...storeProps });
|
|
267
|
+
}
|
|
268
|
+
function Select({ store, ...props }) {
|
|
269
|
+
const storeProps = useStoreInputProps(store, props);
|
|
270
|
+
return /* @__PURE__ */ import_react7.default.createElement("select", { ...props, ...storeProps });
|
|
271
|
+
}
|
|
272
|
+
function Textarea({ store, ...props }) {
|
|
273
|
+
const storeProps = useStoreInputProps(store, props);
|
|
274
|
+
return /* @__PURE__ */ import_react7.default.createElement("textarea", { ...props, ...storeProps });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/use_form_store.tsx
|
|
278
|
+
function useFormStore(initialState) {
|
|
279
|
+
const store = useStore(initialState);
|
|
280
|
+
const storeInput = useStoreInput(store);
|
|
281
|
+
return {
|
|
282
|
+
get state() {
|
|
283
|
+
return store.state;
|
|
284
|
+
},
|
|
285
|
+
dispatch: store.dispatch,
|
|
286
|
+
subscribe: store.subscribe,
|
|
287
|
+
render: createRender(store),
|
|
288
|
+
...storeInput
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
292
|
+
0 && (module.exports = {
|
|
293
|
+
useFormStore
|
|
294
|
+
});
|
|
@@ -0,0 +1,263 @@
|
|
|
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
|
+
// src/create_render.tsx
|
|
29
|
+
import React from "react";
|
|
30
|
+
function createRender(store) {
|
|
31
|
+
return function createRender2(selector, compare) {
|
|
32
|
+
function Component() {
|
|
33
|
+
const result = useSelector(store, (state) => state, compare);
|
|
34
|
+
const content = selector(result);
|
|
35
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, content);
|
|
36
|
+
}
|
|
37
|
+
return /* @__PURE__ */ React.createElement(Component, null);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/use_store.tsx
|
|
42
|
+
import { useRef } from "react";
|
|
43
|
+
import { produce } from "immer";
|
|
44
|
+
function useStore(initialState) {
|
|
45
|
+
const stateRef = useRef(initialState);
|
|
46
|
+
const subscribers = useRef([]);
|
|
47
|
+
const dispatch = (recipe, options = {}) => {
|
|
48
|
+
if (typeof recipe === "function") {
|
|
49
|
+
stateRef.current = produce(stateRef.current, recipe);
|
|
50
|
+
} else {
|
|
51
|
+
stateRef.current = produce(
|
|
52
|
+
stateRef.current,
|
|
53
|
+
(draft) => {
|
|
54
|
+
for (const key in recipe) {
|
|
55
|
+
draft[key] = recipe[key];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
notify(options.key);
|
|
61
|
+
};
|
|
62
|
+
const subscribe = (subscriber) => {
|
|
63
|
+
subscribers.current.push(subscriber);
|
|
64
|
+
return () => {
|
|
65
|
+
const index = subscribers.current.indexOf(subscriber);
|
|
66
|
+
if (index !== -1) {
|
|
67
|
+
subscribers.current.splice(index, 1);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const notify = (key) => {
|
|
72
|
+
for (const listener of subscribers.current) {
|
|
73
|
+
listener(stateRef.current, key);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
get state() {
|
|
78
|
+
return stateRef.current;
|
|
79
|
+
},
|
|
80
|
+
dispatch,
|
|
81
|
+
subscribe
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/use_store_input.tsx
|
|
86
|
+
import {
|
|
87
|
+
useCallback
|
|
88
|
+
} from "react";
|
|
89
|
+
import React2 from "react";
|
|
90
|
+
|
|
91
|
+
// src/use_store_input_props.tsx
|
|
92
|
+
import {
|
|
93
|
+
useEffect as useEffect2,
|
|
94
|
+
useId,
|
|
95
|
+
useRef as useRef2
|
|
96
|
+
} from "react";
|
|
97
|
+
import { format } from "date-fns";
|
|
98
|
+
function useStoreInputProps(store, props) {
|
|
99
|
+
const toInputValue = (value) => {
|
|
100
|
+
if (value === void 0 || value === null) {
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
if (props.type === "datetime-local") {
|
|
104
|
+
if (value instanceof Date) {
|
|
105
|
+
return format(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
106
|
+
}
|
|
107
|
+
if (typeof value === "string") {
|
|
108
|
+
return toInputValue(new Date(value));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return String(value);
|
|
112
|
+
};
|
|
113
|
+
const getDefaultValue = () => {
|
|
114
|
+
if (props.defaultValue !== void 0) {
|
|
115
|
+
return props.defaultValue;
|
|
116
|
+
}
|
|
117
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
118
|
+
return void 0;
|
|
119
|
+
}
|
|
120
|
+
return toInputValue(store.state[props.name]);
|
|
121
|
+
};
|
|
122
|
+
const toInputChecked = (value) => {
|
|
123
|
+
if (props.type === "radio") {
|
|
124
|
+
return value !== void 0 && value === props.value;
|
|
125
|
+
}
|
|
126
|
+
return Boolean(value);
|
|
127
|
+
};
|
|
128
|
+
const getDefaultChecked = () => {
|
|
129
|
+
if (props.defaultChecked) {
|
|
130
|
+
return props.defaultChecked;
|
|
131
|
+
}
|
|
132
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
return toInputChecked(store.state[props.name]);
|
|
136
|
+
};
|
|
137
|
+
function useSubscriptionRef() {
|
|
138
|
+
const ref2 = useRef2(null);
|
|
139
|
+
useEffect2(() => {
|
|
140
|
+
return store.subscribe((state, key) => {
|
|
141
|
+
if (key === dispatchKey) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const input = ref2.current;
|
|
145
|
+
if (!input) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
149
|
+
const checked = toInputChecked(state[props.name]);
|
|
150
|
+
if (input.checked === checked) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
input.checked = checked;
|
|
154
|
+
} else {
|
|
155
|
+
const value = toInputValue(state[props.name]);
|
|
156
|
+
if (input.value === value) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
input.value = value;
|
|
160
|
+
}
|
|
161
|
+
const event = new Event("input", { bubbles: true });
|
|
162
|
+
input.dispatchEvent(event);
|
|
163
|
+
});
|
|
164
|
+
}, []);
|
|
165
|
+
return ref2;
|
|
166
|
+
}
|
|
167
|
+
function toStateValue(value) {
|
|
168
|
+
if (typeof store.state[props.name] === "number") {
|
|
169
|
+
return Number(value);
|
|
170
|
+
}
|
|
171
|
+
if (store.state[props.name] instanceof Date) {
|
|
172
|
+
return new Date(value);
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
function createChangeEventHandler() {
|
|
177
|
+
return (e) => {
|
|
178
|
+
const target = e.target;
|
|
179
|
+
if (props.type === "checkbox") {
|
|
180
|
+
store.dispatch((state) => {
|
|
181
|
+
state[props.name] = target.checked;
|
|
182
|
+
}, {
|
|
183
|
+
key: dispatchKey
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
store.dispatch((state) => {
|
|
187
|
+
state[props.name] = toStateValue(target.value);
|
|
188
|
+
}, {
|
|
189
|
+
key: dispatchKey
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
props.onChange?.(e);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const dispatchKey = useId();
|
|
196
|
+
const ref = useSubscriptionRef();
|
|
197
|
+
const name = String(props.name);
|
|
198
|
+
const defaultValue = getDefaultValue();
|
|
199
|
+
const defaultChecked = getDefaultChecked();
|
|
200
|
+
const onChange = createChangeEventHandler();
|
|
201
|
+
return {
|
|
202
|
+
key: dispatchKey,
|
|
203
|
+
ref,
|
|
204
|
+
name,
|
|
205
|
+
defaultValue,
|
|
206
|
+
defaultChecked,
|
|
207
|
+
onChange
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/use_store_input.tsx
|
|
212
|
+
function useStoreInput(store) {
|
|
213
|
+
const input = useCallback(
|
|
214
|
+
function Component(props) {
|
|
215
|
+
return /* @__PURE__ */ React2.createElement(Input, { store, ...props });
|
|
216
|
+
},
|
|
217
|
+
[]
|
|
218
|
+
);
|
|
219
|
+
const select = useCallback(
|
|
220
|
+
function Component(props) {
|
|
221
|
+
return /* @__PURE__ */ React2.createElement(Select, { store, ...props });
|
|
222
|
+
},
|
|
223
|
+
[]
|
|
224
|
+
);
|
|
225
|
+
const textarea = useCallback(function Component(props) {
|
|
226
|
+
return /* @__PURE__ */ React2.createElement(Textarea, { store, ...props });
|
|
227
|
+
}, []);
|
|
228
|
+
return {
|
|
229
|
+
input,
|
|
230
|
+
select,
|
|
231
|
+
textarea
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function Input({ store, ...props }) {
|
|
235
|
+
const storeProps = useStoreInputProps(store, props);
|
|
236
|
+
return /* @__PURE__ */ React2.createElement("input", { ...props, ...storeProps });
|
|
237
|
+
}
|
|
238
|
+
function Select({ store, ...props }) {
|
|
239
|
+
const storeProps = useStoreInputProps(store, props);
|
|
240
|
+
return /* @__PURE__ */ React2.createElement("select", { ...props, ...storeProps });
|
|
241
|
+
}
|
|
242
|
+
function Textarea({ store, ...props }) {
|
|
243
|
+
const storeProps = useStoreInputProps(store, props);
|
|
244
|
+
return /* @__PURE__ */ React2.createElement("textarea", { ...props, ...storeProps });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/use_form_store.tsx
|
|
248
|
+
function useFormStore(initialState) {
|
|
249
|
+
const store = useStore(initialState);
|
|
250
|
+
const storeInput = useStoreInput(store);
|
|
251
|
+
return {
|
|
252
|
+
get state() {
|
|
253
|
+
return store.state;
|
|
254
|
+
},
|
|
255
|
+
dispatch: store.dispatch,
|
|
256
|
+
subscribe: store.subscribe,
|
|
257
|
+
render: createRender(store),
|
|
258
|
+
...storeInput
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
export {
|
|
262
|
+
useFormStore
|
|
263
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
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_selector.tsx
|
|
21
|
+
var use_selector_exports = {};
|
|
22
|
+
__export(use_selector_exports, {
|
|
23
|
+
useSelector: () => useSelector
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_selector_exports);
|
|
26
|
+
var import_react2 = require("react");
|
|
27
|
+
|
|
28
|
+
// src/use_subscribe.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
function useSubscribe(store, subscriber) {
|
|
31
|
+
(0, import_react.useEffect)(() => {
|
|
32
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
33
|
+
return () => {
|
|
34
|
+
unsubscribe();
|
|
35
|
+
};
|
|
36
|
+
}, [store]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/use_selector.tsx
|
|
40
|
+
function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
41
|
+
const [state, setState] = (0, import_react2.useState)(selector(store.state));
|
|
42
|
+
useSubscribe(store, (newState) => {
|
|
43
|
+
const result = selector(newState);
|
|
44
|
+
if (compare(state, result)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
setState(result);
|
|
48
|
+
});
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
useSelector
|
|
54
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
export {
|
|
28
|
+
useSelector
|
|
29
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Store<TState> = ReturnType<typeof useStore<TState>>;
|
|
2
|
+
type DispatchOptions = {
|
|
3
|
+
key?: string;
|
|
4
|
+
};
|
|
5
|
+
type StoreSubscriber<TState> = (state: TState, key?: string) => void;
|
|
6
|
+
declare function useStore<TState>(initialState: TState): {
|
|
7
|
+
readonly state: TState;
|
|
8
|
+
dispatch: (recipe: Partial<TState> | ((draft: TState) => void), options?: DispatchOptions) => void;
|
|
9
|
+
subscribe: (subscriber: StoreSubscriber<TState>) => () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { type Store, type StoreSubscriber, useStore };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Store<TState> = ReturnType<typeof useStore<TState>>;
|
|
2
|
+
type DispatchOptions = {
|
|
3
|
+
key?: string;
|
|
4
|
+
};
|
|
5
|
+
type StoreSubscriber<TState> = (state: TState, key?: string) => void;
|
|
6
|
+
declare function useStore<TState>(initialState: TState): {
|
|
7
|
+
readonly state: TState;
|
|
8
|
+
dispatch: (recipe: Partial<TState> | ((draft: TState) => void), options?: DispatchOptions) => void;
|
|
9
|
+
subscribe: (subscriber: StoreSubscriber<TState>) => () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { type Store, type StoreSubscriber, useStore };
|