react-store-input 0.1.1 → 0.1.4
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/README.md +92 -29
- package/dist/index.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +136 -100
- package/dist/index.mjs +124 -94
- package/dist/use_form_store.d.mts +2 -2
- package/dist/use_form_store.d.ts +2 -2
- package/dist/use_form_store.js +130 -94
- package/dist/use_form_store.mjs +121 -89
- package/dist/use_store_component.d.mts +21 -0
- package/dist/use_store_component.d.ts +21 -0
- package/dist/use_store_component.js +262 -0
- package/dist/use_store_component.mjs +226 -0
- package/dist/use_store_controller.d.mts +15 -0
- package/dist/use_store_controller.d.ts +15 -0
- package/dist/use_store_controller.js +81 -0
- package/dist/use_store_controller.mjs +56 -0
- package/dist/use_store_input.d.mts +18 -14
- package/dist/use_store_input.d.ts +18 -14
- package/dist/use_store_input.js +93 -133
- package/dist/use_store_input.mjs +95 -128
- package/dist/use_store_input_with_name.d.mts +16 -0
- package/dist/use_store_input_with_name.d.ts +16 -0
- package/dist/use_store_input_with_name.js +199 -0
- package/dist/use_store_input_with_name.mjs +172 -0
- package/package.json +43 -43
- package/dist/use_store_input_props.d.mts +0 -24
- package/dist/use_store_input_props.d.ts +0 -24
- package/dist/use_store_input_props.js +0 -154
- package/dist/use_store_input_props.mjs +0 -133
package/dist/use_form_store.mjs
CHANGED
|
@@ -82,20 +82,59 @@ function useStore(initialState) {
|
|
|
82
82
|
};
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
// src/
|
|
85
|
+
// src/use_store_component.tsx
|
|
86
86
|
import {
|
|
87
87
|
useCallback
|
|
88
88
|
} from "react";
|
|
89
89
|
import React2 from "react";
|
|
90
90
|
|
|
91
|
-
// src/
|
|
92
|
-
import
|
|
93
|
-
useEffect as useEffect2,
|
|
94
|
-
useId,
|
|
95
|
-
useRef as useRef2
|
|
96
|
-
} from "react";
|
|
91
|
+
// src/use_store_input.tsx
|
|
92
|
+
import "react";
|
|
97
93
|
import { format } from "date-fns";
|
|
98
|
-
|
|
94
|
+
|
|
95
|
+
// src/use_store_controller.tsx
|
|
96
|
+
import { useEffect as useEffect2, useId, useRef as useRef2 } from "react";
|
|
97
|
+
function useStoreController(store, props) {
|
|
98
|
+
const ref = useRef2(null);
|
|
99
|
+
const dispatchKey = useId();
|
|
100
|
+
useEffect2(() => {
|
|
101
|
+
const element = ref.current;
|
|
102
|
+
if (!element) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (typeof props.ref === "function") {
|
|
106
|
+
props.ref(element);
|
|
107
|
+
} else if (props.ref) {
|
|
108
|
+
props.ref.current = element;
|
|
109
|
+
}
|
|
110
|
+
}, []);
|
|
111
|
+
useSubscribe(store, (state, key) => {
|
|
112
|
+
if (key === dispatchKey) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (!ref.current) return;
|
|
116
|
+
props.onSubscribe(state, ref.current);
|
|
117
|
+
});
|
|
118
|
+
const onChange = () => {
|
|
119
|
+
store.dispatch(
|
|
120
|
+
(state) => {
|
|
121
|
+
const element = ref.current;
|
|
122
|
+
if (!element) return;
|
|
123
|
+
props.onDispatch(state, element);
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
key: dispatchKey
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
ref,
|
|
132
|
+
onChange
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/use_store_input.tsx
|
|
137
|
+
function useStoreInput(store, props) {
|
|
99
138
|
const toInputValue = (value) => {
|
|
100
139
|
if (value === void 0 || value === null) {
|
|
101
140
|
return "";
|
|
@@ -117,7 +156,7 @@ function useStoreInputProps(store, props) {
|
|
|
117
156
|
if (props.type === "checkbox" || props.type === "radio") {
|
|
118
157
|
return void 0;
|
|
119
158
|
}
|
|
120
|
-
return toInputValue(store.state
|
|
159
|
+
return toInputValue(props.getter(store.state));
|
|
121
160
|
};
|
|
122
161
|
const toInputChecked = (value) => {
|
|
123
162
|
if (props.type === "radio") {
|
|
@@ -132,95 +171,82 @@ function useStoreInputProps(store, props) {
|
|
|
132
171
|
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
133
172
|
return void 0;
|
|
134
173
|
}
|
|
135
|
-
return toInputChecked(store.state
|
|
174
|
+
return toInputChecked(props.getter(store.state));
|
|
136
175
|
};
|
|
137
|
-
function useSubscriptionRef() {
|
|
138
|
-
const ref2 = useRef2(null);
|
|
139
|
-
useEffect2(() => {
|
|
140
|
-
const input = ref2.current;
|
|
141
|
-
if (!input) {
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
if (typeof props.ref === "function") {
|
|
145
|
-
props.ref(input);
|
|
146
|
-
} else if (props.ref) {
|
|
147
|
-
props.ref.current = input;
|
|
148
|
-
}
|
|
149
|
-
return store.subscribe((state, key) => {
|
|
150
|
-
if (key === dispatchKey) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
154
|
-
const checked = toInputChecked(state[props.name]);
|
|
155
|
-
if (input.checked === checked) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
input.checked = checked;
|
|
159
|
-
} else {
|
|
160
|
-
const value = toInputValue(state[props.name]);
|
|
161
|
-
if (input.value === value) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
input.value = value;
|
|
165
|
-
}
|
|
166
|
-
const event = new Event("input", { bubbles: true });
|
|
167
|
-
input.dispatchEvent(event);
|
|
168
|
-
});
|
|
169
|
-
}, []);
|
|
170
|
-
return ref2;
|
|
171
|
-
}
|
|
172
176
|
function toStateValue(value) {
|
|
173
|
-
|
|
177
|
+
const selected = props.getter(store.state);
|
|
178
|
+
if (typeof selected === "number") {
|
|
174
179
|
return Number(value);
|
|
175
180
|
}
|
|
176
|
-
if (
|
|
181
|
+
if (selected instanceof Date) {
|
|
177
182
|
return new Date(value);
|
|
178
183
|
}
|
|
179
184
|
return value;
|
|
180
185
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (props.type === "checkbox") {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
key: dispatchKey
|
|
191
|
-
}
|
|
192
|
-
);
|
|
186
|
+
const inputProps = useStoreController(store, {
|
|
187
|
+
ref: props.ref,
|
|
188
|
+
onSubscribe: (state, element) => {
|
|
189
|
+
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
190
|
+
const checked = toInputChecked(props.getter(state));
|
|
191
|
+
if (element.checked === checked) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
element.checked = checked;
|
|
193
195
|
} else {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
key: dispatchKey
|
|
200
|
-
}
|
|
201
|
-
);
|
|
196
|
+
const value = toInputValue(props.getter(state));
|
|
197
|
+
if (element.value === value) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
element.value = value;
|
|
202
201
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
202
|
+
const event = new Event("input", { bubbles: true });
|
|
203
|
+
element.dispatchEvent(event);
|
|
204
|
+
},
|
|
205
|
+
onDispatch: (state, element) => {
|
|
206
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
207
|
+
props.setter(state, element.checked);
|
|
208
|
+
} else {
|
|
209
|
+
props.setter(state, toStateValue(element.value));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
212
213
|
return {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
214
|
+
ref: inputProps.ref,
|
|
215
|
+
defaultValue: getDefaultValue(),
|
|
216
|
+
defaultChecked: getDefaultChecked(),
|
|
217
|
+
onChange: (event) => {
|
|
218
|
+
inputProps.onChange();
|
|
219
|
+
props.onChange?.(event);
|
|
220
|
+
}
|
|
219
221
|
};
|
|
220
222
|
}
|
|
221
223
|
|
|
222
|
-
// src/
|
|
223
|
-
function
|
|
224
|
+
// src/use_store_input_with_name.tsx
|
|
225
|
+
function useStoreInputWithName(store, props) {
|
|
226
|
+
const inputProps = useStoreInput(store, {
|
|
227
|
+
...props,
|
|
228
|
+
getter: (state) => {
|
|
229
|
+
if (props.getter) {
|
|
230
|
+
return props.getter(state);
|
|
231
|
+
}
|
|
232
|
+
return state[props.name];
|
|
233
|
+
},
|
|
234
|
+
setter: (state, value) => {
|
|
235
|
+
if (props.setter) {
|
|
236
|
+
props.setter(state, value);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
state[props.name] = value;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return {
|
|
243
|
+
...inputProps,
|
|
244
|
+
name: "name" in props ? String(props.name) : void 0
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/use_store_component.tsx
|
|
249
|
+
function useStoreComponent(store) {
|
|
224
250
|
const input = useCallback(function Component(props) {
|
|
225
251
|
return /* @__PURE__ */ React2.createElement(Input, { store, ...props });
|
|
226
252
|
}, []);
|
|
@@ -238,30 +264,36 @@ function useStoreInput(store) {
|
|
|
238
264
|
}
|
|
239
265
|
function Input({
|
|
240
266
|
store,
|
|
267
|
+
getter,
|
|
268
|
+
setter,
|
|
241
269
|
...props
|
|
242
270
|
}) {
|
|
243
|
-
const storeProps =
|
|
271
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
244
272
|
return /* @__PURE__ */ React2.createElement("input", { ...props, ...storeProps });
|
|
245
273
|
}
|
|
246
274
|
function Select({
|
|
247
275
|
store,
|
|
276
|
+
getter,
|
|
277
|
+
setter,
|
|
248
278
|
...props
|
|
249
279
|
}) {
|
|
250
|
-
const storeProps =
|
|
280
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
251
281
|
return /* @__PURE__ */ React2.createElement("select", { ...props, ...storeProps });
|
|
252
282
|
}
|
|
253
283
|
function Textarea({
|
|
254
284
|
store,
|
|
285
|
+
getter,
|
|
286
|
+
setter,
|
|
255
287
|
...props
|
|
256
288
|
}) {
|
|
257
|
-
const storeProps =
|
|
289
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
258
290
|
return /* @__PURE__ */ React2.createElement("textarea", { ...props, ...storeProps });
|
|
259
291
|
}
|
|
260
292
|
|
|
261
293
|
// src/use_form_store.tsx
|
|
262
294
|
function useFormStore(initialState) {
|
|
263
295
|
const store = useStore(initialState);
|
|
264
|
-
const
|
|
296
|
+
const storeComponent = useStoreComponent(store);
|
|
265
297
|
return {
|
|
266
298
|
get state() {
|
|
267
299
|
return store.state;
|
|
@@ -269,7 +301,7 @@ function useFormStore(initialState) {
|
|
|
269
301
|
dispatch: store.dispatch,
|
|
270
302
|
subscribe: store.subscribe,
|
|
271
303
|
render: createRenderWithStore(store),
|
|
272
|
-
...
|
|
304
|
+
...storeComponent
|
|
273
305
|
};
|
|
274
306
|
}
|
|
275
307
|
export {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React__default, { DetailedHTMLProps, InputHTMLAttributes } from 'react';
|
|
2
|
+
import { Store } from './use_store.mjs';
|
|
3
|
+
|
|
4
|
+
type StoreComponentProps<TInputElement, TState> = Omit<DetailedHTMLProps<InputHTMLAttributes<TInputElement>, TInputElement>, "name"> & {
|
|
5
|
+
name?: keyof TState;
|
|
6
|
+
getter?: (state: TState) => unknown;
|
|
7
|
+
setter?: (state: TState, value: unknown) => void;
|
|
8
|
+
};
|
|
9
|
+
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
10
|
+
input: React__default.FC<StoreComponentProps<HTMLInputElement, TState>>;
|
|
11
|
+
select: React__default.FC<StoreComponentProps<HTMLSelectElement, TState>>;
|
|
12
|
+
textarea: React__default.FC<StoreComponentProps<HTMLTextAreaElement, TState>>;
|
|
13
|
+
};
|
|
14
|
+
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreComponentProps<TElement, TState> & {
|
|
15
|
+
store: Store<TState>;
|
|
16
|
+
};
|
|
17
|
+
declare function Input<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
|
|
18
|
+
declare function Select<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
|
|
19
|
+
declare function Textarea<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
|
|
20
|
+
|
|
21
|
+
export { Input, Select, type StoreComponentProps, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React__default, { DetailedHTMLProps, InputHTMLAttributes } from 'react';
|
|
2
|
+
import { Store } from './use_store.js';
|
|
3
|
+
|
|
4
|
+
type StoreComponentProps<TInputElement, TState> = Omit<DetailedHTMLProps<InputHTMLAttributes<TInputElement>, TInputElement>, "name"> & {
|
|
5
|
+
name?: keyof TState;
|
|
6
|
+
getter?: (state: TState) => unknown;
|
|
7
|
+
setter?: (state: TState, value: unknown) => void;
|
|
8
|
+
};
|
|
9
|
+
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
10
|
+
input: React__default.FC<StoreComponentProps<HTMLInputElement, TState>>;
|
|
11
|
+
select: React__default.FC<StoreComponentProps<HTMLSelectElement, TState>>;
|
|
12
|
+
textarea: React__default.FC<StoreComponentProps<HTMLTextAreaElement, TState>>;
|
|
13
|
+
};
|
|
14
|
+
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreComponentProps<TElement, TState> & {
|
|
15
|
+
store: Store<TState>;
|
|
16
|
+
};
|
|
17
|
+
declare function Input<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
|
|
18
|
+
declare function Select<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
|
|
19
|
+
declare function Textarea<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
|
|
20
|
+
|
|
21
|
+
export { Input, Select, type StoreComponentProps, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
|
|
@@ -0,0 +1,262 @@
|
|
|
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_store_component.tsx
|
|
31
|
+
var use_store_component_exports = {};
|
|
32
|
+
__export(use_store_component_exports, {
|
|
33
|
+
Input: () => Input,
|
|
34
|
+
Select: () => Select,
|
|
35
|
+
Textarea: () => Textarea,
|
|
36
|
+
useStoreComponent: () => useStoreComponent
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(use_store_component_exports);
|
|
39
|
+
var import_react4 = require("react");
|
|
40
|
+
var import_react5 = __toESM(require("react"));
|
|
41
|
+
|
|
42
|
+
// src/use_store_input.tsx
|
|
43
|
+
var import_react3 = require("react");
|
|
44
|
+
var import_date_fns = require("date-fns");
|
|
45
|
+
|
|
46
|
+
// src/use_store_controller.tsx
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
|
|
49
|
+
// src/use_subscribe.tsx
|
|
50
|
+
var import_react = require("react");
|
|
51
|
+
function useSubscribe(store, subscriber) {
|
|
52
|
+
(0, import_react.useEffect)(() => {
|
|
53
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
54
|
+
return () => {
|
|
55
|
+
unsubscribe();
|
|
56
|
+
};
|
|
57
|
+
}, [store]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/use_store_controller.tsx
|
|
61
|
+
function useStoreController(store, props) {
|
|
62
|
+
const ref = (0, import_react2.useRef)(null);
|
|
63
|
+
const dispatchKey = (0, import_react2.useId)();
|
|
64
|
+
(0, import_react2.useEffect)(() => {
|
|
65
|
+
const element = ref.current;
|
|
66
|
+
if (!element) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (typeof props.ref === "function") {
|
|
70
|
+
props.ref(element);
|
|
71
|
+
} else if (props.ref) {
|
|
72
|
+
props.ref.current = element;
|
|
73
|
+
}
|
|
74
|
+
}, []);
|
|
75
|
+
useSubscribe(store, (state, key) => {
|
|
76
|
+
if (key === dispatchKey) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!ref.current) return;
|
|
80
|
+
props.onSubscribe(state, ref.current);
|
|
81
|
+
});
|
|
82
|
+
const onChange = () => {
|
|
83
|
+
store.dispatch(
|
|
84
|
+
(state) => {
|
|
85
|
+
const element = ref.current;
|
|
86
|
+
if (!element) return;
|
|
87
|
+
props.onDispatch(state, element);
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
key: dispatchKey
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
ref,
|
|
96
|
+
onChange
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/use_store_input.tsx
|
|
101
|
+
function useStoreInput(store, props) {
|
|
102
|
+
const toInputValue = (value) => {
|
|
103
|
+
if (value === void 0 || value === null) {
|
|
104
|
+
return "";
|
|
105
|
+
}
|
|
106
|
+
if (props.type === "datetime-local") {
|
|
107
|
+
if (value instanceof Date) {
|
|
108
|
+
return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
109
|
+
}
|
|
110
|
+
if (typeof value === "string") {
|
|
111
|
+
return toInputValue(new Date(value));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return String(value);
|
|
115
|
+
};
|
|
116
|
+
const getDefaultValue = () => {
|
|
117
|
+
if (props.defaultValue !== void 0) {
|
|
118
|
+
return props.defaultValue;
|
|
119
|
+
}
|
|
120
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
121
|
+
return void 0;
|
|
122
|
+
}
|
|
123
|
+
return toInputValue(props.getter(store.state));
|
|
124
|
+
};
|
|
125
|
+
const toInputChecked = (value) => {
|
|
126
|
+
if (props.type === "radio") {
|
|
127
|
+
return value !== void 0 && value === props.value;
|
|
128
|
+
}
|
|
129
|
+
return Boolean(value);
|
|
130
|
+
};
|
|
131
|
+
const getDefaultChecked = () => {
|
|
132
|
+
if (props.defaultChecked) {
|
|
133
|
+
return props.defaultChecked;
|
|
134
|
+
}
|
|
135
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
136
|
+
return void 0;
|
|
137
|
+
}
|
|
138
|
+
return toInputChecked(props.getter(store.state));
|
|
139
|
+
};
|
|
140
|
+
function toStateValue(value) {
|
|
141
|
+
const selected = props.getter(store.state);
|
|
142
|
+
if (typeof selected === "number") {
|
|
143
|
+
return Number(value);
|
|
144
|
+
}
|
|
145
|
+
if (selected instanceof Date) {
|
|
146
|
+
return new Date(value);
|
|
147
|
+
}
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
const inputProps = useStoreController(store, {
|
|
151
|
+
ref: props.ref,
|
|
152
|
+
onSubscribe: (state, element) => {
|
|
153
|
+
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
154
|
+
const checked = toInputChecked(props.getter(state));
|
|
155
|
+
if (element.checked === checked) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
element.checked = checked;
|
|
159
|
+
} else {
|
|
160
|
+
const value = toInputValue(props.getter(state));
|
|
161
|
+
if (element.value === value) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
element.value = value;
|
|
165
|
+
}
|
|
166
|
+
const event = new Event("input", { bubbles: true });
|
|
167
|
+
element.dispatchEvent(event);
|
|
168
|
+
},
|
|
169
|
+
onDispatch: (state, element) => {
|
|
170
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
171
|
+
props.setter(state, element.checked);
|
|
172
|
+
} else {
|
|
173
|
+
props.setter(state, toStateValue(element.value));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
ref: inputProps.ref,
|
|
179
|
+
defaultValue: getDefaultValue(),
|
|
180
|
+
defaultChecked: getDefaultChecked(),
|
|
181
|
+
onChange: (event) => {
|
|
182
|
+
inputProps.onChange();
|
|
183
|
+
props.onChange?.(event);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/use_store_input_with_name.tsx
|
|
189
|
+
function useStoreInputWithName(store, props) {
|
|
190
|
+
const inputProps = useStoreInput(store, {
|
|
191
|
+
...props,
|
|
192
|
+
getter: (state) => {
|
|
193
|
+
if (props.getter) {
|
|
194
|
+
return props.getter(state);
|
|
195
|
+
}
|
|
196
|
+
return state[props.name];
|
|
197
|
+
},
|
|
198
|
+
setter: (state, value) => {
|
|
199
|
+
if (props.setter) {
|
|
200
|
+
props.setter(state, value);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
state[props.name] = value;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
...inputProps,
|
|
208
|
+
name: "name" in props ? String(props.name) : void 0
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/use_store_component.tsx
|
|
213
|
+
function useStoreComponent(store) {
|
|
214
|
+
const input = (0, import_react4.useCallback)(function Component(props) {
|
|
215
|
+
return /* @__PURE__ */ import_react5.default.createElement(Input, { store, ...props });
|
|
216
|
+
}, []);
|
|
217
|
+
const select = (0, import_react4.useCallback)(function Component(props) {
|
|
218
|
+
return /* @__PURE__ */ import_react5.default.createElement(Select, { store, ...props });
|
|
219
|
+
}, []);
|
|
220
|
+
const textarea = (0, import_react4.useCallback)(function Component(props) {
|
|
221
|
+
return /* @__PURE__ */ import_react5.default.createElement(Textarea, { store, ...props });
|
|
222
|
+
}, []);
|
|
223
|
+
return {
|
|
224
|
+
input,
|
|
225
|
+
select,
|
|
226
|
+
textarea
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function Input({
|
|
230
|
+
store,
|
|
231
|
+
getter,
|
|
232
|
+
setter,
|
|
233
|
+
...props
|
|
234
|
+
}) {
|
|
235
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
236
|
+
return /* @__PURE__ */ import_react5.default.createElement("input", { ...props, ...storeProps });
|
|
237
|
+
}
|
|
238
|
+
function Select({
|
|
239
|
+
store,
|
|
240
|
+
getter,
|
|
241
|
+
setter,
|
|
242
|
+
...props
|
|
243
|
+
}) {
|
|
244
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
245
|
+
return /* @__PURE__ */ import_react5.default.createElement("select", { ...props, ...storeProps });
|
|
246
|
+
}
|
|
247
|
+
function Textarea({
|
|
248
|
+
store,
|
|
249
|
+
getter,
|
|
250
|
+
setter,
|
|
251
|
+
...props
|
|
252
|
+
}) {
|
|
253
|
+
const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
|
|
254
|
+
return /* @__PURE__ */ import_react5.default.createElement("textarea", { ...props, ...storeProps });
|
|
255
|
+
}
|
|
256
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
257
|
+
0 && (module.exports = {
|
|
258
|
+
Input,
|
|
259
|
+
Select,
|
|
260
|
+
Textarea,
|
|
261
|
+
useStoreComponent
|
|
262
|
+
});
|