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
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025 Dndnsoft.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# React Store Input
|
|
2
|
+
|
|
3
|
+
The goal of this package is to make state management easier when using input elements in React.
|
|
4
|
+
|
|
5
|
+
It eliminates repetitive code required to implement state changes and subscriptions for input elements, and provides a simple interface.
|
|
6
|
+
|
|
7
|
+
At the same time, it allows you to use all the attributes originally provided by the input tag as-is, without needing to learn this package.
|
|
8
|
+
|
|
9
|
+
## Get Started
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { useFormStore } from "dn-react-input";
|
|
13
|
+
|
|
14
|
+
export default function App() {
|
|
15
|
+
const store = useFormStore({
|
|
16
|
+
email: "",
|
|
17
|
+
password: "",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const submit = async () => {
|
|
21
|
+
const { email, password } = store.state;
|
|
22
|
+
|
|
23
|
+
alert(`Email: ${email}\nPassword: ${password}`);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<form
|
|
28
|
+
onSubmit={(e) => {
|
|
29
|
+
e.preventDefault();
|
|
30
|
+
submit();
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
33
|
+
<store.input name="email" type="email" />
|
|
34
|
+
<store.input name="password" type="password" />
|
|
35
|
+
<button type="submit">Submit</button>
|
|
36
|
+
</form>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { Store } from './use_store.mjs';
|
|
3
|
+
|
|
4
|
+
type CreateRender<TState> = (selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean) => ReactNode;
|
|
5
|
+
declare function createRender<TState>(store: Store<TState>): CreateRender<TState>;
|
|
6
|
+
|
|
7
|
+
export { type CreateRender, createRender };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { Store } from './use_store.js';
|
|
3
|
+
|
|
4
|
+
type CreateRender<TState> = (selector: (state: TState) => ReactNode, compare?: (a: TState, b: TState) => boolean) => ReactNode;
|
|
5
|
+
declare function createRender<TState>(store: Store<TState>): CreateRender<TState>;
|
|
6
|
+
|
|
7
|
+
export { type CreateRender, createRender };
|
|
@@ -0,0 +1,79 @@
|
|
|
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/create_render.tsx
|
|
31
|
+
var create_render_exports = {};
|
|
32
|
+
__export(create_render_exports, {
|
|
33
|
+
createRender: () => createRender
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(create_render_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
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
createRender
|
|
79
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
export {
|
|
41
|
+
createRender
|
|
42
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Store, StoreSubscriber, useStore } from './use_store.mjs';
|
|
2
|
+
export { useSelector } from './use_selector.mjs';
|
|
3
|
+
export { useSubscribe } from './use_subscribe.mjs';
|
|
4
|
+
export { Input, Select, StoreInputPropsWithStore, Textarea, useStoreInput } from './use_store_input.mjs';
|
|
5
|
+
export { useStoreInputProps } from './use_store_input_props.mjs';
|
|
6
|
+
export { FormStore, useFormStore } from './use_form_store.mjs';
|
|
7
|
+
export { deserialize, serialize } from './serialize.mjs';
|
|
8
|
+
import 'react';
|
|
9
|
+
import './create_render.mjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Store, StoreSubscriber, useStore } from './use_store.js';
|
|
2
|
+
export { useSelector } from './use_selector.js';
|
|
3
|
+
export { useSubscribe } from './use_subscribe.js';
|
|
4
|
+
export { Input, Select, StoreInputPropsWithStore, Textarea, useStoreInput } from './use_store_input.js';
|
|
5
|
+
export { useStoreInputProps } from './use_store_input_props.js';
|
|
6
|
+
export { FormStore, useFormStore } from './use_form_store.js';
|
|
7
|
+
export { deserialize, serialize } from './serialize.js';
|
|
8
|
+
import 'react';
|
|
9
|
+
import './create_render.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
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/index.tsx
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Input: () => Input,
|
|
34
|
+
Select: () => Select,
|
|
35
|
+
Textarea: () => Textarea,
|
|
36
|
+
deserialize: () => deserialize,
|
|
37
|
+
serialize: () => serialize,
|
|
38
|
+
useFormStore: () => useFormStore,
|
|
39
|
+
useSelector: () => useSelector,
|
|
40
|
+
useStore: () => useStore,
|
|
41
|
+
useStoreInput: () => useStoreInput,
|
|
42
|
+
useStoreInputProps: () => useStoreInputProps,
|
|
43
|
+
useSubscribe: () => useSubscribe
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/use_store.tsx
|
|
48
|
+
var import_react = require("react");
|
|
49
|
+
var import_immer = require("immer");
|
|
50
|
+
function useStore(initialState) {
|
|
51
|
+
const stateRef = (0, import_react.useRef)(initialState);
|
|
52
|
+
const subscribers = (0, import_react.useRef)([]);
|
|
53
|
+
const dispatch = (recipe, options = {}) => {
|
|
54
|
+
if (typeof recipe === "function") {
|
|
55
|
+
stateRef.current = (0, import_immer.produce)(stateRef.current, recipe);
|
|
56
|
+
} else {
|
|
57
|
+
stateRef.current = (0, import_immer.produce)(
|
|
58
|
+
stateRef.current,
|
|
59
|
+
(draft) => {
|
|
60
|
+
for (const key in recipe) {
|
|
61
|
+
draft[key] = recipe[key];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
notify(options.key);
|
|
67
|
+
};
|
|
68
|
+
const subscribe = (subscriber) => {
|
|
69
|
+
subscribers.current.push(subscriber);
|
|
70
|
+
return () => {
|
|
71
|
+
const index = subscribers.current.indexOf(subscriber);
|
|
72
|
+
if (index !== -1) {
|
|
73
|
+
subscribers.current.splice(index, 1);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
const notify = (key) => {
|
|
78
|
+
for (const listener of subscribers.current) {
|
|
79
|
+
listener(stateRef.current, key);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
get state() {
|
|
84
|
+
return stateRef.current;
|
|
85
|
+
},
|
|
86
|
+
dispatch,
|
|
87
|
+
subscribe
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/use_selector.tsx
|
|
92
|
+
var import_react3 = require("react");
|
|
93
|
+
|
|
94
|
+
// src/use_subscribe.tsx
|
|
95
|
+
var import_react2 = require("react");
|
|
96
|
+
function useSubscribe(store, subscriber) {
|
|
97
|
+
(0, import_react2.useEffect)(() => {
|
|
98
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
99
|
+
return () => {
|
|
100
|
+
unsubscribe();
|
|
101
|
+
};
|
|
102
|
+
}, [store]);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/use_selector.tsx
|
|
106
|
+
function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
107
|
+
const [state, setState] = (0, import_react3.useState)(selector(store.state));
|
|
108
|
+
useSubscribe(store, (newState) => {
|
|
109
|
+
const result = selector(newState);
|
|
110
|
+
if (compare(state, result)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
setState(result);
|
|
114
|
+
});
|
|
115
|
+
return state;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/use_store_input.tsx
|
|
119
|
+
var import_react5 = require("react");
|
|
120
|
+
var import_react6 = __toESM(require("react"));
|
|
121
|
+
|
|
122
|
+
// src/use_store_input_props.tsx
|
|
123
|
+
var import_react4 = require("react");
|
|
124
|
+
var import_date_fns = require("date-fns");
|
|
125
|
+
function useStoreInputProps(store, props) {
|
|
126
|
+
const toInputValue = (value) => {
|
|
127
|
+
if (value === void 0 || value === null) {
|
|
128
|
+
return "";
|
|
129
|
+
}
|
|
130
|
+
if (props.type === "datetime-local") {
|
|
131
|
+
if (value instanceof Date) {
|
|
132
|
+
return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
133
|
+
}
|
|
134
|
+
if (typeof value === "string") {
|
|
135
|
+
return toInputValue(new Date(value));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return String(value);
|
|
139
|
+
};
|
|
140
|
+
const getDefaultValue = () => {
|
|
141
|
+
if (props.defaultValue !== void 0) {
|
|
142
|
+
return props.defaultValue;
|
|
143
|
+
}
|
|
144
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
145
|
+
return void 0;
|
|
146
|
+
}
|
|
147
|
+
return toInputValue(store.state[props.name]);
|
|
148
|
+
};
|
|
149
|
+
const toInputChecked = (value) => {
|
|
150
|
+
if (props.type === "radio") {
|
|
151
|
+
return value !== void 0 && value === props.value;
|
|
152
|
+
}
|
|
153
|
+
return Boolean(value);
|
|
154
|
+
};
|
|
155
|
+
const getDefaultChecked = () => {
|
|
156
|
+
if (props.defaultChecked) {
|
|
157
|
+
return props.defaultChecked;
|
|
158
|
+
}
|
|
159
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
160
|
+
return void 0;
|
|
161
|
+
}
|
|
162
|
+
return toInputChecked(store.state[props.name]);
|
|
163
|
+
};
|
|
164
|
+
function useSubscriptionRef() {
|
|
165
|
+
const ref2 = (0, import_react4.useRef)(null);
|
|
166
|
+
(0, import_react4.useEffect)(() => {
|
|
167
|
+
return store.subscribe((state, key) => {
|
|
168
|
+
if (key === dispatchKey) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const input = ref2.current;
|
|
172
|
+
if (!input) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
176
|
+
const checked = toInputChecked(state[props.name]);
|
|
177
|
+
if (input.checked === checked) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
input.checked = checked;
|
|
181
|
+
} else {
|
|
182
|
+
const value = toInputValue(state[props.name]);
|
|
183
|
+
if (input.value === value) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
input.value = value;
|
|
187
|
+
}
|
|
188
|
+
const event = new Event("input", { bubbles: true });
|
|
189
|
+
input.dispatchEvent(event);
|
|
190
|
+
});
|
|
191
|
+
}, []);
|
|
192
|
+
return ref2;
|
|
193
|
+
}
|
|
194
|
+
function toStateValue(value) {
|
|
195
|
+
if (typeof store.state[props.name] === "number") {
|
|
196
|
+
return Number(value);
|
|
197
|
+
}
|
|
198
|
+
if (store.state[props.name] instanceof Date) {
|
|
199
|
+
return new Date(value);
|
|
200
|
+
}
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
function createChangeEventHandler() {
|
|
204
|
+
return (e) => {
|
|
205
|
+
const target = e.target;
|
|
206
|
+
if (props.type === "checkbox") {
|
|
207
|
+
store.dispatch((state) => {
|
|
208
|
+
state[props.name] = target.checked;
|
|
209
|
+
}, {
|
|
210
|
+
key: dispatchKey
|
|
211
|
+
});
|
|
212
|
+
} else {
|
|
213
|
+
store.dispatch((state) => {
|
|
214
|
+
state[props.name] = toStateValue(target.value);
|
|
215
|
+
}, {
|
|
216
|
+
key: dispatchKey
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
props.onChange?.(e);
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const dispatchKey = (0, import_react4.useId)();
|
|
223
|
+
const ref = useSubscriptionRef();
|
|
224
|
+
const name = String(props.name);
|
|
225
|
+
const defaultValue = getDefaultValue();
|
|
226
|
+
const defaultChecked = getDefaultChecked();
|
|
227
|
+
const onChange = createChangeEventHandler();
|
|
228
|
+
return {
|
|
229
|
+
key: dispatchKey,
|
|
230
|
+
ref,
|
|
231
|
+
name,
|
|
232
|
+
defaultValue,
|
|
233
|
+
defaultChecked,
|
|
234
|
+
onChange
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/use_store_input.tsx
|
|
239
|
+
function useStoreInput(store) {
|
|
240
|
+
const input = (0, import_react5.useCallback)(
|
|
241
|
+
function Component(props) {
|
|
242
|
+
return /* @__PURE__ */ import_react6.default.createElement(Input, { store, ...props });
|
|
243
|
+
},
|
|
244
|
+
[]
|
|
245
|
+
);
|
|
246
|
+
const select = (0, import_react5.useCallback)(
|
|
247
|
+
function Component(props) {
|
|
248
|
+
return /* @__PURE__ */ import_react6.default.createElement(Select, { store, ...props });
|
|
249
|
+
},
|
|
250
|
+
[]
|
|
251
|
+
);
|
|
252
|
+
const textarea = (0, import_react5.useCallback)(function Component(props) {
|
|
253
|
+
return /* @__PURE__ */ import_react6.default.createElement(Textarea, { store, ...props });
|
|
254
|
+
}, []);
|
|
255
|
+
return {
|
|
256
|
+
input,
|
|
257
|
+
select,
|
|
258
|
+
textarea
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function Input({ store, ...props }) {
|
|
262
|
+
const storeProps = useStoreInputProps(store, props);
|
|
263
|
+
return /* @__PURE__ */ import_react6.default.createElement("input", { ...props, ...storeProps });
|
|
264
|
+
}
|
|
265
|
+
function Select({ store, ...props }) {
|
|
266
|
+
const storeProps = useStoreInputProps(store, props);
|
|
267
|
+
return /* @__PURE__ */ import_react6.default.createElement("select", { ...props, ...storeProps });
|
|
268
|
+
}
|
|
269
|
+
function Textarea({ store, ...props }) {
|
|
270
|
+
const storeProps = useStoreInputProps(store, props);
|
|
271
|
+
return /* @__PURE__ */ import_react6.default.createElement("textarea", { ...props, ...storeProps });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/create_render.tsx
|
|
275
|
+
var import_react7 = __toESM(require("react"));
|
|
276
|
+
function createRender(store) {
|
|
277
|
+
return function createRender2(selector, compare) {
|
|
278
|
+
function Component() {
|
|
279
|
+
const result = useSelector(store, (state) => state, compare);
|
|
280
|
+
const content = selector(result);
|
|
281
|
+
return /* @__PURE__ */ import_react7.default.createElement(import_react7.default.Fragment, null, content);
|
|
282
|
+
}
|
|
283
|
+
return /* @__PURE__ */ import_react7.default.createElement(Component, null);
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// src/use_form_store.tsx
|
|
288
|
+
function useFormStore(initialState) {
|
|
289
|
+
const store = useStore(initialState);
|
|
290
|
+
const storeInput = useStoreInput(store);
|
|
291
|
+
return {
|
|
292
|
+
get state() {
|
|
293
|
+
return store.state;
|
|
294
|
+
},
|
|
295
|
+
dispatch: store.dispatch,
|
|
296
|
+
subscribe: store.subscribe,
|
|
297
|
+
render: createRender(store),
|
|
298
|
+
...storeInput
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/serialize.ts
|
|
303
|
+
function serialize(value) {
|
|
304
|
+
if (value === void 0) {
|
|
305
|
+
return void 0;
|
|
306
|
+
}
|
|
307
|
+
if (value === null) {
|
|
308
|
+
return {
|
|
309
|
+
type: "null",
|
|
310
|
+
value: null
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (typeof value === "string") {
|
|
314
|
+
return {
|
|
315
|
+
type: "string",
|
|
316
|
+
value
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
if (typeof value === "number") {
|
|
320
|
+
return {
|
|
321
|
+
type: "number",
|
|
322
|
+
value
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
if (typeof value === "boolean") {
|
|
326
|
+
return {
|
|
327
|
+
type: "boolean",
|
|
328
|
+
value
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
if (value instanceof Date) {
|
|
332
|
+
return {
|
|
333
|
+
type: "date",
|
|
334
|
+
value: value.toISOString()
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
if (Array.isArray(value)) {
|
|
338
|
+
return {
|
|
339
|
+
type: "array",
|
|
340
|
+
value: value.map((item) => serialize(item))
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
if (typeof value === "object") {
|
|
344
|
+
return {
|
|
345
|
+
type: "object",
|
|
346
|
+
value: Object.entries(value).reduce((acc, [key, value2]) => {
|
|
347
|
+
return {
|
|
348
|
+
...acc,
|
|
349
|
+
[key]: serialize(value2)
|
|
350
|
+
};
|
|
351
|
+
}, {})
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
return void 0;
|
|
355
|
+
}
|
|
356
|
+
function deserialize(data) {
|
|
357
|
+
if (data === void 0) {
|
|
358
|
+
return void 0;
|
|
359
|
+
}
|
|
360
|
+
if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
|
|
361
|
+
const { type, value } = data;
|
|
362
|
+
switch (type) {
|
|
363
|
+
case "null":
|
|
364
|
+
return null;
|
|
365
|
+
case "string":
|
|
366
|
+
return value;
|
|
367
|
+
case "number":
|
|
368
|
+
return value;
|
|
369
|
+
case "boolean":
|
|
370
|
+
return value;
|
|
371
|
+
case "date":
|
|
372
|
+
return new Date(value);
|
|
373
|
+
case "array":
|
|
374
|
+
return value.map((item) => deserialize(item));
|
|
375
|
+
case "object":
|
|
376
|
+
return Object.entries(value).reduce((acc, [key, value2]) => {
|
|
377
|
+
return {
|
|
378
|
+
...acc,
|
|
379
|
+
[key]: deserialize(value2)
|
|
380
|
+
};
|
|
381
|
+
}, {});
|
|
382
|
+
default:
|
|
383
|
+
return void 0;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return void 0;
|
|
387
|
+
}
|
|
388
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
389
|
+
0 && (module.exports = {
|
|
390
|
+
Input,
|
|
391
|
+
Select,
|
|
392
|
+
Textarea,
|
|
393
|
+
deserialize,
|
|
394
|
+
serialize,
|
|
395
|
+
useFormStore,
|
|
396
|
+
useSelector,
|
|
397
|
+
useStore,
|
|
398
|
+
useStoreInput,
|
|
399
|
+
useStoreInputProps,
|
|
400
|
+
useSubscribe
|
|
401
|
+
});
|