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
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
// src/use_store_component.tsx
|
|
2
|
-
import {
|
|
3
|
-
useCallback
|
|
4
|
-
} from "react";
|
|
2
|
+
import { useCallback } from "react";
|
|
5
3
|
import React from "react";
|
|
6
4
|
|
|
7
5
|
// src/use_store_input.tsx
|
|
@@ -34,7 +32,7 @@ function useStoreController(store, props) {
|
|
|
34
32
|
if (!ref.current) return;
|
|
35
33
|
props.onSubscribe(state, ref.current);
|
|
36
34
|
});
|
|
37
|
-
const
|
|
35
|
+
const dispatch = () => {
|
|
38
36
|
store.dispatch(
|
|
39
37
|
(state) => {
|
|
40
38
|
const element = ref.current;
|
|
@@ -48,26 +46,24 @@ function useStoreController(store, props) {
|
|
|
48
46
|
};
|
|
49
47
|
return {
|
|
50
48
|
ref,
|
|
51
|
-
|
|
49
|
+
dispatch
|
|
52
50
|
};
|
|
53
51
|
}
|
|
54
52
|
|
|
55
53
|
// src/use_store_input.tsx
|
|
56
54
|
function useStoreInput(store, props) {
|
|
57
|
-
const toInputValue = (value) => {
|
|
55
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
58
56
|
if (value === void 0 || value === null) {
|
|
59
57
|
return "";
|
|
60
58
|
}
|
|
61
59
|
if (props.type === "datetime-local") {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (typeof value === "string") {
|
|
66
|
-
return toInputValue(new Date(value));
|
|
60
|
+
const date = new Date(value);
|
|
61
|
+
if (!isNaN(date.getTime())) {
|
|
62
|
+
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
67
63
|
}
|
|
68
64
|
}
|
|
69
65
|
return String(value);
|
|
70
|
-
};
|
|
66
|
+
});
|
|
71
67
|
const getDefaultValue = () => {
|
|
72
68
|
if (props.defaultValue !== void 0) {
|
|
73
69
|
return props.defaultValue;
|
|
@@ -92,16 +88,15 @@ function useStoreInput(store, props) {
|
|
|
92
88
|
}
|
|
93
89
|
return toInputChecked(props.getter(store.state));
|
|
94
90
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (typeof selected === "number") {
|
|
91
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
92
|
+
if (props.type === "number" || props.type === "range") {
|
|
98
93
|
return Number(value);
|
|
99
94
|
}
|
|
100
|
-
if (
|
|
95
|
+
if (props.type === "datetime-local") {
|
|
101
96
|
return new Date(value);
|
|
102
97
|
}
|
|
103
98
|
return value;
|
|
104
|
-
}
|
|
99
|
+
});
|
|
105
100
|
const inputProps = useStoreController(store, {
|
|
106
101
|
ref: props.ref,
|
|
107
102
|
onSubscribe: (state, element) => {
|
|
@@ -134,7 +129,7 @@ function useStoreInput(store, props) {
|
|
|
134
129
|
defaultValue: getDefaultValue(),
|
|
135
130
|
defaultChecked: getDefaultChecked(),
|
|
136
131
|
onChange: (event) => {
|
|
137
|
-
inputProps.
|
|
132
|
+
inputProps.dispatch();
|
|
138
133
|
props.onChange?.(event);
|
|
139
134
|
}
|
|
140
135
|
};
|
|
@@ -166,15 +161,24 @@ function useStoreInputWithName(store, props) {
|
|
|
166
161
|
|
|
167
162
|
// src/use_store_component.tsx
|
|
168
163
|
function useStoreComponent(store) {
|
|
169
|
-
const input = useCallback(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
164
|
+
const input = useCallback(
|
|
165
|
+
function Component(props) {
|
|
166
|
+
return /* @__PURE__ */ React.createElement(Input, { store, ...props });
|
|
167
|
+
},
|
|
168
|
+
[]
|
|
169
|
+
);
|
|
170
|
+
const select = useCallback(
|
|
171
|
+
function Component(props) {
|
|
172
|
+
return /* @__PURE__ */ React.createElement(Select, { store, ...props });
|
|
173
|
+
},
|
|
174
|
+
[]
|
|
175
|
+
);
|
|
176
|
+
const textarea = useCallback(
|
|
177
|
+
function Component(props) {
|
|
178
|
+
return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
|
|
179
|
+
},
|
|
180
|
+
[]
|
|
181
|
+
);
|
|
178
182
|
return {
|
|
179
183
|
input,
|
|
180
184
|
select,
|
|
@@ -185,27 +189,51 @@ function Input({
|
|
|
185
189
|
store,
|
|
186
190
|
getter,
|
|
187
191
|
setter,
|
|
192
|
+
toInputValue,
|
|
193
|
+
toStateValue,
|
|
188
194
|
...props
|
|
189
195
|
}) {
|
|
190
|
-
const storeProps = useStoreInputWithName(store, {
|
|
196
|
+
const storeProps = useStoreInputWithName(store, {
|
|
197
|
+
...props,
|
|
198
|
+
getter,
|
|
199
|
+
setter,
|
|
200
|
+
toInputValue,
|
|
201
|
+
toStateValue
|
|
202
|
+
});
|
|
191
203
|
return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
|
|
192
204
|
}
|
|
193
205
|
function Select({
|
|
194
206
|
store,
|
|
195
207
|
getter,
|
|
196
208
|
setter,
|
|
209
|
+
toInputValue,
|
|
210
|
+
toStateValue,
|
|
197
211
|
...props
|
|
198
212
|
}) {
|
|
199
|
-
const storeProps = useStoreInputWithName(store, {
|
|
213
|
+
const storeProps = useStoreInputWithName(store, {
|
|
214
|
+
...props,
|
|
215
|
+
getter,
|
|
216
|
+
setter,
|
|
217
|
+
toInputValue,
|
|
218
|
+
toStateValue
|
|
219
|
+
});
|
|
200
220
|
return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
|
|
201
221
|
}
|
|
202
222
|
function Textarea({
|
|
203
223
|
store,
|
|
204
224
|
getter,
|
|
205
225
|
setter,
|
|
226
|
+
toInputValue,
|
|
227
|
+
toStateValue,
|
|
206
228
|
...props
|
|
207
229
|
}) {
|
|
208
|
-
const storeProps = useStoreInputWithName(store, {
|
|
230
|
+
const storeProps = useStoreInputWithName(store, {
|
|
231
|
+
...props,
|
|
232
|
+
getter,
|
|
233
|
+
setter,
|
|
234
|
+
toInputValue,
|
|
235
|
+
toStateValue
|
|
236
|
+
});
|
|
209
237
|
return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
|
|
210
238
|
}
|
|
211
239
|
export {
|
|
@@ -9,7 +9,7 @@ type StoreControllerProps<TController, TState> = {
|
|
|
9
9
|
};
|
|
10
10
|
declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
|
|
11
11
|
ref: React.RefObject<TController | null>;
|
|
12
|
-
|
|
12
|
+
dispatch: () => void;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export { type StoreControllerProps, useStoreController };
|
|
@@ -9,7 +9,7 @@ type StoreControllerProps<TController, TState> = {
|
|
|
9
9
|
};
|
|
10
10
|
declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
|
|
11
11
|
ref: React.RefObject<TController | null>;
|
|
12
|
-
|
|
12
|
+
dispatch: () => void;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export { type StoreControllerProps, useStoreController };
|
|
@@ -48,7 +48,7 @@ function useStoreController(store, props) {
|
|
|
48
48
|
if (!ref.current) return;
|
|
49
49
|
props.onSubscribe(state, ref.current);
|
|
50
50
|
});
|
|
51
|
-
const
|
|
51
|
+
const dispatch = () => {
|
|
52
52
|
store.dispatch(
|
|
53
53
|
(state) => {
|
|
54
54
|
const element = ref.current;
|
|
@@ -62,7 +62,7 @@ function useStoreController(store, props) {
|
|
|
62
62
|
};
|
|
63
63
|
return {
|
|
64
64
|
ref,
|
|
65
|
-
|
|
65
|
+
dispatch
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -24,7 +24,7 @@ function useStoreController(store, props) {
|
|
|
24
24
|
if (!ref.current) return;
|
|
25
25
|
props.onSubscribe(state, ref.current);
|
|
26
26
|
});
|
|
27
|
-
const
|
|
27
|
+
const dispatch = () => {
|
|
28
28
|
store.dispatch(
|
|
29
29
|
(state) => {
|
|
30
30
|
const element = ref.current;
|
|
@@ -38,7 +38,7 @@ function useStoreController(store, props) {
|
|
|
38
38
|
};
|
|
39
39
|
return {
|
|
40
40
|
ref,
|
|
41
|
-
|
|
41
|
+
dispatch
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
export {
|
|
@@ -2,7 +2,7 @@ import * as React$1 from 'react';
|
|
|
2
2
|
import { Ref, HTMLInputTypeAttribute } from 'react';
|
|
3
3
|
import { Store } from './use_store.mjs';
|
|
4
4
|
|
|
5
|
-
type StoreInputProps<TInputElement, TState> = {
|
|
5
|
+
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
6
6
|
ref?: Ref<TInputElement>;
|
|
7
7
|
type?: HTMLInputTypeAttribute;
|
|
8
8
|
defaultValue?: string | number | readonly string[] | undefined;
|
|
@@ -10,10 +10,12 @@ type StoreInputProps<TInputElement, TState> = {
|
|
|
10
10
|
defaultChecked?: boolean | undefined;
|
|
11
11
|
onChange?: (event: React.ChangeEvent<TInputElement>) => void;
|
|
12
12
|
} & {
|
|
13
|
-
getter: (state: TState) =>
|
|
14
|
-
setter: (state: TState, value:
|
|
13
|
+
getter: (state: TState) => TValue;
|
|
14
|
+
setter: (state: TState, value: TValue) => void;
|
|
15
|
+
toInputValue?: (value: TValue) => string;
|
|
16
|
+
toStateValue?: (value: string) => TValue;
|
|
15
17
|
};
|
|
16
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: StoreInputProps<TInputElement, TState>): {
|
|
18
|
+
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
17
19
|
ref: React$1.RefObject<TInputElement | null>;
|
|
18
20
|
defaultValue: string | number | readonly string[] | undefined;
|
|
19
21
|
defaultChecked: boolean | undefined;
|
|
@@ -2,7 +2,7 @@ import * as React$1 from 'react';
|
|
|
2
2
|
import { Ref, HTMLInputTypeAttribute } from 'react';
|
|
3
3
|
import { Store } from './use_store.js';
|
|
4
4
|
|
|
5
|
-
type StoreInputProps<TInputElement, TState> = {
|
|
5
|
+
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
6
6
|
ref?: Ref<TInputElement>;
|
|
7
7
|
type?: HTMLInputTypeAttribute;
|
|
8
8
|
defaultValue?: string | number | readonly string[] | undefined;
|
|
@@ -10,10 +10,12 @@ type StoreInputProps<TInputElement, TState> = {
|
|
|
10
10
|
defaultChecked?: boolean | undefined;
|
|
11
11
|
onChange?: (event: React.ChangeEvent<TInputElement>) => void;
|
|
12
12
|
} & {
|
|
13
|
-
getter: (state: TState) =>
|
|
14
|
-
setter: (state: TState, value:
|
|
13
|
+
getter: (state: TState) => TValue;
|
|
14
|
+
setter: (state: TState, value: TValue) => void;
|
|
15
|
+
toInputValue?: (value: TValue) => string;
|
|
16
|
+
toStateValue?: (value: string) => TValue;
|
|
15
17
|
};
|
|
16
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: StoreInputProps<TInputElement, TState>): {
|
|
18
|
+
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
17
19
|
ref: React$1.RefObject<TInputElement | null>;
|
|
18
20
|
defaultValue: string | number | readonly string[] | undefined;
|
|
19
21
|
defaultChecked: boolean | undefined;
|
package/dist/use_store_input.js
CHANGED
|
@@ -52,7 +52,7 @@ function useStoreController(store, props) {
|
|
|
52
52
|
if (!ref.current) return;
|
|
53
53
|
props.onSubscribe(state, ref.current);
|
|
54
54
|
});
|
|
55
|
-
const
|
|
55
|
+
const dispatch = () => {
|
|
56
56
|
store.dispatch(
|
|
57
57
|
(state) => {
|
|
58
58
|
const element = ref.current;
|
|
@@ -66,26 +66,24 @@ function useStoreController(store, props) {
|
|
|
66
66
|
};
|
|
67
67
|
return {
|
|
68
68
|
ref,
|
|
69
|
-
|
|
69
|
+
dispatch
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
// src/use_store_input.tsx
|
|
74
74
|
function useStoreInput(store, props) {
|
|
75
|
-
const toInputValue = (value) => {
|
|
75
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
76
76
|
if (value === void 0 || value === null) {
|
|
77
77
|
return "";
|
|
78
78
|
}
|
|
79
79
|
if (props.type === "datetime-local") {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (typeof value === "string") {
|
|
84
|
-
return toInputValue(new Date(value));
|
|
80
|
+
const date = new Date(value);
|
|
81
|
+
if (!isNaN(date.getTime())) {
|
|
82
|
+
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
85
83
|
}
|
|
86
84
|
}
|
|
87
85
|
return String(value);
|
|
88
|
-
};
|
|
86
|
+
});
|
|
89
87
|
const getDefaultValue = () => {
|
|
90
88
|
if (props.defaultValue !== void 0) {
|
|
91
89
|
return props.defaultValue;
|
|
@@ -110,16 +108,15 @@ function useStoreInput(store, props) {
|
|
|
110
108
|
}
|
|
111
109
|
return toInputChecked(props.getter(store.state));
|
|
112
110
|
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (typeof selected === "number") {
|
|
111
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
112
|
+
if (props.type === "number" || props.type === "range") {
|
|
116
113
|
return Number(value);
|
|
117
114
|
}
|
|
118
|
-
if (
|
|
115
|
+
if (props.type === "datetime-local") {
|
|
119
116
|
return new Date(value);
|
|
120
117
|
}
|
|
121
118
|
return value;
|
|
122
|
-
}
|
|
119
|
+
});
|
|
123
120
|
const inputProps = useStoreController(store, {
|
|
124
121
|
ref: props.ref,
|
|
125
122
|
onSubscribe: (state, element) => {
|
|
@@ -152,7 +149,7 @@ function useStoreInput(store, props) {
|
|
|
152
149
|
defaultValue: getDefaultValue(),
|
|
153
150
|
defaultChecked: getDefaultChecked(),
|
|
154
151
|
onChange: (event) => {
|
|
155
|
-
inputProps.
|
|
152
|
+
inputProps.dispatch();
|
|
156
153
|
props.onChange?.(event);
|
|
157
154
|
}
|
|
158
155
|
};
|
package/dist/use_store_input.mjs
CHANGED
|
@@ -28,7 +28,7 @@ function useStoreController(store, props) {
|
|
|
28
28
|
if (!ref.current) return;
|
|
29
29
|
props.onSubscribe(state, ref.current);
|
|
30
30
|
});
|
|
31
|
-
const
|
|
31
|
+
const dispatch = () => {
|
|
32
32
|
store.dispatch(
|
|
33
33
|
(state) => {
|
|
34
34
|
const element = ref.current;
|
|
@@ -42,26 +42,24 @@ function useStoreController(store, props) {
|
|
|
42
42
|
};
|
|
43
43
|
return {
|
|
44
44
|
ref,
|
|
45
|
-
|
|
45
|
+
dispatch
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// src/use_store_input.tsx
|
|
50
50
|
function useStoreInput(store, props) {
|
|
51
|
-
const toInputValue = (value) => {
|
|
51
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
52
52
|
if (value === void 0 || value === null) {
|
|
53
53
|
return "";
|
|
54
54
|
}
|
|
55
55
|
if (props.type === "datetime-local") {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (typeof value === "string") {
|
|
60
|
-
return toInputValue(new Date(value));
|
|
56
|
+
const date = new Date(value);
|
|
57
|
+
if (!isNaN(date.getTime())) {
|
|
58
|
+
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
return String(value);
|
|
64
|
-
};
|
|
62
|
+
});
|
|
65
63
|
const getDefaultValue = () => {
|
|
66
64
|
if (props.defaultValue !== void 0) {
|
|
67
65
|
return props.defaultValue;
|
|
@@ -86,16 +84,15 @@ function useStoreInput(store, props) {
|
|
|
86
84
|
}
|
|
87
85
|
return toInputChecked(props.getter(store.state));
|
|
88
86
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (typeof selected === "number") {
|
|
87
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
88
|
+
if (props.type === "number" || props.type === "range") {
|
|
92
89
|
return Number(value);
|
|
93
90
|
}
|
|
94
|
-
if (
|
|
91
|
+
if (props.type === "datetime-local") {
|
|
95
92
|
return new Date(value);
|
|
96
93
|
}
|
|
97
94
|
return value;
|
|
98
|
-
}
|
|
95
|
+
});
|
|
99
96
|
const inputProps = useStoreController(store, {
|
|
100
97
|
ref: props.ref,
|
|
101
98
|
onSubscribe: (state, element) => {
|
|
@@ -128,7 +125,7 @@ function useStoreInput(store, props) {
|
|
|
128
125
|
defaultValue: getDefaultValue(),
|
|
129
126
|
defaultChecked: getDefaultChecked(),
|
|
130
127
|
onChange: (event) => {
|
|
131
|
-
inputProps.
|
|
128
|
+
inputProps.dispatch();
|
|
132
129
|
props.onChange?.(event);
|
|
133
130
|
}
|
|
134
131
|
};
|
|
@@ -2,10 +2,11 @@ import * as React from 'react';
|
|
|
2
2
|
import { Store } from './use_store.mjs';
|
|
3
3
|
import { StoreInputProps } from './use_store_input.mjs';
|
|
4
4
|
|
|
5
|
-
type
|
|
6
|
-
|
|
5
|
+
type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue> = undefined extends TName ? TValue : TName extends keyof TState ? TState[TName] : TValue;
|
|
6
|
+
type StoreInputWithNameProps<TInputElement, TState, TName extends keyof TState | undefined, TValue> = Omit<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter">> & {
|
|
7
|
+
name?: TName;
|
|
7
8
|
};
|
|
8
|
-
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props:
|
|
9
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
9
10
|
name: string | undefined;
|
|
10
11
|
ref: React.RefObject<TInputElement | null>;
|
|
11
12
|
defaultValue: string | number | readonly string[] | undefined;
|
|
@@ -13,4 +14,4 @@ declare function useStoreInputWithName<TInputElement extends HTMLInputElement |
|
|
|
13
14
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
export { useStoreInputWithName };
|
|
17
|
+
export { type InferNameFromProps, type StoreInputWithNameProps, useStoreInputWithName };
|
|
@@ -2,10 +2,11 @@ import * as React from 'react';
|
|
|
2
2
|
import { Store } from './use_store.js';
|
|
3
3
|
import { StoreInputProps } from './use_store_input.js';
|
|
4
4
|
|
|
5
|
-
type
|
|
6
|
-
|
|
5
|
+
type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue> = undefined extends TName ? TValue : TName extends keyof TState ? TState[TName] : TValue;
|
|
6
|
+
type StoreInputWithNameProps<TInputElement, TState, TName extends keyof TState | undefined, TValue> = Omit<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter">> & {
|
|
7
|
+
name?: TName;
|
|
7
8
|
};
|
|
8
|
-
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props:
|
|
9
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
9
10
|
name: string | undefined;
|
|
10
11
|
ref: React.RefObject<TInputElement | null>;
|
|
11
12
|
defaultValue: string | number | readonly string[] | undefined;
|
|
@@ -13,4 +14,4 @@ declare function useStoreInputWithName<TInputElement extends HTMLInputElement |
|
|
|
13
14
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
export { useStoreInputWithName };
|
|
17
|
+
export { type InferNameFromProps, type StoreInputWithNameProps, useStoreInputWithName };
|
|
@@ -54,7 +54,7 @@ function useStoreController(store, props) {
|
|
|
54
54
|
if (!ref.current) return;
|
|
55
55
|
props.onSubscribe(state, ref.current);
|
|
56
56
|
});
|
|
57
|
-
const
|
|
57
|
+
const dispatch = () => {
|
|
58
58
|
store.dispatch(
|
|
59
59
|
(state) => {
|
|
60
60
|
const element = ref.current;
|
|
@@ -68,26 +68,24 @@ function useStoreController(store, props) {
|
|
|
68
68
|
};
|
|
69
69
|
return {
|
|
70
70
|
ref,
|
|
71
|
-
|
|
71
|
+
dispatch
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
// src/use_store_input.tsx
|
|
76
76
|
function useStoreInput(store, props) {
|
|
77
|
-
const toInputValue = (value) => {
|
|
77
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
78
78
|
if (value === void 0 || value === null) {
|
|
79
79
|
return "";
|
|
80
80
|
}
|
|
81
81
|
if (props.type === "datetime-local") {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (typeof value === "string") {
|
|
86
|
-
return toInputValue(new Date(value));
|
|
82
|
+
const date = new Date(value);
|
|
83
|
+
if (!isNaN(date.getTime())) {
|
|
84
|
+
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
87
85
|
}
|
|
88
86
|
}
|
|
89
87
|
return String(value);
|
|
90
|
-
};
|
|
88
|
+
});
|
|
91
89
|
const getDefaultValue = () => {
|
|
92
90
|
if (props.defaultValue !== void 0) {
|
|
93
91
|
return props.defaultValue;
|
|
@@ -112,16 +110,15 @@ function useStoreInput(store, props) {
|
|
|
112
110
|
}
|
|
113
111
|
return toInputChecked(props.getter(store.state));
|
|
114
112
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (typeof selected === "number") {
|
|
113
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
114
|
+
if (props.type === "number" || props.type === "range") {
|
|
118
115
|
return Number(value);
|
|
119
116
|
}
|
|
120
|
-
if (
|
|
117
|
+
if (props.type === "datetime-local") {
|
|
121
118
|
return new Date(value);
|
|
122
119
|
}
|
|
123
120
|
return value;
|
|
124
|
-
}
|
|
121
|
+
});
|
|
125
122
|
const inputProps = useStoreController(store, {
|
|
126
123
|
ref: props.ref,
|
|
127
124
|
onSubscribe: (state, element) => {
|
|
@@ -154,7 +151,7 @@ function useStoreInput(store, props) {
|
|
|
154
151
|
defaultValue: getDefaultValue(),
|
|
155
152
|
defaultChecked: getDefaultChecked(),
|
|
156
153
|
onChange: (event) => {
|
|
157
|
-
inputProps.
|
|
154
|
+
inputProps.dispatch();
|
|
158
155
|
props.onChange?.(event);
|
|
159
156
|
}
|
|
160
157
|
};
|
|
@@ -28,7 +28,7 @@ function useStoreController(store, props) {
|
|
|
28
28
|
if (!ref.current) return;
|
|
29
29
|
props.onSubscribe(state, ref.current);
|
|
30
30
|
});
|
|
31
|
-
const
|
|
31
|
+
const dispatch = () => {
|
|
32
32
|
store.dispatch(
|
|
33
33
|
(state) => {
|
|
34
34
|
const element = ref.current;
|
|
@@ -42,26 +42,24 @@ function useStoreController(store, props) {
|
|
|
42
42
|
};
|
|
43
43
|
return {
|
|
44
44
|
ref,
|
|
45
|
-
|
|
45
|
+
dispatch
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
// src/use_store_input.tsx
|
|
50
50
|
function useStoreInput(store, props) {
|
|
51
|
-
const toInputValue = (value) => {
|
|
51
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
52
52
|
if (value === void 0 || value === null) {
|
|
53
53
|
return "";
|
|
54
54
|
}
|
|
55
55
|
if (props.type === "datetime-local") {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (typeof value === "string") {
|
|
60
|
-
return toInputValue(new Date(value));
|
|
56
|
+
const date = new Date(value);
|
|
57
|
+
if (!isNaN(date.getTime())) {
|
|
58
|
+
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
return String(value);
|
|
64
|
-
};
|
|
62
|
+
});
|
|
65
63
|
const getDefaultValue = () => {
|
|
66
64
|
if (props.defaultValue !== void 0) {
|
|
67
65
|
return props.defaultValue;
|
|
@@ -86,16 +84,15 @@ function useStoreInput(store, props) {
|
|
|
86
84
|
}
|
|
87
85
|
return toInputChecked(props.getter(store.state));
|
|
88
86
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (typeof selected === "number") {
|
|
87
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
88
|
+
if (props.type === "number" || props.type === "range") {
|
|
92
89
|
return Number(value);
|
|
93
90
|
}
|
|
94
|
-
if (
|
|
91
|
+
if (props.type === "datetime-local") {
|
|
95
92
|
return new Date(value);
|
|
96
93
|
}
|
|
97
94
|
return value;
|
|
98
|
-
}
|
|
95
|
+
});
|
|
99
96
|
const inputProps = useStoreController(store, {
|
|
100
97
|
ref: props.ref,
|
|
101
98
|
onSubscribe: (state, element) => {
|
|
@@ -128,7 +125,7 @@ function useStoreInput(store, props) {
|
|
|
128
125
|
defaultValue: getDefaultValue(),
|
|
129
126
|
defaultChecked: getDefaultChecked(),
|
|
130
127
|
onChange: (event) => {
|
|
131
|
-
inputProps.
|
|
128
|
+
inputProps.dispatch();
|
|
132
129
|
props.onChange?.(event);
|
|
133
130
|
}
|
|
134
131
|
};
|
package/package.json
CHANGED
package/dist/serialize.d.mts
DELETED
package/dist/serialize.d.ts
DELETED