react-store-input 0.1.6 → 0.1.8
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +21 -18
- package/dist/index.mjs +23 -20
- package/dist/use_form_store.js +21 -18
- package/dist/use_form_store.mjs +23 -20
- package/dist/use_store_component.d.mts +7 -6
- package/dist/use_store_component.d.ts +7 -6
- package/dist/use_store_component.js +21 -18
- package/dist/use_store_component.mjs +23 -20
- package/dist/use_store_controller.d.mts +4 -8
- package/dist/use_store_controller.d.ts +4 -8
- package/dist/use_store_controller.js +2 -8
- package/dist/use_store_controller.mjs +3 -9
- package/dist/use_store_input.d.mts +2 -5
- package/dist/use_store_input.d.ts +2 -5
- package/dist/use_store_input.js +13 -13
- package/dist/use_store_input.mjs +14 -14
- package/dist/use_store_input_with_name.d.mts +2 -2
- package/dist/use_store_input_with_name.d.ts +2 -2
- package/dist/use_store_input_with_name.js +15 -15
- package/dist/use_store_input_with_name.mjs +16 -16
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/use_store_component.tsx
|
|
2
|
-
import { useCallback } from "react";
|
|
2
|
+
import { useCallback, useRef } from "react";
|
|
3
3
|
import React from "react";
|
|
4
4
|
|
|
5
5
|
// src/use_store_input.tsx
|
|
@@ -7,7 +7,7 @@ import "react";
|
|
|
7
7
|
import { format } from "date-fns";
|
|
8
8
|
|
|
9
9
|
// src/use_store_controller.tsx
|
|
10
|
-
import { useId
|
|
10
|
+
import { useId } from "react";
|
|
11
11
|
|
|
12
12
|
// src/use_subscribe.tsx
|
|
13
13
|
import { useEffect } from "react";
|
|
@@ -22,22 +22,17 @@ function useSubscribe(store, subscriber) {
|
|
|
22
22
|
|
|
23
23
|
// src/use_store_controller.tsx
|
|
24
24
|
function useStoreController(store, props) {
|
|
25
|
-
const ref = useRef(null);
|
|
26
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
27
25
|
const dispatchKey = useId();
|
|
28
26
|
useSubscribe(store, (state, key) => {
|
|
29
27
|
if (key === dispatchKey) {
|
|
30
28
|
return;
|
|
31
29
|
}
|
|
32
|
-
|
|
33
|
-
props.onSubscribe(state, ref.current);
|
|
30
|
+
props.onSubscribe(state);
|
|
34
31
|
});
|
|
35
32
|
const dispatch = () => {
|
|
36
33
|
store.dispatch(
|
|
37
34
|
(state) => {
|
|
38
|
-
|
|
39
|
-
if (!element) return;
|
|
40
|
-
props.onDispatch(state, element);
|
|
35
|
+
props.onDispatch(state);
|
|
41
36
|
},
|
|
42
37
|
{
|
|
43
38
|
key: dispatchKey
|
|
@@ -45,13 +40,12 @@ function useStoreController(store, props) {
|
|
|
45
40
|
);
|
|
46
41
|
};
|
|
47
42
|
return {
|
|
48
|
-
ref,
|
|
49
43
|
dispatch
|
|
50
44
|
};
|
|
51
45
|
}
|
|
52
46
|
|
|
53
47
|
// src/use_store_input.tsx
|
|
54
|
-
function useStoreInput(store, props) {
|
|
48
|
+
function useStoreInput(ref, store, props) {
|
|
55
49
|
const toInputValue = props.toInputValue || ((value) => {
|
|
56
50
|
if (value === void 0 || value === null) {
|
|
57
51
|
return "";
|
|
@@ -98,8 +92,11 @@ function useStoreInput(store, props) {
|
|
|
98
92
|
return value;
|
|
99
93
|
});
|
|
100
94
|
const inputProps = useStoreController(store, {
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
onSubscribe: (state) => {
|
|
96
|
+
const element = ref.current;
|
|
97
|
+
if (!element) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
103
100
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
104
101
|
const checked = toInputChecked(props.getter(state));
|
|
105
102
|
if (element.checked === checked) {
|
|
@@ -116,7 +113,11 @@ function useStoreInput(store, props) {
|
|
|
116
113
|
const event = new Event("input", { bubbles: true });
|
|
117
114
|
element.dispatchEvent(event);
|
|
118
115
|
},
|
|
119
|
-
onDispatch: (state
|
|
116
|
+
onDispatch: (state) => {
|
|
117
|
+
const element = ref.current;
|
|
118
|
+
if (!element) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
120
121
|
if ("checked" in element && props.type === "checkbox") {
|
|
121
122
|
props.setter(state, element.checked);
|
|
122
123
|
} else {
|
|
@@ -125,7 +126,6 @@ function useStoreInput(store, props) {
|
|
|
125
126
|
}
|
|
126
127
|
});
|
|
127
128
|
return {
|
|
128
|
-
ref: inputProps.ref,
|
|
129
129
|
defaultValue: getDefaultValue(),
|
|
130
130
|
defaultChecked: getDefaultChecked(),
|
|
131
131
|
onChange: (event) => {
|
|
@@ -136,8 +136,8 @@ function useStoreInput(store, props) {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// src/use_store_input_with_name.tsx
|
|
139
|
-
function useStoreInputWithName(store, props) {
|
|
140
|
-
const inputProps = useStoreInput(store, {
|
|
139
|
+
function useStoreInputWithName(ref, store, props) {
|
|
140
|
+
const inputProps = useStoreInput(ref, store, {
|
|
141
141
|
...props,
|
|
142
142
|
getter: (state) => {
|
|
143
143
|
if (props.getter) {
|
|
@@ -193,7 +193,8 @@ function Input({
|
|
|
193
193
|
toStateValue,
|
|
194
194
|
...props
|
|
195
195
|
}) {
|
|
196
|
-
const
|
|
196
|
+
const ref = useRef(null);
|
|
197
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
197
198
|
...props,
|
|
198
199
|
getter,
|
|
199
200
|
setter,
|
|
@@ -210,7 +211,8 @@ function Select({
|
|
|
210
211
|
toStateValue,
|
|
211
212
|
...props
|
|
212
213
|
}) {
|
|
213
|
-
const
|
|
214
|
+
const ref = useRef(null);
|
|
215
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
214
216
|
...props,
|
|
215
217
|
getter,
|
|
216
218
|
setter,
|
|
@@ -227,7 +229,8 @@ function Textarea({
|
|
|
227
229
|
toStateValue,
|
|
228
230
|
...props
|
|
229
231
|
}) {
|
|
230
|
-
const
|
|
232
|
+
const ref = useRef(null);
|
|
233
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
231
234
|
...props,
|
|
232
235
|
getter,
|
|
233
236
|
setter,
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { Ref } from 'react';
|
|
3
1
|
import { Store } from './use_store.mjs';
|
|
4
2
|
|
|
5
|
-
type StoreControllerProps<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
onDispatch: (state: TState, element: TController) => void;
|
|
3
|
+
type StoreControllerProps<TState> = {
|
|
4
|
+
onSubscribe: (state: TState) => void;
|
|
5
|
+
onDispatch: (state: TState) => void;
|
|
9
6
|
};
|
|
10
|
-
declare function useStoreController<
|
|
11
|
-
ref: React.RefObject<TController | null>;
|
|
7
|
+
declare function useStoreController<TState>(store: Store<TState>, props: StoreControllerProps<TState>): {
|
|
12
8
|
dispatch: () => void;
|
|
13
9
|
};
|
|
14
10
|
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { Ref } from 'react';
|
|
3
1
|
import { Store } from './use_store.js';
|
|
4
2
|
|
|
5
|
-
type StoreControllerProps<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
onDispatch: (state: TState, element: TController) => void;
|
|
3
|
+
type StoreControllerProps<TState> = {
|
|
4
|
+
onSubscribe: (state: TState) => void;
|
|
5
|
+
onDispatch: (state: TState) => void;
|
|
9
6
|
};
|
|
10
|
-
declare function useStoreController<
|
|
11
|
-
ref: React.RefObject<TController | null>;
|
|
7
|
+
declare function useStoreController<TState>(store: Store<TState>, props: StoreControllerProps<TState>): {
|
|
12
8
|
dispatch: () => void;
|
|
13
9
|
};
|
|
14
10
|
|
|
@@ -38,22 +38,17 @@ function useSubscribe(store, subscriber) {
|
|
|
38
38
|
|
|
39
39
|
// src/use_store_controller.tsx
|
|
40
40
|
function useStoreController(store, props) {
|
|
41
|
-
const ref = (0, import_react2.useRef)(null);
|
|
42
|
-
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
43
41
|
const dispatchKey = (0, import_react2.useId)();
|
|
44
42
|
useSubscribe(store, (state, key) => {
|
|
45
43
|
if (key === dispatchKey) {
|
|
46
44
|
return;
|
|
47
45
|
}
|
|
48
|
-
|
|
49
|
-
props.onSubscribe(state, ref.current);
|
|
46
|
+
props.onSubscribe(state);
|
|
50
47
|
});
|
|
51
48
|
const dispatch = () => {
|
|
52
49
|
store.dispatch(
|
|
53
50
|
(state) => {
|
|
54
|
-
|
|
55
|
-
if (!element) return;
|
|
56
|
-
props.onDispatch(state, element);
|
|
51
|
+
props.onDispatch(state);
|
|
57
52
|
},
|
|
58
53
|
{
|
|
59
54
|
key: dispatchKey
|
|
@@ -61,7 +56,6 @@ function useStoreController(store, props) {
|
|
|
61
56
|
);
|
|
62
57
|
};
|
|
63
58
|
return {
|
|
64
|
-
ref,
|
|
65
59
|
dispatch
|
|
66
60
|
};
|
|
67
61
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/use_store_controller.tsx
|
|
2
|
-
import { useId
|
|
2
|
+
import { useId } from "react";
|
|
3
3
|
|
|
4
4
|
// src/use_subscribe.tsx
|
|
5
5
|
import { useEffect } from "react";
|
|
@@ -14,22 +14,17 @@ function useSubscribe(store, subscriber) {
|
|
|
14
14
|
|
|
15
15
|
// src/use_store_controller.tsx
|
|
16
16
|
function useStoreController(store, props) {
|
|
17
|
-
const ref = useRef(null);
|
|
18
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
19
17
|
const dispatchKey = useId();
|
|
20
18
|
useSubscribe(store, (state, key) => {
|
|
21
19
|
if (key === dispatchKey) {
|
|
22
20
|
return;
|
|
23
21
|
}
|
|
24
|
-
|
|
25
|
-
props.onSubscribe(state, ref.current);
|
|
22
|
+
props.onSubscribe(state);
|
|
26
23
|
});
|
|
27
24
|
const dispatch = () => {
|
|
28
25
|
store.dispatch(
|
|
29
26
|
(state) => {
|
|
30
|
-
|
|
31
|
-
if (!element) return;
|
|
32
|
-
props.onDispatch(state, element);
|
|
27
|
+
props.onDispatch(state);
|
|
33
28
|
},
|
|
34
29
|
{
|
|
35
30
|
key: dispatchKey
|
|
@@ -37,7 +32,6 @@ function useStoreController(store, props) {
|
|
|
37
32
|
);
|
|
38
33
|
};
|
|
39
34
|
return {
|
|
40
|
-
ref,
|
|
41
35
|
dispatch
|
|
42
36
|
};
|
|
43
37
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Ref, HTMLInputTypeAttribute } from 'react';
|
|
1
|
+
import { HTMLInputTypeAttribute, RefObject } from 'react';
|
|
3
2
|
import { Store } from './use_store.mjs';
|
|
4
3
|
|
|
5
4
|
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
6
|
-
ref?: Ref<TInputElement>;
|
|
7
5
|
type?: HTMLInputTypeAttribute;
|
|
8
6
|
defaultValue?: string | number | readonly string[] | undefined;
|
|
9
7
|
value?: string | number | readonly string[] | undefined;
|
|
@@ -15,8 +13,7 @@ type StoreInputProps<TInputElement, TState, TValue> = {
|
|
|
15
13
|
toInputValue?: (value: TValue) => string;
|
|
16
14
|
toStateValue?: (value: string) => TValue;
|
|
17
15
|
};
|
|
18
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
19
|
-
ref: React$1.RefObject<TInputElement | null>;
|
|
16
|
+
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
20
17
|
defaultValue: string | number | readonly string[] | undefined;
|
|
21
18
|
defaultChecked: boolean | undefined;
|
|
22
19
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Ref, HTMLInputTypeAttribute } from 'react';
|
|
1
|
+
import { HTMLInputTypeAttribute, RefObject } from 'react';
|
|
3
2
|
import { Store } from './use_store.js';
|
|
4
3
|
|
|
5
4
|
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
6
|
-
ref?: Ref<TInputElement>;
|
|
7
5
|
type?: HTMLInputTypeAttribute;
|
|
8
6
|
defaultValue?: string | number | readonly string[] | undefined;
|
|
9
7
|
value?: string | number | readonly string[] | undefined;
|
|
@@ -15,8 +13,7 @@ type StoreInputProps<TInputElement, TState, TValue> = {
|
|
|
15
13
|
toInputValue?: (value: TValue) => string;
|
|
16
14
|
toStateValue?: (value: string) => TValue;
|
|
17
15
|
};
|
|
18
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
19
|
-
ref: React$1.RefObject<TInputElement | null>;
|
|
16
|
+
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
20
17
|
defaultValue: string | number | readonly string[] | undefined;
|
|
21
18
|
defaultChecked: boolean | undefined;
|
|
22
19
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
package/dist/use_store_input.js
CHANGED
|
@@ -42,22 +42,17 @@ function useSubscribe(store, subscriber) {
|
|
|
42
42
|
|
|
43
43
|
// src/use_store_controller.tsx
|
|
44
44
|
function useStoreController(store, props) {
|
|
45
|
-
const ref = (0, import_react2.useRef)(null);
|
|
46
|
-
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
47
45
|
const dispatchKey = (0, import_react2.useId)();
|
|
48
46
|
useSubscribe(store, (state, key) => {
|
|
49
47
|
if (key === dispatchKey) {
|
|
50
48
|
return;
|
|
51
49
|
}
|
|
52
|
-
|
|
53
|
-
props.onSubscribe(state, ref.current);
|
|
50
|
+
props.onSubscribe(state);
|
|
54
51
|
});
|
|
55
52
|
const dispatch = () => {
|
|
56
53
|
store.dispatch(
|
|
57
54
|
(state) => {
|
|
58
|
-
|
|
59
|
-
if (!element) return;
|
|
60
|
-
props.onDispatch(state, element);
|
|
55
|
+
props.onDispatch(state);
|
|
61
56
|
},
|
|
62
57
|
{
|
|
63
58
|
key: dispatchKey
|
|
@@ -65,13 +60,12 @@ function useStoreController(store, props) {
|
|
|
65
60
|
);
|
|
66
61
|
};
|
|
67
62
|
return {
|
|
68
|
-
ref,
|
|
69
63
|
dispatch
|
|
70
64
|
};
|
|
71
65
|
}
|
|
72
66
|
|
|
73
67
|
// src/use_store_input.tsx
|
|
74
|
-
function useStoreInput(store, props) {
|
|
68
|
+
function useStoreInput(ref, store, props) {
|
|
75
69
|
const toInputValue = props.toInputValue || ((value) => {
|
|
76
70
|
if (value === void 0 || value === null) {
|
|
77
71
|
return "";
|
|
@@ -118,8 +112,11 @@ function useStoreInput(store, props) {
|
|
|
118
112
|
return value;
|
|
119
113
|
});
|
|
120
114
|
const inputProps = useStoreController(store, {
|
|
121
|
-
|
|
122
|
-
|
|
115
|
+
onSubscribe: (state) => {
|
|
116
|
+
const element = ref.current;
|
|
117
|
+
if (!element) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
123
120
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
124
121
|
const checked = toInputChecked(props.getter(state));
|
|
125
122
|
if (element.checked === checked) {
|
|
@@ -136,7 +133,11 @@ function useStoreInput(store, props) {
|
|
|
136
133
|
const event = new Event("input", { bubbles: true });
|
|
137
134
|
element.dispatchEvent(event);
|
|
138
135
|
},
|
|
139
|
-
onDispatch: (state
|
|
136
|
+
onDispatch: (state) => {
|
|
137
|
+
const element = ref.current;
|
|
138
|
+
if (!element) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
140
141
|
if ("checked" in element && props.type === "checkbox") {
|
|
141
142
|
props.setter(state, element.checked);
|
|
142
143
|
} else {
|
|
@@ -145,7 +146,6 @@ function useStoreInput(store, props) {
|
|
|
145
146
|
}
|
|
146
147
|
});
|
|
147
148
|
return {
|
|
148
|
-
ref: inputProps.ref,
|
|
149
149
|
defaultValue: getDefaultValue(),
|
|
150
150
|
defaultChecked: getDefaultChecked(),
|
|
151
151
|
onChange: (event) => {
|
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 { useId
|
|
6
|
+
import { useId } from "react";
|
|
7
7
|
|
|
8
8
|
// src/use_subscribe.tsx
|
|
9
9
|
import { useEffect } from "react";
|
|
@@ -18,22 +18,17 @@ function useSubscribe(store, subscriber) {
|
|
|
18
18
|
|
|
19
19
|
// src/use_store_controller.tsx
|
|
20
20
|
function useStoreController(store, props) {
|
|
21
|
-
const ref = useRef(null);
|
|
22
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
23
21
|
const dispatchKey = useId();
|
|
24
22
|
useSubscribe(store, (state, key) => {
|
|
25
23
|
if (key === dispatchKey) {
|
|
26
24
|
return;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
props.onSubscribe(state, ref.current);
|
|
26
|
+
props.onSubscribe(state);
|
|
30
27
|
});
|
|
31
28
|
const dispatch = () => {
|
|
32
29
|
store.dispatch(
|
|
33
30
|
(state) => {
|
|
34
|
-
|
|
35
|
-
if (!element) return;
|
|
36
|
-
props.onDispatch(state, element);
|
|
31
|
+
props.onDispatch(state);
|
|
37
32
|
},
|
|
38
33
|
{
|
|
39
34
|
key: dispatchKey
|
|
@@ -41,13 +36,12 @@ function useStoreController(store, props) {
|
|
|
41
36
|
);
|
|
42
37
|
};
|
|
43
38
|
return {
|
|
44
|
-
ref,
|
|
45
39
|
dispatch
|
|
46
40
|
};
|
|
47
41
|
}
|
|
48
42
|
|
|
49
43
|
// src/use_store_input.tsx
|
|
50
|
-
function useStoreInput(store, props) {
|
|
44
|
+
function useStoreInput(ref, store, props) {
|
|
51
45
|
const toInputValue = props.toInputValue || ((value) => {
|
|
52
46
|
if (value === void 0 || value === null) {
|
|
53
47
|
return "";
|
|
@@ -94,8 +88,11 @@ function useStoreInput(store, props) {
|
|
|
94
88
|
return value;
|
|
95
89
|
});
|
|
96
90
|
const inputProps = useStoreController(store, {
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
onSubscribe: (state) => {
|
|
92
|
+
const element = ref.current;
|
|
93
|
+
if (!element) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
99
96
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
100
97
|
const checked = toInputChecked(props.getter(state));
|
|
101
98
|
if (element.checked === checked) {
|
|
@@ -112,7 +109,11 @@ function useStoreInput(store, props) {
|
|
|
112
109
|
const event = new Event("input", { bubbles: true });
|
|
113
110
|
element.dispatchEvent(event);
|
|
114
111
|
},
|
|
115
|
-
onDispatch: (state
|
|
112
|
+
onDispatch: (state) => {
|
|
113
|
+
const element = ref.current;
|
|
114
|
+
if (!element) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
116
117
|
if ("checked" in element && props.type === "checkbox") {
|
|
117
118
|
props.setter(state, element.checked);
|
|
118
119
|
} else {
|
|
@@ -121,7 +122,6 @@ function useStoreInput(store, props) {
|
|
|
121
122
|
}
|
|
122
123
|
});
|
|
123
124
|
return {
|
|
124
|
-
ref: inputProps.ref,
|
|
125
125
|
defaultValue: getDefaultValue(),
|
|
126
126
|
defaultChecked: getDefaultChecked(),
|
|
127
127
|
onChange: (event) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { RefObject } from 'react';
|
|
2
3
|
import { Store } from './use_store.mjs';
|
|
3
4
|
import { StoreInputProps } from './use_store_input.mjs';
|
|
4
5
|
|
|
@@ -6,9 +7,8 @@ type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue>
|
|
|
6
7
|
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
8
|
name?: TName;
|
|
8
9
|
};
|
|
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>): {
|
|
10
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
10
11
|
name: string | undefined;
|
|
11
|
-
ref: React.RefObject<TInputElement | null>;
|
|
12
12
|
defaultValue: string | number | readonly string[] | undefined;
|
|
13
13
|
defaultChecked: boolean | undefined;
|
|
14
14
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { RefObject } from 'react';
|
|
2
3
|
import { Store } from './use_store.js';
|
|
3
4
|
import { StoreInputProps } from './use_store_input.js';
|
|
4
5
|
|
|
@@ -6,9 +7,8 @@ type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue>
|
|
|
6
7
|
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
8
|
name?: TName;
|
|
8
9
|
};
|
|
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>): {
|
|
10
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
10
11
|
name: string | undefined;
|
|
11
|
-
ref: React.RefObject<TInputElement | null>;
|
|
12
12
|
defaultValue: string | number | readonly string[] | undefined;
|
|
13
13
|
defaultChecked: boolean | undefined;
|
|
14
14
|
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
@@ -44,22 +44,17 @@ function useSubscribe(store, subscriber) {
|
|
|
44
44
|
|
|
45
45
|
// src/use_store_controller.tsx
|
|
46
46
|
function useStoreController(store, props) {
|
|
47
|
-
const ref = (0, import_react2.useRef)(null);
|
|
48
|
-
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
49
47
|
const dispatchKey = (0, import_react2.useId)();
|
|
50
48
|
useSubscribe(store, (state, key) => {
|
|
51
49
|
if (key === dispatchKey) {
|
|
52
50
|
return;
|
|
53
51
|
}
|
|
54
|
-
|
|
55
|
-
props.onSubscribe(state, ref.current);
|
|
52
|
+
props.onSubscribe(state);
|
|
56
53
|
});
|
|
57
54
|
const dispatch = () => {
|
|
58
55
|
store.dispatch(
|
|
59
56
|
(state) => {
|
|
60
|
-
|
|
61
|
-
if (!element) return;
|
|
62
|
-
props.onDispatch(state, element);
|
|
57
|
+
props.onDispatch(state);
|
|
63
58
|
},
|
|
64
59
|
{
|
|
65
60
|
key: dispatchKey
|
|
@@ -67,13 +62,12 @@ function useStoreController(store, props) {
|
|
|
67
62
|
);
|
|
68
63
|
};
|
|
69
64
|
return {
|
|
70
|
-
ref,
|
|
71
65
|
dispatch
|
|
72
66
|
};
|
|
73
67
|
}
|
|
74
68
|
|
|
75
69
|
// src/use_store_input.tsx
|
|
76
|
-
function useStoreInput(store, props) {
|
|
70
|
+
function useStoreInput(ref, store, props) {
|
|
77
71
|
const toInputValue = props.toInputValue || ((value) => {
|
|
78
72
|
if (value === void 0 || value === null) {
|
|
79
73
|
return "";
|
|
@@ -120,8 +114,11 @@ function useStoreInput(store, props) {
|
|
|
120
114
|
return value;
|
|
121
115
|
});
|
|
122
116
|
const inputProps = useStoreController(store, {
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
onSubscribe: (state) => {
|
|
118
|
+
const element = ref.current;
|
|
119
|
+
if (!element) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
125
122
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
126
123
|
const checked = toInputChecked(props.getter(state));
|
|
127
124
|
if (element.checked === checked) {
|
|
@@ -138,7 +135,11 @@ function useStoreInput(store, props) {
|
|
|
138
135
|
const event = new Event("input", { bubbles: true });
|
|
139
136
|
element.dispatchEvent(event);
|
|
140
137
|
},
|
|
141
|
-
onDispatch: (state
|
|
138
|
+
onDispatch: (state) => {
|
|
139
|
+
const element = ref.current;
|
|
140
|
+
if (!element) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
142
143
|
if ("checked" in element && props.type === "checkbox") {
|
|
143
144
|
props.setter(state, element.checked);
|
|
144
145
|
} else {
|
|
@@ -147,7 +148,6 @@ function useStoreInput(store, props) {
|
|
|
147
148
|
}
|
|
148
149
|
});
|
|
149
150
|
return {
|
|
150
|
-
ref: inputProps.ref,
|
|
151
151
|
defaultValue: getDefaultValue(),
|
|
152
152
|
defaultChecked: getDefaultChecked(),
|
|
153
153
|
onChange: (event) => {
|
|
@@ -158,8 +158,8 @@ function useStoreInput(store, props) {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// src/use_store_input_with_name.tsx
|
|
161
|
-
function useStoreInputWithName(store, props) {
|
|
162
|
-
const inputProps = useStoreInput(store, {
|
|
161
|
+
function useStoreInputWithName(ref, store, props) {
|
|
162
|
+
const inputProps = useStoreInput(ref, store, {
|
|
163
163
|
...props,
|
|
164
164
|
getter: (state) => {
|
|
165
165
|
if (props.getter) {
|
|
@@ -3,7 +3,7 @@ import "react";
|
|
|
3
3
|
import { format } from "date-fns";
|
|
4
4
|
|
|
5
5
|
// src/use_store_controller.tsx
|
|
6
|
-
import { useId
|
|
6
|
+
import { useId } from "react";
|
|
7
7
|
|
|
8
8
|
// src/use_subscribe.tsx
|
|
9
9
|
import { useEffect } from "react";
|
|
@@ -18,22 +18,17 @@ function useSubscribe(store, subscriber) {
|
|
|
18
18
|
|
|
19
19
|
// src/use_store_controller.tsx
|
|
20
20
|
function useStoreController(store, props) {
|
|
21
|
-
const ref = useRef(null);
|
|
22
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
23
21
|
const dispatchKey = useId();
|
|
24
22
|
useSubscribe(store, (state, key) => {
|
|
25
23
|
if (key === dispatchKey) {
|
|
26
24
|
return;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
props.onSubscribe(state, ref.current);
|
|
26
|
+
props.onSubscribe(state);
|
|
30
27
|
});
|
|
31
28
|
const dispatch = () => {
|
|
32
29
|
store.dispatch(
|
|
33
30
|
(state) => {
|
|
34
|
-
|
|
35
|
-
if (!element) return;
|
|
36
|
-
props.onDispatch(state, element);
|
|
31
|
+
props.onDispatch(state);
|
|
37
32
|
},
|
|
38
33
|
{
|
|
39
34
|
key: dispatchKey
|
|
@@ -41,13 +36,12 @@ function useStoreController(store, props) {
|
|
|
41
36
|
);
|
|
42
37
|
};
|
|
43
38
|
return {
|
|
44
|
-
ref,
|
|
45
39
|
dispatch
|
|
46
40
|
};
|
|
47
41
|
}
|
|
48
42
|
|
|
49
43
|
// src/use_store_input.tsx
|
|
50
|
-
function useStoreInput(store, props) {
|
|
44
|
+
function useStoreInput(ref, store, props) {
|
|
51
45
|
const toInputValue = props.toInputValue || ((value) => {
|
|
52
46
|
if (value === void 0 || value === null) {
|
|
53
47
|
return "";
|
|
@@ -94,8 +88,11 @@ function useStoreInput(store, props) {
|
|
|
94
88
|
return value;
|
|
95
89
|
});
|
|
96
90
|
const inputProps = useStoreController(store, {
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
onSubscribe: (state) => {
|
|
92
|
+
const element = ref.current;
|
|
93
|
+
if (!element) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
99
96
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
100
97
|
const checked = toInputChecked(props.getter(state));
|
|
101
98
|
if (element.checked === checked) {
|
|
@@ -112,7 +109,11 @@ function useStoreInput(store, props) {
|
|
|
112
109
|
const event = new Event("input", { bubbles: true });
|
|
113
110
|
element.dispatchEvent(event);
|
|
114
111
|
},
|
|
115
|
-
onDispatch: (state
|
|
112
|
+
onDispatch: (state) => {
|
|
113
|
+
const element = ref.current;
|
|
114
|
+
if (!element) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
116
117
|
if ("checked" in element && props.type === "checkbox") {
|
|
117
118
|
props.setter(state, element.checked);
|
|
118
119
|
} else {
|
|
@@ -121,7 +122,6 @@ function useStoreInput(store, props) {
|
|
|
121
122
|
}
|
|
122
123
|
});
|
|
123
124
|
return {
|
|
124
|
-
ref: inputProps.ref,
|
|
125
125
|
defaultValue: getDefaultValue(),
|
|
126
126
|
defaultChecked: getDefaultChecked(),
|
|
127
127
|
onChange: (event) => {
|
|
@@ -132,8 +132,8 @@ function useStoreInput(store, props) {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
// src/use_store_input_with_name.tsx
|
|
135
|
-
function useStoreInputWithName(store, props) {
|
|
136
|
-
const inputProps = useStoreInput(store, {
|
|
135
|
+
function useStoreInputWithName(ref, store, props) {
|
|
136
|
+
const inputProps = useStoreInput(ref, store, {
|
|
137
137
|
...props,
|
|
138
138
|
getter: (state) => {
|
|
139
139
|
if (props.getter) {
|