react-store-input 0.1.4 → 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 +58 -129
- package/dist/index.mjs +60 -131
- package/dist/use_form_store.d.mts +2 -0
- package/dist/use_form_store.d.ts +2 -0
- package/dist/use_form_store.js +58 -38
- package/dist/use_form_store.mjs +60 -42
- package/dist/use_store_component.d.mts +11 -14
- package/dist/use_store_component.d.ts +11 -14
- package/dist/use_store_component.js +58 -38
- package/dist/use_store_component.mjs +60 -42
- package/dist/use_store_controller.d.mts +1 -1
- package/dist/use_store_controller.d.ts +1 -1
- package/dist/use_store_controller.js +3 -13
- package/dist/use_store_controller.mjs +4 -14
- package/dist/use_store_input.d.mts +6 -4
- package/dist/use_store_input.d.ts +6 -4
- package/dist/use_store_input.js +13 -26
- package/dist/use_store_input.mjs +14 -27
- 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 +13 -26
- package/dist/use_store_input_with_name.mjs +14 -27
- 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
|
|
@@ -9,7 +7,7 @@ import "react";
|
|
|
9
7
|
import { format } from "date-fns";
|
|
10
8
|
|
|
11
9
|
// src/use_store_controller.tsx
|
|
12
|
-
import {
|
|
10
|
+
import { useId, useImperativeHandle, useRef } from "react";
|
|
13
11
|
|
|
14
12
|
// src/use_subscribe.tsx
|
|
15
13
|
import { useEffect } from "react";
|
|
@@ -25,18 +23,8 @@ function useSubscribe(store, subscriber) {
|
|
|
25
23
|
// src/use_store_controller.tsx
|
|
26
24
|
function useStoreController(store, props) {
|
|
27
25
|
const ref = useRef(null);
|
|
26
|
+
useImperativeHandle(props.ref, () => ref.current, []);
|
|
28
27
|
const dispatchKey = useId();
|
|
29
|
-
useEffect2(() => {
|
|
30
|
-
const element = ref.current;
|
|
31
|
-
if (!element) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (typeof props.ref === "function") {
|
|
35
|
-
props.ref(element);
|
|
36
|
-
} else if (props.ref) {
|
|
37
|
-
props.ref.current = element;
|
|
38
|
-
}
|
|
39
|
-
}, []);
|
|
40
28
|
useSubscribe(store, (state, key) => {
|
|
41
29
|
if (key === dispatchKey) {
|
|
42
30
|
return;
|
|
@@ -44,7 +32,7 @@ function useStoreController(store, props) {
|
|
|
44
32
|
if (!ref.current) return;
|
|
45
33
|
props.onSubscribe(state, ref.current);
|
|
46
34
|
});
|
|
47
|
-
const
|
|
35
|
+
const dispatch = () => {
|
|
48
36
|
store.dispatch(
|
|
49
37
|
(state) => {
|
|
50
38
|
const element = ref.current;
|
|
@@ -58,26 +46,24 @@ function useStoreController(store, props) {
|
|
|
58
46
|
};
|
|
59
47
|
return {
|
|
60
48
|
ref,
|
|
61
|
-
|
|
49
|
+
dispatch
|
|
62
50
|
};
|
|
63
51
|
}
|
|
64
52
|
|
|
65
53
|
// src/use_store_input.tsx
|
|
66
54
|
function useStoreInput(store, props) {
|
|
67
|
-
const toInputValue = (value) => {
|
|
55
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
68
56
|
if (value === void 0 || value === null) {
|
|
69
57
|
return "";
|
|
70
58
|
}
|
|
71
59
|
if (props.type === "datetime-local") {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (typeof value === "string") {
|
|
76
|
-
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");
|
|
77
63
|
}
|
|
78
64
|
}
|
|
79
65
|
return String(value);
|
|
80
|
-
};
|
|
66
|
+
});
|
|
81
67
|
const getDefaultValue = () => {
|
|
82
68
|
if (props.defaultValue !== void 0) {
|
|
83
69
|
return props.defaultValue;
|
|
@@ -102,16 +88,15 @@ function useStoreInput(store, props) {
|
|
|
102
88
|
}
|
|
103
89
|
return toInputChecked(props.getter(store.state));
|
|
104
90
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (typeof selected === "number") {
|
|
91
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
92
|
+
if (props.type === "number" || props.type === "range") {
|
|
108
93
|
return Number(value);
|
|
109
94
|
}
|
|
110
|
-
if (
|
|
95
|
+
if (props.type === "datetime-local") {
|
|
111
96
|
return new Date(value);
|
|
112
97
|
}
|
|
113
98
|
return value;
|
|
114
|
-
}
|
|
99
|
+
});
|
|
115
100
|
const inputProps = useStoreController(store, {
|
|
116
101
|
ref: props.ref,
|
|
117
102
|
onSubscribe: (state, element) => {
|
|
@@ -144,7 +129,7 @@ function useStoreInput(store, props) {
|
|
|
144
129
|
defaultValue: getDefaultValue(),
|
|
145
130
|
defaultChecked: getDefaultChecked(),
|
|
146
131
|
onChange: (event) => {
|
|
147
|
-
inputProps.
|
|
132
|
+
inputProps.dispatch();
|
|
148
133
|
props.onChange?.(event);
|
|
149
134
|
}
|
|
150
135
|
};
|
|
@@ -176,15 +161,24 @@ function useStoreInputWithName(store, props) {
|
|
|
176
161
|
|
|
177
162
|
// src/use_store_component.tsx
|
|
178
163
|
function useStoreComponent(store) {
|
|
179
|
-
const input = useCallback(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
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
|
+
);
|
|
188
182
|
return {
|
|
189
183
|
input,
|
|
190
184
|
select,
|
|
@@ -195,27 +189,51 @@ function Input({
|
|
|
195
189
|
store,
|
|
196
190
|
getter,
|
|
197
191
|
setter,
|
|
192
|
+
toInputValue,
|
|
193
|
+
toStateValue,
|
|
198
194
|
...props
|
|
199
195
|
}) {
|
|
200
|
-
const storeProps = useStoreInputWithName(store, {
|
|
196
|
+
const storeProps = useStoreInputWithName(store, {
|
|
197
|
+
...props,
|
|
198
|
+
getter,
|
|
199
|
+
setter,
|
|
200
|
+
toInputValue,
|
|
201
|
+
toStateValue
|
|
202
|
+
});
|
|
201
203
|
return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
|
|
202
204
|
}
|
|
203
205
|
function Select({
|
|
204
206
|
store,
|
|
205
207
|
getter,
|
|
206
208
|
setter,
|
|
209
|
+
toInputValue,
|
|
210
|
+
toStateValue,
|
|
207
211
|
...props
|
|
208
212
|
}) {
|
|
209
|
-
const storeProps = useStoreInputWithName(store, {
|
|
213
|
+
const storeProps = useStoreInputWithName(store, {
|
|
214
|
+
...props,
|
|
215
|
+
getter,
|
|
216
|
+
setter,
|
|
217
|
+
toInputValue,
|
|
218
|
+
toStateValue
|
|
219
|
+
});
|
|
210
220
|
return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
|
|
211
221
|
}
|
|
212
222
|
function Textarea({
|
|
213
223
|
store,
|
|
214
224
|
getter,
|
|
215
225
|
setter,
|
|
226
|
+
toInputValue,
|
|
227
|
+
toStateValue,
|
|
216
228
|
...props
|
|
217
229
|
}) {
|
|
218
|
-
const storeProps = useStoreInputWithName(store, {
|
|
230
|
+
const storeProps = useStoreInputWithName(store, {
|
|
231
|
+
...props,
|
|
232
|
+
getter,
|
|
233
|
+
setter,
|
|
234
|
+
toInputValue,
|
|
235
|
+
toStateValue
|
|
236
|
+
});
|
|
219
237
|
return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
|
|
220
238
|
}
|
|
221
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 };
|
|
@@ -39,18 +39,8 @@ function useSubscribe(store, subscriber) {
|
|
|
39
39
|
// src/use_store_controller.tsx
|
|
40
40
|
function useStoreController(store, props) {
|
|
41
41
|
const ref = (0, import_react2.useRef)(null);
|
|
42
|
+
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
42
43
|
const dispatchKey = (0, import_react2.useId)();
|
|
43
|
-
(0, import_react2.useEffect)(() => {
|
|
44
|
-
const element = ref.current;
|
|
45
|
-
if (!element) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (typeof props.ref === "function") {
|
|
49
|
-
props.ref(element);
|
|
50
|
-
} else if (props.ref) {
|
|
51
|
-
props.ref.current = element;
|
|
52
|
-
}
|
|
53
|
-
}, []);
|
|
54
44
|
useSubscribe(store, (state, key) => {
|
|
55
45
|
if (key === dispatchKey) {
|
|
56
46
|
return;
|
|
@@ -58,7 +48,7 @@ function useStoreController(store, props) {
|
|
|
58
48
|
if (!ref.current) return;
|
|
59
49
|
props.onSubscribe(state, ref.current);
|
|
60
50
|
});
|
|
61
|
-
const
|
|
51
|
+
const dispatch = () => {
|
|
62
52
|
store.dispatch(
|
|
63
53
|
(state) => {
|
|
64
54
|
const element = ref.current;
|
|
@@ -72,7 +62,7 @@ function useStoreController(store, props) {
|
|
|
72
62
|
};
|
|
73
63
|
return {
|
|
74
64
|
ref,
|
|
75
|
-
|
|
65
|
+
dispatch
|
|
76
66
|
};
|
|
77
67
|
}
|
|
78
68
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/use_store_controller.tsx
|
|
2
|
-
import {
|
|
2
|
+
import { useId, useImperativeHandle, useRef } from "react";
|
|
3
3
|
|
|
4
4
|
// src/use_subscribe.tsx
|
|
5
5
|
import { useEffect } from "react";
|
|
@@ -15,18 +15,8 @@ function useSubscribe(store, subscriber) {
|
|
|
15
15
|
// src/use_store_controller.tsx
|
|
16
16
|
function useStoreController(store, props) {
|
|
17
17
|
const ref = useRef(null);
|
|
18
|
+
useImperativeHandle(props.ref, () => ref.current, []);
|
|
18
19
|
const dispatchKey = useId();
|
|
19
|
-
useEffect2(() => {
|
|
20
|
-
const element = ref.current;
|
|
21
|
-
if (!element) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
if (typeof props.ref === "function") {
|
|
25
|
-
props.ref(element);
|
|
26
|
-
} else if (props.ref) {
|
|
27
|
-
props.ref.current = element;
|
|
28
|
-
}
|
|
29
|
-
}, []);
|
|
30
20
|
useSubscribe(store, (state, key) => {
|
|
31
21
|
if (key === dispatchKey) {
|
|
32
22
|
return;
|
|
@@ -34,7 +24,7 @@ function useStoreController(store, props) {
|
|
|
34
24
|
if (!ref.current) return;
|
|
35
25
|
props.onSubscribe(state, ref.current);
|
|
36
26
|
});
|
|
37
|
-
const
|
|
27
|
+
const dispatch = () => {
|
|
38
28
|
store.dispatch(
|
|
39
29
|
(state) => {
|
|
40
30
|
const element = ref.current;
|
|
@@ -48,7 +38,7 @@ function useStoreController(store, props) {
|
|
|
48
38
|
};
|
|
49
39
|
return {
|
|
50
40
|
ref,
|
|
51
|
-
|
|
41
|
+
dispatch
|
|
52
42
|
};
|
|
53
43
|
}
|
|
54
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
|
@@ -43,18 +43,8 @@ function useSubscribe(store, subscriber) {
|
|
|
43
43
|
// src/use_store_controller.tsx
|
|
44
44
|
function useStoreController(store, props) {
|
|
45
45
|
const ref = (0, import_react2.useRef)(null);
|
|
46
|
+
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
46
47
|
const dispatchKey = (0, import_react2.useId)();
|
|
47
|
-
(0, import_react2.useEffect)(() => {
|
|
48
|
-
const element = ref.current;
|
|
49
|
-
if (!element) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (typeof props.ref === "function") {
|
|
53
|
-
props.ref(element);
|
|
54
|
-
} else if (props.ref) {
|
|
55
|
-
props.ref.current = element;
|
|
56
|
-
}
|
|
57
|
-
}, []);
|
|
58
48
|
useSubscribe(store, (state, key) => {
|
|
59
49
|
if (key === dispatchKey) {
|
|
60
50
|
return;
|
|
@@ -62,7 +52,7 @@ function useStoreController(store, props) {
|
|
|
62
52
|
if (!ref.current) return;
|
|
63
53
|
props.onSubscribe(state, ref.current);
|
|
64
54
|
});
|
|
65
|
-
const
|
|
55
|
+
const dispatch = () => {
|
|
66
56
|
store.dispatch(
|
|
67
57
|
(state) => {
|
|
68
58
|
const element = ref.current;
|
|
@@ -76,26 +66,24 @@ function useStoreController(store, props) {
|
|
|
76
66
|
};
|
|
77
67
|
return {
|
|
78
68
|
ref,
|
|
79
|
-
|
|
69
|
+
dispatch
|
|
80
70
|
};
|
|
81
71
|
}
|
|
82
72
|
|
|
83
73
|
// src/use_store_input.tsx
|
|
84
74
|
function useStoreInput(store, props) {
|
|
85
|
-
const toInputValue = (value) => {
|
|
75
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
86
76
|
if (value === void 0 || value === null) {
|
|
87
77
|
return "";
|
|
88
78
|
}
|
|
89
79
|
if (props.type === "datetime-local") {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (typeof value === "string") {
|
|
94
|
-
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");
|
|
95
83
|
}
|
|
96
84
|
}
|
|
97
85
|
return String(value);
|
|
98
|
-
};
|
|
86
|
+
});
|
|
99
87
|
const getDefaultValue = () => {
|
|
100
88
|
if (props.defaultValue !== void 0) {
|
|
101
89
|
return props.defaultValue;
|
|
@@ -120,16 +108,15 @@ function useStoreInput(store, props) {
|
|
|
120
108
|
}
|
|
121
109
|
return toInputChecked(props.getter(store.state));
|
|
122
110
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (typeof selected === "number") {
|
|
111
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
112
|
+
if (props.type === "number" || props.type === "range") {
|
|
126
113
|
return Number(value);
|
|
127
114
|
}
|
|
128
|
-
if (
|
|
115
|
+
if (props.type === "datetime-local") {
|
|
129
116
|
return new Date(value);
|
|
130
117
|
}
|
|
131
118
|
return value;
|
|
132
|
-
}
|
|
119
|
+
});
|
|
133
120
|
const inputProps = useStoreController(store, {
|
|
134
121
|
ref: props.ref,
|
|
135
122
|
onSubscribe: (state, element) => {
|
|
@@ -162,7 +149,7 @@ function useStoreInput(store, props) {
|
|
|
162
149
|
defaultValue: getDefaultValue(),
|
|
163
150
|
defaultChecked: getDefaultChecked(),
|
|
164
151
|
onChange: (event) => {
|
|
165
|
-
inputProps.
|
|
152
|
+
inputProps.dispatch();
|
|
166
153
|
props.onChange?.(event);
|
|
167
154
|
}
|
|
168
155
|
};
|
package/dist/use_store_input.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import "react";
|
|
|
3
3
|
import { format } from "date-fns";
|
|
4
4
|
|
|
5
5
|
// src/use_store_controller.tsx
|
|
6
|
-
import {
|
|
6
|
+
import { useId, useImperativeHandle, useRef } from "react";
|
|
7
7
|
|
|
8
8
|
// src/use_subscribe.tsx
|
|
9
9
|
import { useEffect } from "react";
|
|
@@ -19,18 +19,8 @@ function useSubscribe(store, subscriber) {
|
|
|
19
19
|
// src/use_store_controller.tsx
|
|
20
20
|
function useStoreController(store, props) {
|
|
21
21
|
const ref = useRef(null);
|
|
22
|
+
useImperativeHandle(props.ref, () => ref.current, []);
|
|
22
23
|
const dispatchKey = useId();
|
|
23
|
-
useEffect2(() => {
|
|
24
|
-
const element = ref.current;
|
|
25
|
-
if (!element) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
if (typeof props.ref === "function") {
|
|
29
|
-
props.ref(element);
|
|
30
|
-
} else if (props.ref) {
|
|
31
|
-
props.ref.current = element;
|
|
32
|
-
}
|
|
33
|
-
}, []);
|
|
34
24
|
useSubscribe(store, (state, key) => {
|
|
35
25
|
if (key === dispatchKey) {
|
|
36
26
|
return;
|
|
@@ -38,7 +28,7 @@ function useStoreController(store, props) {
|
|
|
38
28
|
if (!ref.current) return;
|
|
39
29
|
props.onSubscribe(state, ref.current);
|
|
40
30
|
});
|
|
41
|
-
const
|
|
31
|
+
const dispatch = () => {
|
|
42
32
|
store.dispatch(
|
|
43
33
|
(state) => {
|
|
44
34
|
const element = ref.current;
|
|
@@ -52,26 +42,24 @@ function useStoreController(store, props) {
|
|
|
52
42
|
};
|
|
53
43
|
return {
|
|
54
44
|
ref,
|
|
55
|
-
|
|
45
|
+
dispatch
|
|
56
46
|
};
|
|
57
47
|
}
|
|
58
48
|
|
|
59
49
|
// src/use_store_input.tsx
|
|
60
50
|
function useStoreInput(store, props) {
|
|
61
|
-
const toInputValue = (value) => {
|
|
51
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
62
52
|
if (value === void 0 || value === null) {
|
|
63
53
|
return "";
|
|
64
54
|
}
|
|
65
55
|
if (props.type === "datetime-local") {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (typeof value === "string") {
|
|
70
|
-
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");
|
|
71
59
|
}
|
|
72
60
|
}
|
|
73
61
|
return String(value);
|
|
74
|
-
};
|
|
62
|
+
});
|
|
75
63
|
const getDefaultValue = () => {
|
|
76
64
|
if (props.defaultValue !== void 0) {
|
|
77
65
|
return props.defaultValue;
|
|
@@ -96,16 +84,15 @@ function useStoreInput(store, props) {
|
|
|
96
84
|
}
|
|
97
85
|
return toInputChecked(props.getter(store.state));
|
|
98
86
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (typeof selected === "number") {
|
|
87
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
88
|
+
if (props.type === "number" || props.type === "range") {
|
|
102
89
|
return Number(value);
|
|
103
90
|
}
|
|
104
|
-
if (
|
|
91
|
+
if (props.type === "datetime-local") {
|
|
105
92
|
return new Date(value);
|
|
106
93
|
}
|
|
107
94
|
return value;
|
|
108
|
-
}
|
|
95
|
+
});
|
|
109
96
|
const inputProps = useStoreController(store, {
|
|
110
97
|
ref: props.ref,
|
|
111
98
|
onSubscribe: (state, element) => {
|
|
@@ -138,7 +125,7 @@ function useStoreInput(store, props) {
|
|
|
138
125
|
defaultValue: getDefaultValue(),
|
|
139
126
|
defaultChecked: getDefaultChecked(),
|
|
140
127
|
onChange: (event) => {
|
|
141
|
-
inputProps.
|
|
128
|
+
inputProps.dispatch();
|
|
142
129
|
props.onChange?.(event);
|
|
143
130
|
}
|
|
144
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 };
|
|
@@ -45,18 +45,8 @@ function useSubscribe(store, subscriber) {
|
|
|
45
45
|
// src/use_store_controller.tsx
|
|
46
46
|
function useStoreController(store, props) {
|
|
47
47
|
const ref = (0, import_react2.useRef)(null);
|
|
48
|
+
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
48
49
|
const dispatchKey = (0, import_react2.useId)();
|
|
49
|
-
(0, import_react2.useEffect)(() => {
|
|
50
|
-
const element = ref.current;
|
|
51
|
-
if (!element) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (typeof props.ref === "function") {
|
|
55
|
-
props.ref(element);
|
|
56
|
-
} else if (props.ref) {
|
|
57
|
-
props.ref.current = element;
|
|
58
|
-
}
|
|
59
|
-
}, []);
|
|
60
50
|
useSubscribe(store, (state, key) => {
|
|
61
51
|
if (key === dispatchKey) {
|
|
62
52
|
return;
|
|
@@ -64,7 +54,7 @@ function useStoreController(store, props) {
|
|
|
64
54
|
if (!ref.current) return;
|
|
65
55
|
props.onSubscribe(state, ref.current);
|
|
66
56
|
});
|
|
67
|
-
const
|
|
57
|
+
const dispatch = () => {
|
|
68
58
|
store.dispatch(
|
|
69
59
|
(state) => {
|
|
70
60
|
const element = ref.current;
|
|
@@ -78,26 +68,24 @@ function useStoreController(store, props) {
|
|
|
78
68
|
};
|
|
79
69
|
return {
|
|
80
70
|
ref,
|
|
81
|
-
|
|
71
|
+
dispatch
|
|
82
72
|
};
|
|
83
73
|
}
|
|
84
74
|
|
|
85
75
|
// src/use_store_input.tsx
|
|
86
76
|
function useStoreInput(store, props) {
|
|
87
|
-
const toInputValue = (value) => {
|
|
77
|
+
const toInputValue = props.toInputValue || ((value) => {
|
|
88
78
|
if (value === void 0 || value === null) {
|
|
89
79
|
return "";
|
|
90
80
|
}
|
|
91
81
|
if (props.type === "datetime-local") {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (typeof value === "string") {
|
|
96
|
-
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");
|
|
97
85
|
}
|
|
98
86
|
}
|
|
99
87
|
return String(value);
|
|
100
|
-
};
|
|
88
|
+
});
|
|
101
89
|
const getDefaultValue = () => {
|
|
102
90
|
if (props.defaultValue !== void 0) {
|
|
103
91
|
return props.defaultValue;
|
|
@@ -122,16 +110,15 @@ function useStoreInput(store, props) {
|
|
|
122
110
|
}
|
|
123
111
|
return toInputChecked(props.getter(store.state));
|
|
124
112
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (typeof selected === "number") {
|
|
113
|
+
const toStateValue = props.toStateValue || ((value) => {
|
|
114
|
+
if (props.type === "number" || props.type === "range") {
|
|
128
115
|
return Number(value);
|
|
129
116
|
}
|
|
130
|
-
if (
|
|
117
|
+
if (props.type === "datetime-local") {
|
|
131
118
|
return new Date(value);
|
|
132
119
|
}
|
|
133
120
|
return value;
|
|
134
|
-
}
|
|
121
|
+
});
|
|
135
122
|
const inputProps = useStoreController(store, {
|
|
136
123
|
ref: props.ref,
|
|
137
124
|
onSubscribe: (state, element) => {
|
|
@@ -164,7 +151,7 @@ function useStoreInput(store, props) {
|
|
|
164
151
|
defaultValue: getDefaultValue(),
|
|
165
152
|
defaultChecked: getDefaultChecked(),
|
|
166
153
|
onChange: (event) => {
|
|
167
|
-
inputProps.
|
|
154
|
+
inputProps.dispatch();
|
|
168
155
|
props.onChange?.(event);
|
|
169
156
|
}
|
|
170
157
|
};
|