react-store-input 0.1.5 → 0.1.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/index.d.mts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +57 -118
- package/dist/index.mjs +58 -119
- package/dist/use_form_store.d.mts +2 -0
- package/dist/use_form_store.d.ts +2 -0
- package/dist/use_form_store.js +57 -27
- package/dist/use_form_store.mjs +58 -30
- package/dist/use_store_component.d.mts +11 -14
- package/dist/use_store_component.d.ts +11 -14
- package/dist/use_store_component.js +57 -27
- package/dist/use_store_component.mjs +58 -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 +2 -2
- package/dist/use_store_controller.mjs +2 -2
- package/dist/use_store_input.d.mts +6 -4
- package/dist/use_store_input.d.ts +6 -4
- package/dist/use_store_input.js +12 -15
- package/dist/use_store_input.mjs +12 -15
- package/dist/use_store_input_with_name.d.mts +5 -4
- package/dist/use_store_input_with_name.d.ts +5 -4
- package/dist/use_store_input_with_name.js +12 -15
- package/dist/use_store_input_with_name.mjs +12 -15
- package/package.json +1 -1
- package/dist/serialize.d.mts +0 -4
- package/dist/serialize.d.ts +0 -4
- package/dist/serialize.js +0 -116
- package/dist/serialize.mjs +0 -90
package/dist/use_form_store.js
CHANGED
|
@@ -139,7 +139,7 @@ function useStoreController(store, props) {
|
|
|
139
139
|
if (!ref.current) return;
|
|
140
140
|
props.onSubscribe(state, ref.current);
|
|
141
141
|
});
|
|
142
|
-
const
|
|
142
|
+
const dispatch = () => {
|
|
143
143
|
store.dispatch(
|
|
144
144
|
(state) => {
|
|
145
145
|
const element = ref.current;
|
|
@@ -153,26 +153,24 @@ function useStoreController(store, props) {
|
|
|
153
153
|
};
|
|
154
154
|
return {
|
|
155
155
|
ref,
|
|
156
|
-
|
|
156
|
+
dispatch
|
|
157
157
|
};
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// src/use_store_input.tsx
|
|
161
161
|
function useStoreInput(store, props) {
|
|
162
|
-
const toInputValue = (value) => {
|
|
162
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
163
163
|
if (value === void 0 || value === null) {
|
|
164
164
|
return "";
|
|
165
165
|
}
|
|
166
166
|
if (props.type === "datetime-local") {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (typeof value === "string") {
|
|
171
|
-
return toInputValue(new Date(value));
|
|
167
|
+
const date = new Date(value);
|
|
168
|
+
if (!isNaN(date.getTime())) {
|
|
169
|
+
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
172
170
|
}
|
|
173
171
|
}
|
|
174
172
|
return String(value);
|
|
175
|
-
};
|
|
173
|
+
});
|
|
176
174
|
const getDefaultValue = () => {
|
|
177
175
|
if (props.defaultValue !== void 0) {
|
|
178
176
|
return props.defaultValue;
|
|
@@ -197,16 +195,15 @@ function useStoreInput(store, props) {
|
|
|
197
195
|
}
|
|
198
196
|
return toInputChecked(props.getter(store.state));
|
|
199
197
|
};
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (typeof selected === "number") {
|
|
198
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
199
|
+
if (props.type === "number" || props.type === "range") {
|
|
203
200
|
return Number(value);
|
|
204
201
|
}
|
|
205
|
-
if (
|
|
202
|
+
if (props.type === "datetime-local") {
|
|
206
203
|
return new Date(value);
|
|
207
204
|
}
|
|
208
205
|
return value;
|
|
209
|
-
}
|
|
206
|
+
});
|
|
210
207
|
const inputProps = useStoreController(store, {
|
|
211
208
|
ref: props.ref,
|
|
212
209
|
onSubscribe: (state, element) => {
|
|
@@ -239,7 +236,7 @@ function useStoreInput(store, props) {
|
|
|
239
236
|
defaultValue: getDefaultValue(),
|
|
240
237
|
defaultChecked: getDefaultChecked(),
|
|
241
238
|
onChange: (event) => {
|
|
242
|
-
inputProps.
|
|
239
|
+
inputProps.dispatch();
|
|
243
240
|
props.onChange?.(event);
|
|
244
241
|
}
|
|
245
242
|
};
|
|
@@ -271,15 +268,24 @@ function useStoreInputWithName(store, props) {
|
|
|
271
268
|
|
|
272
269
|
// src/use_store_component.tsx
|
|
273
270
|
function useStoreComponent(store) {
|
|
274
|
-
const input = (0, import_react7.useCallback)(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
271
|
+
const input = (0, import_react7.useCallback)(
|
|
272
|
+
function Component(props) {
|
|
273
|
+
return /* @__PURE__ */ import_react8.default.createElement(Input, { store, ...props });
|
|
274
|
+
},
|
|
275
|
+
[]
|
|
276
|
+
);
|
|
277
|
+
const select = (0, import_react7.useCallback)(
|
|
278
|
+
function Component(props) {
|
|
279
|
+
return /* @__PURE__ */ import_react8.default.createElement(Select, { store, ...props });
|
|
280
|
+
},
|
|
281
|
+
[]
|
|
282
|
+
);
|
|
283
|
+
const textarea = (0, import_react7.useCallback)(
|
|
284
|
+
function Component(props) {
|
|
285
|
+
return /* @__PURE__ */ import_react8.default.createElement(Textarea, { store, ...props });
|
|
286
|
+
},
|
|
287
|
+
[]
|
|
288
|
+
);
|
|
283
289
|
return {
|
|
284
290
|
input,
|
|
285
291
|
select,
|
|
@@ -290,27 +296,51 @@ function Input({
|
|
|
290
296
|
store,
|
|
291
297
|
getter,
|
|
292
298
|
setter,
|
|
299
|
+
toInputValue,
|
|
300
|
+
toStateValue,
|
|
293
301
|
...props
|
|
294
302
|
}) {
|
|
295
|
-
const storeProps = useStoreInputWithName(store, {
|
|
303
|
+
const storeProps = useStoreInputWithName(store, {
|
|
304
|
+
...props,
|
|
305
|
+
getter,
|
|
306
|
+
setter,
|
|
307
|
+
toInputValue,
|
|
308
|
+
toStateValue
|
|
309
|
+
});
|
|
296
310
|
return /* @__PURE__ */ import_react8.default.createElement("input", { ...props, ...storeProps });
|
|
297
311
|
}
|
|
298
312
|
function Select({
|
|
299
313
|
store,
|
|
300
314
|
getter,
|
|
301
315
|
setter,
|
|
316
|
+
toInputValue,
|
|
317
|
+
toStateValue,
|
|
302
318
|
...props
|
|
303
319
|
}) {
|
|
304
|
-
const storeProps = useStoreInputWithName(store, {
|
|
320
|
+
const storeProps = useStoreInputWithName(store, {
|
|
321
|
+
...props,
|
|
322
|
+
getter,
|
|
323
|
+
setter,
|
|
324
|
+
toInputValue,
|
|
325
|
+
toStateValue
|
|
326
|
+
});
|
|
305
327
|
return /* @__PURE__ */ import_react8.default.createElement("select", { ...props, ...storeProps });
|
|
306
328
|
}
|
|
307
329
|
function Textarea({
|
|
308
330
|
store,
|
|
309
331
|
getter,
|
|
310
332
|
setter,
|
|
333
|
+
toInputValue,
|
|
334
|
+
toStateValue,
|
|
311
335
|
...props
|
|
312
336
|
}) {
|
|
313
|
-
const storeProps = useStoreInputWithName(store, {
|
|
337
|
+
const storeProps = useStoreInputWithName(store, {
|
|
338
|
+
...props,
|
|
339
|
+
getter,
|
|
340
|
+
setter,
|
|
341
|
+
toInputValue,
|
|
342
|
+
toStateValue
|
|
343
|
+
});
|
|
314
344
|
return /* @__PURE__ */ import_react8.default.createElement("textarea", { ...props, ...storeProps });
|
|
315
345
|
}
|
|
316
346
|
|
package/dist/use_form_store.mjs
CHANGED
|
@@ -83,9 +83,7 @@ function useStore(initialState) {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// src/use_store_component.tsx
|
|
86
|
-
import {
|
|
87
|
-
useCallback
|
|
88
|
-
} from "react";
|
|
86
|
+
import { useCallback } from "react";
|
|
89
87
|
import React2 from "react";
|
|
90
88
|
|
|
91
89
|
// src/use_store_input.tsx
|
|
@@ -105,7 +103,7 @@ function useStoreController(store, props) {
|
|
|
105
103
|
if (!ref.current) return;
|
|
106
104
|
props.onSubscribe(state, ref.current);
|
|
107
105
|
});
|
|
108
|
-
const
|
|
106
|
+
const dispatch = () => {
|
|
109
107
|
store.dispatch(
|
|
110
108
|
(state) => {
|
|
111
109
|
const element = ref.current;
|
|
@@ -119,26 +117,24 @@ function useStoreController(store, props) {
|
|
|
119
117
|
};
|
|
120
118
|
return {
|
|
121
119
|
ref,
|
|
122
|
-
|
|
120
|
+
dispatch
|
|
123
121
|
};
|
|
124
122
|
}
|
|
125
123
|
|
|
126
124
|
// src/use_store_input.tsx
|
|
127
125
|
function useStoreInput(store, props) {
|
|
128
|
-
const toInputValue = (value) => {
|
|
126
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
129
127
|
if (value === void 0 || value === null) {
|
|
130
128
|
return "";
|
|
131
129
|
}
|
|
132
130
|
if (props.type === "datetime-local") {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (typeof value === "string") {
|
|
137
|
-
return toInputValue(new Date(value));
|
|
131
|
+
const date = new Date(value);
|
|
132
|
+
if (!isNaN(date.getTime())) {
|
|
133
|
+
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
138
134
|
}
|
|
139
135
|
}
|
|
140
136
|
return String(value);
|
|
141
|
-
};
|
|
137
|
+
});
|
|
142
138
|
const getDefaultValue = () => {
|
|
143
139
|
if (props.defaultValue !== void 0) {
|
|
144
140
|
return props.defaultValue;
|
|
@@ -163,16 +159,15 @@ function useStoreInput(store, props) {
|
|
|
163
159
|
}
|
|
164
160
|
return toInputChecked(props.getter(store.state));
|
|
165
161
|
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (typeof selected === "number") {
|
|
162
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
163
|
+
if (props.type === "number" || props.type === "range") {
|
|
169
164
|
return Number(value);
|
|
170
165
|
}
|
|
171
|
-
if (
|
|
166
|
+
if (props.type === "datetime-local") {
|
|
172
167
|
return new Date(value);
|
|
173
168
|
}
|
|
174
169
|
return value;
|
|
175
|
-
}
|
|
170
|
+
});
|
|
176
171
|
const inputProps = useStoreController(store, {
|
|
177
172
|
ref: props.ref,
|
|
178
173
|
onSubscribe: (state, element) => {
|
|
@@ -205,7 +200,7 @@ function useStoreInput(store, props) {
|
|
|
205
200
|
defaultValue: getDefaultValue(),
|
|
206
201
|
defaultChecked: getDefaultChecked(),
|
|
207
202
|
onChange: (event) => {
|
|
208
|
-
inputProps.
|
|
203
|
+
inputProps.dispatch();
|
|
209
204
|
props.onChange?.(event);
|
|
210
205
|
}
|
|
211
206
|
};
|
|
@@ -237,15 +232,24 @@ function useStoreInputWithName(store, props) {
|
|
|
237
232
|
|
|
238
233
|
// src/use_store_component.tsx
|
|
239
234
|
function useStoreComponent(store) {
|
|
240
|
-
const input = useCallback(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
235
|
+
const input = useCallback(
|
|
236
|
+
function Component(props) {
|
|
237
|
+
return /* @__PURE__ */ React2.createElement(Input, { store, ...props });
|
|
238
|
+
},
|
|
239
|
+
[]
|
|
240
|
+
);
|
|
241
|
+
const select = useCallback(
|
|
242
|
+
function Component(props) {
|
|
243
|
+
return /* @__PURE__ */ React2.createElement(Select, { store, ...props });
|
|
244
|
+
},
|
|
245
|
+
[]
|
|
246
|
+
);
|
|
247
|
+
const textarea = useCallback(
|
|
248
|
+
function Component(props) {
|
|
249
|
+
return /* @__PURE__ */ React2.createElement(Textarea, { store, ...props });
|
|
250
|
+
},
|
|
251
|
+
[]
|
|
252
|
+
);
|
|
249
253
|
return {
|
|
250
254
|
input,
|
|
251
255
|
select,
|
|
@@ -256,27 +260,51 @@ function Input({
|
|
|
256
260
|
store,
|
|
257
261
|
getter,
|
|
258
262
|
setter,
|
|
263
|
+
toInputValue,
|
|
264
|
+
toStateValue,
|
|
259
265
|
...props
|
|
260
266
|
}) {
|
|
261
|
-
const storeProps = useStoreInputWithName(store, {
|
|
267
|
+
const storeProps = useStoreInputWithName(store, {
|
|
268
|
+
...props,
|
|
269
|
+
getter,
|
|
270
|
+
setter,
|
|
271
|
+
toInputValue,
|
|
272
|
+
toStateValue
|
|
273
|
+
});
|
|
262
274
|
return /* @__PURE__ */ React2.createElement("input", { ...props, ...storeProps });
|
|
263
275
|
}
|
|
264
276
|
function Select({
|
|
265
277
|
store,
|
|
266
278
|
getter,
|
|
267
279
|
setter,
|
|
280
|
+
toInputValue,
|
|
281
|
+
toStateValue,
|
|
268
282
|
...props
|
|
269
283
|
}) {
|
|
270
|
-
const storeProps = useStoreInputWithName(store, {
|
|
284
|
+
const storeProps = useStoreInputWithName(store, {
|
|
285
|
+
...props,
|
|
286
|
+
getter,
|
|
287
|
+
setter,
|
|
288
|
+
toInputValue,
|
|
289
|
+
toStateValue
|
|
290
|
+
});
|
|
271
291
|
return /* @__PURE__ */ React2.createElement("select", { ...props, ...storeProps });
|
|
272
292
|
}
|
|
273
293
|
function Textarea({
|
|
274
294
|
store,
|
|
275
295
|
getter,
|
|
276
296
|
setter,
|
|
297
|
+
toInputValue,
|
|
298
|
+
toStateValue,
|
|
277
299
|
...props
|
|
278
300
|
}) {
|
|
279
|
-
const storeProps = useStoreInputWithName(store, {
|
|
301
|
+
const storeProps = useStoreInputWithName(store, {
|
|
302
|
+
...props,
|
|
303
|
+
getter,
|
|
304
|
+
setter,
|
|
305
|
+
toInputValue,
|
|
306
|
+
toStateValue
|
|
307
|
+
});
|
|
280
308
|
return /* @__PURE__ */ React2.createElement("textarea", { ...props, ...storeProps });
|
|
281
309
|
}
|
|
282
310
|
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
import React__default, { DetailedHTMLProps, InputHTMLAttributes } from 'react';
|
|
2
1
|
import { Store } from './use_store.mjs';
|
|
2
|
+
import React__default from 'react';
|
|
3
|
+
import { StoreInputWithNameProps } from './use_store_input_with_name.mjs';
|
|
4
|
+
import './use_store_input.mjs';
|
|
3
5
|
|
|
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
6
|
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
10
|
-
input:
|
|
11
|
-
select:
|
|
12
|
-
textarea:
|
|
7
|
+
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLInputElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
8
|
+
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLSelectElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
9
|
+
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLTextAreaElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
13
10
|
};
|
|
14
|
-
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> =
|
|
11
|
+
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & {
|
|
15
12
|
store: Store<TState>;
|
|
16
13
|
};
|
|
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;
|
|
14
|
+
declare function Input<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
15
|
+
declare function Select<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
16
|
+
declare function Textarea<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
20
17
|
|
|
21
|
-
export { Input, Select, type
|
|
18
|
+
export { Input, Select, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
import React__default, { DetailedHTMLProps, InputHTMLAttributes } from 'react';
|
|
2
1
|
import { Store } from './use_store.js';
|
|
2
|
+
import React__default from 'react';
|
|
3
|
+
import { StoreInputWithNameProps } from './use_store_input_with_name.js';
|
|
4
|
+
import './use_store_input.js';
|
|
3
5
|
|
|
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
6
|
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
10
|
-
input:
|
|
11
|
-
select:
|
|
12
|
-
textarea:
|
|
7
|
+
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLInputElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
8
|
+
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLSelectElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
9
|
+
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameProps<HTMLTextAreaElement, TState, TName, TValue>) => React__default.JSX.Element;
|
|
13
10
|
};
|
|
14
|
-
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> =
|
|
11
|
+
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & {
|
|
15
12
|
store: Store<TState>;
|
|
16
13
|
};
|
|
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;
|
|
14
|
+
declare function Input<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
15
|
+
declare function Select<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
16
|
+
declare function Textarea<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState, TName, TValue>): React__default.JSX.Element;
|
|
20
17
|
|
|
21
|
-
export { Input, Select, type
|
|
18
|
+
export { Input, Select, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
|
|
@@ -69,7 +69,7 @@ function useStoreController(store, props) {
|
|
|
69
69
|
if (!ref.current) return;
|
|
70
70
|
props.onSubscribe(state, ref.current);
|
|
71
71
|
});
|
|
72
|
-
const
|
|
72
|
+
const dispatch = () => {
|
|
73
73
|
store.dispatch(
|
|
74
74
|
(state) => {
|
|
75
75
|
const element = ref.current;
|
|
@@ -83,26 +83,24 @@ function useStoreController(store, props) {
|
|
|
83
83
|
};
|
|
84
84
|
return {
|
|
85
85
|
ref,
|
|
86
|
-
|
|
86
|
+
dispatch
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
// src/use_store_input.tsx
|
|
91
91
|
function useStoreInput(store, props) {
|
|
92
|
-
const toInputValue = (value) => {
|
|
92
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
93
93
|
if (value === void 0 || value === null) {
|
|
94
94
|
return "";
|
|
95
95
|
}
|
|
96
96
|
if (props.type === "datetime-local") {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (typeof value === "string") {
|
|
101
|
-
return toInputValue(new Date(value));
|
|
97
|
+
const date = new Date(value);
|
|
98
|
+
if (!isNaN(date.getTime())) {
|
|
99
|
+
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
102
100
|
}
|
|
103
101
|
}
|
|
104
102
|
return String(value);
|
|
105
|
-
};
|
|
103
|
+
});
|
|
106
104
|
const getDefaultValue = () => {
|
|
107
105
|
if (props.defaultValue !== void 0) {
|
|
108
106
|
return props.defaultValue;
|
|
@@ -127,16 +125,15 @@ function useStoreInput(store, props) {
|
|
|
127
125
|
}
|
|
128
126
|
return toInputChecked(props.getter(store.state));
|
|
129
127
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (typeof selected === "number") {
|
|
128
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
129
|
+
if (props.type === "number" || props.type === "range") {
|
|
133
130
|
return Number(value);
|
|
134
131
|
}
|
|
135
|
-
if (
|
|
132
|
+
if (props.type === "datetime-local") {
|
|
136
133
|
return new Date(value);
|
|
137
134
|
}
|
|
138
135
|
return value;
|
|
139
|
-
}
|
|
136
|
+
});
|
|
140
137
|
const inputProps = useStoreController(store, {
|
|
141
138
|
ref: props.ref,
|
|
142
139
|
onSubscribe: (state, element) => {
|
|
@@ -169,7 +166,7 @@ function useStoreInput(store, props) {
|
|
|
169
166
|
defaultValue: getDefaultValue(),
|
|
170
167
|
defaultChecked: getDefaultChecked(),
|
|
171
168
|
onChange: (event) => {
|
|
172
|
-
inputProps.
|
|
169
|
+
inputProps.dispatch();
|
|
173
170
|
props.onChange?.(event);
|
|
174
171
|
}
|
|
175
172
|
};
|
|
@@ -201,15 +198,24 @@ function useStoreInputWithName(store, props) {
|
|
|
201
198
|
|
|
202
199
|
// src/use_store_component.tsx
|
|
203
200
|
function useStoreComponent(store) {
|
|
204
|
-
const input = (0, import_react4.useCallback)(
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
201
|
+
const input = (0, import_react4.useCallback)(
|
|
202
|
+
function Component(props) {
|
|
203
|
+
return /* @__PURE__ */ import_react5.default.createElement(Input, { store, ...props });
|
|
204
|
+
},
|
|
205
|
+
[]
|
|
206
|
+
);
|
|
207
|
+
const select = (0, import_react4.useCallback)(
|
|
208
|
+
function Component(props) {
|
|
209
|
+
return /* @__PURE__ */ import_react5.default.createElement(Select, { store, ...props });
|
|
210
|
+
},
|
|
211
|
+
[]
|
|
212
|
+
);
|
|
213
|
+
const textarea = (0, import_react4.useCallback)(
|
|
214
|
+
function Component(props) {
|
|
215
|
+
return /* @__PURE__ */ import_react5.default.createElement(Textarea, { store, ...props });
|
|
216
|
+
},
|
|
217
|
+
[]
|
|
218
|
+
);
|
|
213
219
|
return {
|
|
214
220
|
input,
|
|
215
221
|
select,
|
|
@@ -220,27 +226,51 @@ function Input({
|
|
|
220
226
|
store,
|
|
221
227
|
getter,
|
|
222
228
|
setter,
|
|
229
|
+
toInputValue,
|
|
230
|
+
toStateValue,
|
|
223
231
|
...props
|
|
224
232
|
}) {
|
|
225
|
-
const storeProps = useStoreInputWithName(store, {
|
|
233
|
+
const storeProps = useStoreInputWithName(store, {
|
|
234
|
+
...props,
|
|
235
|
+
getter,
|
|
236
|
+
setter,
|
|
237
|
+
toInputValue,
|
|
238
|
+
toStateValue
|
|
239
|
+
});
|
|
226
240
|
return /* @__PURE__ */ import_react5.default.createElement("input", { ...props, ...storeProps });
|
|
227
241
|
}
|
|
228
242
|
function Select({
|
|
229
243
|
store,
|
|
230
244
|
getter,
|
|
231
245
|
setter,
|
|
246
|
+
toInputValue,
|
|
247
|
+
toStateValue,
|
|
232
248
|
...props
|
|
233
249
|
}) {
|
|
234
|
-
const storeProps = useStoreInputWithName(store, {
|
|
250
|
+
const storeProps = useStoreInputWithName(store, {
|
|
251
|
+
...props,
|
|
252
|
+
getter,
|
|
253
|
+
setter,
|
|
254
|
+
toInputValue,
|
|
255
|
+
toStateValue
|
|
256
|
+
});
|
|
235
257
|
return /* @__PURE__ */ import_react5.default.createElement("select", { ...props, ...storeProps });
|
|
236
258
|
}
|
|
237
259
|
function Textarea({
|
|
238
260
|
store,
|
|
239
261
|
getter,
|
|
240
262
|
setter,
|
|
263
|
+
toInputValue,
|
|
264
|
+
toStateValue,
|
|
241
265
|
...props
|
|
242
266
|
}) {
|
|
243
|
-
const storeProps = useStoreInputWithName(store, {
|
|
267
|
+
const storeProps = useStoreInputWithName(store, {
|
|
268
|
+
...props,
|
|
269
|
+
getter,
|
|
270
|
+
setter,
|
|
271
|
+
toInputValue,
|
|
272
|
+
toStateValue
|
|
273
|
+
});
|
|
244
274
|
return /* @__PURE__ */ import_react5.default.createElement("textarea", { ...props, ...storeProps });
|
|
245
275
|
}
|
|
246
276
|
// Annotate the CommonJS export names for ESM import in node:
|