react-store-input 0.1.7 → 0.1.9
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.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.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
package/dist/index.js
CHANGED
|
@@ -118,22 +118,17 @@ function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
|
118
118
|
// src/use_store_controller.tsx
|
|
119
119
|
var import_react4 = require("react");
|
|
120
120
|
function useStoreController(store, props) {
|
|
121
|
-
const ref = (0, import_react4.useRef)(null);
|
|
122
|
-
(0, import_react4.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
123
121
|
const dispatchKey = (0, import_react4.useId)();
|
|
124
122
|
useSubscribe(store, (state, key) => {
|
|
125
123
|
if (key === dispatchKey) {
|
|
126
124
|
return;
|
|
127
125
|
}
|
|
128
|
-
|
|
129
|
-
props.onSubscribe(state, ref.current);
|
|
126
|
+
props.onSubscribe(state);
|
|
130
127
|
});
|
|
131
128
|
const dispatch = () => {
|
|
132
129
|
store.dispatch(
|
|
133
130
|
(state) => {
|
|
134
|
-
|
|
135
|
-
if (!element) return;
|
|
136
|
-
props.onDispatch(state, element);
|
|
131
|
+
props.onDispatch(state);
|
|
137
132
|
},
|
|
138
133
|
{
|
|
139
134
|
key: dispatchKey
|
|
@@ -141,7 +136,6 @@ function useStoreController(store, props) {
|
|
|
141
136
|
);
|
|
142
137
|
};
|
|
143
138
|
return {
|
|
144
|
-
ref,
|
|
145
139
|
dispatch
|
|
146
140
|
};
|
|
147
141
|
}
|
|
@@ -149,7 +143,7 @@ function useStoreController(store, props) {
|
|
|
149
143
|
// src/use_store_input.tsx
|
|
150
144
|
var import_react5 = require("react");
|
|
151
145
|
var import_date_fns = require("date-fns");
|
|
152
|
-
function useStoreInput(store, props) {
|
|
146
|
+
function useStoreInput(ref, store, props) {
|
|
153
147
|
const toInputValue = props.toInputValue || ((value) => {
|
|
154
148
|
if (value === void 0 || value === null) {
|
|
155
149
|
return "";
|
|
@@ -196,8 +190,11 @@ function useStoreInput(store, props) {
|
|
|
196
190
|
return value;
|
|
197
191
|
});
|
|
198
192
|
const inputProps = useStoreController(store, {
|
|
199
|
-
|
|
200
|
-
|
|
193
|
+
onSubscribe: (state) => {
|
|
194
|
+
const element = ref.current;
|
|
195
|
+
if (!element) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
201
198
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
202
199
|
const checked = toInputChecked(props.getter(state));
|
|
203
200
|
if (element.checked === checked) {
|
|
@@ -214,7 +211,11 @@ function useStoreInput(store, props) {
|
|
|
214
211
|
const event = new Event("input", { bubbles: true });
|
|
215
212
|
element.dispatchEvent(event);
|
|
216
213
|
},
|
|
217
|
-
onDispatch: (state
|
|
214
|
+
onDispatch: (state) => {
|
|
215
|
+
const element = ref.current;
|
|
216
|
+
if (!element) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
218
219
|
if ("checked" in element && props.type === "checkbox") {
|
|
219
220
|
props.setter(state, element.checked);
|
|
220
221
|
} else {
|
|
@@ -223,7 +224,6 @@ function useStoreInput(store, props) {
|
|
|
223
224
|
}
|
|
224
225
|
});
|
|
225
226
|
return {
|
|
226
|
-
ref: inputProps.ref,
|
|
227
227
|
defaultValue: getDefaultValue(),
|
|
228
228
|
defaultChecked: getDefaultChecked(),
|
|
229
229
|
onChange: (event) => {
|
|
@@ -234,8 +234,8 @@ function useStoreInput(store, props) {
|
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
// src/use_store_input_with_name.tsx
|
|
237
|
-
function useStoreInputWithName(store, props) {
|
|
238
|
-
const inputProps = useStoreInput(store, {
|
|
237
|
+
function useStoreInputWithName(ref, store, props) {
|
|
238
|
+
const inputProps = useStoreInput(ref, store, {
|
|
239
239
|
...props,
|
|
240
240
|
getter: (state) => {
|
|
241
241
|
if (props.getter) {
|
|
@@ -293,7 +293,8 @@ function Input({
|
|
|
293
293
|
toStateValue,
|
|
294
294
|
...props
|
|
295
295
|
}) {
|
|
296
|
-
const
|
|
296
|
+
const ref = (0, import_react6.useRef)(null);
|
|
297
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
297
298
|
...props,
|
|
298
299
|
getter,
|
|
299
300
|
setter,
|
|
@@ -310,7 +311,8 @@ function Select({
|
|
|
310
311
|
toStateValue,
|
|
311
312
|
...props
|
|
312
313
|
}) {
|
|
313
|
-
const
|
|
314
|
+
const ref = (0, import_react6.useRef)(null);
|
|
315
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
314
316
|
...props,
|
|
315
317
|
getter,
|
|
316
318
|
setter,
|
|
@@ -327,7 +329,8 @@ function Textarea({
|
|
|
327
329
|
toStateValue,
|
|
328
330
|
...props
|
|
329
331
|
}) {
|
|
330
|
-
const
|
|
332
|
+
const ref = (0, import_react6.useRef)(null);
|
|
333
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
331
334
|
...props,
|
|
332
335
|
getter,
|
|
333
336
|
setter,
|
package/dist/index.mjs
CHANGED
|
@@ -70,24 +70,19 @@ function useSelector(store, selector, compare = (a, b) => a === b) {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
// src/use_store_controller.tsx
|
|
73
|
-
import { useId
|
|
73
|
+
import { useId } from "react";
|
|
74
74
|
function useStoreController(store, props) {
|
|
75
|
-
const ref = useRef2(null);
|
|
76
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
77
75
|
const dispatchKey = useId();
|
|
78
76
|
useSubscribe(store, (state, key) => {
|
|
79
77
|
if (key === dispatchKey) {
|
|
80
78
|
return;
|
|
81
79
|
}
|
|
82
|
-
|
|
83
|
-
props.onSubscribe(state, ref.current);
|
|
80
|
+
props.onSubscribe(state);
|
|
84
81
|
});
|
|
85
82
|
const dispatch = () => {
|
|
86
83
|
store.dispatch(
|
|
87
84
|
(state) => {
|
|
88
|
-
|
|
89
|
-
if (!element) return;
|
|
90
|
-
props.onDispatch(state, element);
|
|
85
|
+
props.onDispatch(state);
|
|
91
86
|
},
|
|
92
87
|
{
|
|
93
88
|
key: dispatchKey
|
|
@@ -95,7 +90,6 @@ function useStoreController(store, props) {
|
|
|
95
90
|
);
|
|
96
91
|
};
|
|
97
92
|
return {
|
|
98
|
-
ref,
|
|
99
93
|
dispatch
|
|
100
94
|
};
|
|
101
95
|
}
|
|
@@ -103,7 +97,7 @@ function useStoreController(store, props) {
|
|
|
103
97
|
// src/use_store_input.tsx
|
|
104
98
|
import "react";
|
|
105
99
|
import { format } from "date-fns";
|
|
106
|
-
function useStoreInput(store, props) {
|
|
100
|
+
function useStoreInput(ref, store, props) {
|
|
107
101
|
const toInputValue = props.toInputValue || ((value) => {
|
|
108
102
|
if (value === void 0 || value === null) {
|
|
109
103
|
return "";
|
|
@@ -150,8 +144,11 @@ function useStoreInput(store, props) {
|
|
|
150
144
|
return value;
|
|
151
145
|
});
|
|
152
146
|
const inputProps = useStoreController(store, {
|
|
153
|
-
|
|
154
|
-
|
|
147
|
+
onSubscribe: (state) => {
|
|
148
|
+
const element = ref.current;
|
|
149
|
+
if (!element) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
155
152
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
156
153
|
const checked = toInputChecked(props.getter(state));
|
|
157
154
|
if (element.checked === checked) {
|
|
@@ -168,7 +165,11 @@ function useStoreInput(store, props) {
|
|
|
168
165
|
const event = new Event("input", { bubbles: true });
|
|
169
166
|
element.dispatchEvent(event);
|
|
170
167
|
},
|
|
171
|
-
onDispatch: (state
|
|
168
|
+
onDispatch: (state) => {
|
|
169
|
+
const element = ref.current;
|
|
170
|
+
if (!element) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
172
173
|
if ("checked" in element && props.type === "checkbox") {
|
|
173
174
|
props.setter(state, element.checked);
|
|
174
175
|
} else {
|
|
@@ -177,7 +178,6 @@ function useStoreInput(store, props) {
|
|
|
177
178
|
}
|
|
178
179
|
});
|
|
179
180
|
return {
|
|
180
|
-
ref: inputProps.ref,
|
|
181
181
|
defaultValue: getDefaultValue(),
|
|
182
182
|
defaultChecked: getDefaultChecked(),
|
|
183
183
|
onChange: (event) => {
|
|
@@ -188,8 +188,8 @@ function useStoreInput(store, props) {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
// src/use_store_input_with_name.tsx
|
|
191
|
-
function useStoreInputWithName(store, props) {
|
|
192
|
-
const inputProps = useStoreInput(store, {
|
|
191
|
+
function useStoreInputWithName(ref, store, props) {
|
|
192
|
+
const inputProps = useStoreInput(ref, store, {
|
|
193
193
|
...props,
|
|
194
194
|
getter: (state) => {
|
|
195
195
|
if (props.getter) {
|
|
@@ -212,7 +212,7 @@ function useStoreInputWithName(store, props) {
|
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
// src/use_store_component.tsx
|
|
215
|
-
import { useCallback } from "react";
|
|
215
|
+
import { useCallback, useRef as useRef2 } from "react";
|
|
216
216
|
import React from "react";
|
|
217
217
|
function useStoreComponent(store) {
|
|
218
218
|
const input = useCallback(
|
|
@@ -247,7 +247,8 @@ function Input({
|
|
|
247
247
|
toStateValue,
|
|
248
248
|
...props
|
|
249
249
|
}) {
|
|
250
|
-
const
|
|
250
|
+
const ref = useRef2(null);
|
|
251
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
251
252
|
...props,
|
|
252
253
|
getter,
|
|
253
254
|
setter,
|
|
@@ -264,7 +265,8 @@ function Select({
|
|
|
264
265
|
toStateValue,
|
|
265
266
|
...props
|
|
266
267
|
}) {
|
|
267
|
-
const
|
|
268
|
+
const ref = useRef2(null);
|
|
269
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
268
270
|
...props,
|
|
269
271
|
getter,
|
|
270
272
|
setter,
|
|
@@ -281,7 +283,8 @@ function Textarea({
|
|
|
281
283
|
toStateValue,
|
|
282
284
|
...props
|
|
283
285
|
}) {
|
|
284
|
-
const
|
|
286
|
+
const ref = useRef2(null);
|
|
287
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
285
288
|
...props,
|
|
286
289
|
getter,
|
|
287
290
|
setter,
|
package/dist/use_form_store.js
CHANGED
|
@@ -129,22 +129,17 @@ var import_date_fns = require("date-fns");
|
|
|
129
129
|
// src/use_store_controller.tsx
|
|
130
130
|
var import_react5 = require("react");
|
|
131
131
|
function useStoreController(store, props) {
|
|
132
|
-
const ref = (0, import_react5.useRef)(null);
|
|
133
|
-
(0, import_react5.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
134
132
|
const dispatchKey = (0, import_react5.useId)();
|
|
135
133
|
useSubscribe(store, (state, key) => {
|
|
136
134
|
if (key === dispatchKey) {
|
|
137
135
|
return;
|
|
138
136
|
}
|
|
139
|
-
|
|
140
|
-
props.onSubscribe(state, ref.current);
|
|
137
|
+
props.onSubscribe(state);
|
|
141
138
|
});
|
|
142
139
|
const dispatch = () => {
|
|
143
140
|
store.dispatch(
|
|
144
141
|
(state) => {
|
|
145
|
-
|
|
146
|
-
if (!element) return;
|
|
147
|
-
props.onDispatch(state, element);
|
|
142
|
+
props.onDispatch(state);
|
|
148
143
|
},
|
|
149
144
|
{
|
|
150
145
|
key: dispatchKey
|
|
@@ -152,13 +147,12 @@ function useStoreController(store, props) {
|
|
|
152
147
|
);
|
|
153
148
|
};
|
|
154
149
|
return {
|
|
155
|
-
ref,
|
|
156
150
|
dispatch
|
|
157
151
|
};
|
|
158
152
|
}
|
|
159
153
|
|
|
160
154
|
// src/use_store_input.tsx
|
|
161
|
-
function useStoreInput(store, props) {
|
|
155
|
+
function useStoreInput(ref, store, props) {
|
|
162
156
|
const toInputValue = props.toInputValue || ((value) => {
|
|
163
157
|
if (value === void 0 || value === null) {
|
|
164
158
|
return "";
|
|
@@ -205,8 +199,11 @@ function useStoreInput(store, props) {
|
|
|
205
199
|
return value;
|
|
206
200
|
});
|
|
207
201
|
const inputProps = useStoreController(store, {
|
|
208
|
-
|
|
209
|
-
|
|
202
|
+
onSubscribe: (state) => {
|
|
203
|
+
const element = ref.current;
|
|
204
|
+
if (!element) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
210
207
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
211
208
|
const checked = toInputChecked(props.getter(state));
|
|
212
209
|
if (element.checked === checked) {
|
|
@@ -223,7 +220,11 @@ function useStoreInput(store, props) {
|
|
|
223
220
|
const event = new Event("input", { bubbles: true });
|
|
224
221
|
element.dispatchEvent(event);
|
|
225
222
|
},
|
|
226
|
-
onDispatch: (state
|
|
223
|
+
onDispatch: (state) => {
|
|
224
|
+
const element = ref.current;
|
|
225
|
+
if (!element) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
227
228
|
if ("checked" in element && props.type === "checkbox") {
|
|
228
229
|
props.setter(state, element.checked);
|
|
229
230
|
} else {
|
|
@@ -232,7 +233,6 @@ function useStoreInput(store, props) {
|
|
|
232
233
|
}
|
|
233
234
|
});
|
|
234
235
|
return {
|
|
235
|
-
ref: inputProps.ref,
|
|
236
236
|
defaultValue: getDefaultValue(),
|
|
237
237
|
defaultChecked: getDefaultChecked(),
|
|
238
238
|
onChange: (event) => {
|
|
@@ -243,8 +243,8 @@ function useStoreInput(store, props) {
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
245
|
// src/use_store_input_with_name.tsx
|
|
246
|
-
function useStoreInputWithName(store, props) {
|
|
247
|
-
const inputProps = useStoreInput(store, {
|
|
246
|
+
function useStoreInputWithName(ref, store, props) {
|
|
247
|
+
const inputProps = useStoreInput(ref, store, {
|
|
248
248
|
...props,
|
|
249
249
|
getter: (state) => {
|
|
250
250
|
if (props.getter) {
|
|
@@ -300,7 +300,8 @@ function Input({
|
|
|
300
300
|
toStateValue,
|
|
301
301
|
...props
|
|
302
302
|
}) {
|
|
303
|
-
const
|
|
303
|
+
const ref = (0, import_react7.useRef)(null);
|
|
304
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
304
305
|
...props,
|
|
305
306
|
getter,
|
|
306
307
|
setter,
|
|
@@ -317,7 +318,8 @@ function Select({
|
|
|
317
318
|
toStateValue,
|
|
318
319
|
...props
|
|
319
320
|
}) {
|
|
320
|
-
const
|
|
321
|
+
const ref = (0, import_react7.useRef)(null);
|
|
322
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
321
323
|
...props,
|
|
322
324
|
getter,
|
|
323
325
|
setter,
|
|
@@ -334,7 +336,8 @@ function Textarea({
|
|
|
334
336
|
toStateValue,
|
|
335
337
|
...props
|
|
336
338
|
}) {
|
|
337
|
-
const
|
|
339
|
+
const ref = (0, import_react7.useRef)(null);
|
|
340
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
338
341
|
...props,
|
|
339
342
|
getter,
|
|
340
343
|
setter,
|
package/dist/use_form_store.mjs
CHANGED
|
@@ -83,7 +83,7 @@ function useStore(initialState) {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// src/use_store_component.tsx
|
|
86
|
-
import { useCallback } from "react";
|
|
86
|
+
import { useCallback, useRef as useRef2 } from "react";
|
|
87
87
|
import React2 from "react";
|
|
88
88
|
|
|
89
89
|
// src/use_store_input.tsx
|
|
@@ -91,24 +91,19 @@ import "react";
|
|
|
91
91
|
import { format } from "date-fns";
|
|
92
92
|
|
|
93
93
|
// src/use_store_controller.tsx
|
|
94
|
-
import { useId
|
|
94
|
+
import { useId } from "react";
|
|
95
95
|
function useStoreController(store, props) {
|
|
96
|
-
const ref = useRef2(null);
|
|
97
|
-
useImperativeHandle(props.ref, () => ref.current, []);
|
|
98
96
|
const dispatchKey = useId();
|
|
99
97
|
useSubscribe(store, (state, key) => {
|
|
100
98
|
if (key === dispatchKey) {
|
|
101
99
|
return;
|
|
102
100
|
}
|
|
103
|
-
|
|
104
|
-
props.onSubscribe(state, ref.current);
|
|
101
|
+
props.onSubscribe(state);
|
|
105
102
|
});
|
|
106
103
|
const dispatch = () => {
|
|
107
104
|
store.dispatch(
|
|
108
105
|
(state) => {
|
|
109
|
-
|
|
110
|
-
if (!element) return;
|
|
111
|
-
props.onDispatch(state, element);
|
|
106
|
+
props.onDispatch(state);
|
|
112
107
|
},
|
|
113
108
|
{
|
|
114
109
|
key: dispatchKey
|
|
@@ -116,13 +111,12 @@ function useStoreController(store, props) {
|
|
|
116
111
|
);
|
|
117
112
|
};
|
|
118
113
|
return {
|
|
119
|
-
ref,
|
|
120
114
|
dispatch
|
|
121
115
|
};
|
|
122
116
|
}
|
|
123
117
|
|
|
124
118
|
// src/use_store_input.tsx
|
|
125
|
-
function useStoreInput(store, props) {
|
|
119
|
+
function useStoreInput(ref, store, props) {
|
|
126
120
|
const toInputValue = props.toInputValue || ((value) => {
|
|
127
121
|
if (value === void 0 || value === null) {
|
|
128
122
|
return "";
|
|
@@ -169,8 +163,11 @@ function useStoreInput(store, props) {
|
|
|
169
163
|
return value;
|
|
170
164
|
});
|
|
171
165
|
const inputProps = useStoreController(store, {
|
|
172
|
-
|
|
173
|
-
|
|
166
|
+
onSubscribe: (state) => {
|
|
167
|
+
const element = ref.current;
|
|
168
|
+
if (!element) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
174
171
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
175
172
|
const checked = toInputChecked(props.getter(state));
|
|
176
173
|
if (element.checked === checked) {
|
|
@@ -187,7 +184,11 @@ function useStoreInput(store, props) {
|
|
|
187
184
|
const event = new Event("input", { bubbles: true });
|
|
188
185
|
element.dispatchEvent(event);
|
|
189
186
|
},
|
|
190
|
-
onDispatch: (state
|
|
187
|
+
onDispatch: (state) => {
|
|
188
|
+
const element = ref.current;
|
|
189
|
+
if (!element) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
191
192
|
if ("checked" in element && props.type === "checkbox") {
|
|
192
193
|
props.setter(state, element.checked);
|
|
193
194
|
} else {
|
|
@@ -196,7 +197,6 @@ function useStoreInput(store, props) {
|
|
|
196
197
|
}
|
|
197
198
|
});
|
|
198
199
|
return {
|
|
199
|
-
ref: inputProps.ref,
|
|
200
200
|
defaultValue: getDefaultValue(),
|
|
201
201
|
defaultChecked: getDefaultChecked(),
|
|
202
202
|
onChange: (event) => {
|
|
@@ -207,8 +207,8 @@ function useStoreInput(store, props) {
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
// src/use_store_input_with_name.tsx
|
|
210
|
-
function useStoreInputWithName(store, props) {
|
|
211
|
-
const inputProps = useStoreInput(store, {
|
|
210
|
+
function useStoreInputWithName(ref, store, props) {
|
|
211
|
+
const inputProps = useStoreInput(ref, store, {
|
|
212
212
|
...props,
|
|
213
213
|
getter: (state) => {
|
|
214
214
|
if (props.getter) {
|
|
@@ -264,7 +264,8 @@ function Input({
|
|
|
264
264
|
toStateValue,
|
|
265
265
|
...props
|
|
266
266
|
}) {
|
|
267
|
-
const
|
|
267
|
+
const ref = useRef2(null);
|
|
268
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
268
269
|
...props,
|
|
269
270
|
getter,
|
|
270
271
|
setter,
|
|
@@ -281,7 +282,8 @@ function Select({
|
|
|
281
282
|
toStateValue,
|
|
282
283
|
...props
|
|
283
284
|
}) {
|
|
284
|
-
const
|
|
285
|
+
const ref = useRef2(null);
|
|
286
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
285
287
|
...props,
|
|
286
288
|
getter,
|
|
287
289
|
setter,
|
|
@@ -298,7 +300,8 @@ function Textarea({
|
|
|
298
300
|
toStateValue,
|
|
299
301
|
...props
|
|
300
302
|
}) {
|
|
301
|
-
const
|
|
303
|
+
const ref = useRef2(null);
|
|
304
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
302
305
|
...props,
|
|
303
306
|
getter,
|
|
304
307
|
setter,
|
|
@@ -59,22 +59,17 @@ function useSubscribe(store, subscriber) {
|
|
|
59
59
|
|
|
60
60
|
// src/use_store_controller.tsx
|
|
61
61
|
function useStoreController(store, props) {
|
|
62
|
-
const ref = (0, import_react2.useRef)(null);
|
|
63
|
-
(0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
|
|
64
62
|
const dispatchKey = (0, import_react2.useId)();
|
|
65
63
|
useSubscribe(store, (state, key) => {
|
|
66
64
|
if (key === dispatchKey) {
|
|
67
65
|
return;
|
|
68
66
|
}
|
|
69
|
-
|
|
70
|
-
props.onSubscribe(state, ref.current);
|
|
67
|
+
props.onSubscribe(state);
|
|
71
68
|
});
|
|
72
69
|
const dispatch = () => {
|
|
73
70
|
store.dispatch(
|
|
74
71
|
(state) => {
|
|
75
|
-
|
|
76
|
-
if (!element) return;
|
|
77
|
-
props.onDispatch(state, element);
|
|
72
|
+
props.onDispatch(state);
|
|
78
73
|
},
|
|
79
74
|
{
|
|
80
75
|
key: dispatchKey
|
|
@@ -82,13 +77,12 @@ function useStoreController(store, props) {
|
|
|
82
77
|
);
|
|
83
78
|
};
|
|
84
79
|
return {
|
|
85
|
-
ref,
|
|
86
80
|
dispatch
|
|
87
81
|
};
|
|
88
82
|
}
|
|
89
83
|
|
|
90
84
|
// src/use_store_input.tsx
|
|
91
|
-
function useStoreInput(store, props) {
|
|
85
|
+
function useStoreInput(ref, store, props) {
|
|
92
86
|
const toInputValue = props.toInputValue || ((value) => {
|
|
93
87
|
if (value === void 0 || value === null) {
|
|
94
88
|
return "";
|
|
@@ -135,8 +129,11 @@ function useStoreInput(store, props) {
|
|
|
135
129
|
return value;
|
|
136
130
|
});
|
|
137
131
|
const inputProps = useStoreController(store, {
|
|
138
|
-
|
|
139
|
-
|
|
132
|
+
onSubscribe: (state) => {
|
|
133
|
+
const element = ref.current;
|
|
134
|
+
if (!element) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
140
137
|
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
141
138
|
const checked = toInputChecked(props.getter(state));
|
|
142
139
|
if (element.checked === checked) {
|
|
@@ -153,7 +150,11 @@ function useStoreInput(store, props) {
|
|
|
153
150
|
const event = new Event("input", { bubbles: true });
|
|
154
151
|
element.dispatchEvent(event);
|
|
155
152
|
},
|
|
156
|
-
onDispatch: (state
|
|
153
|
+
onDispatch: (state) => {
|
|
154
|
+
const element = ref.current;
|
|
155
|
+
if (!element) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
157
158
|
if ("checked" in element && props.type === "checkbox") {
|
|
158
159
|
props.setter(state, element.checked);
|
|
159
160
|
} else {
|
|
@@ -162,7 +163,6 @@ function useStoreInput(store, props) {
|
|
|
162
163
|
}
|
|
163
164
|
});
|
|
164
165
|
return {
|
|
165
|
-
ref: inputProps.ref,
|
|
166
166
|
defaultValue: getDefaultValue(),
|
|
167
167
|
defaultChecked: getDefaultChecked(),
|
|
168
168
|
onChange: (event) => {
|
|
@@ -173,8 +173,8 @@ function useStoreInput(store, props) {
|
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
// src/use_store_input_with_name.tsx
|
|
176
|
-
function useStoreInputWithName(store, props) {
|
|
177
|
-
const inputProps = useStoreInput(store, {
|
|
176
|
+
function useStoreInputWithName(ref, store, props) {
|
|
177
|
+
const inputProps = useStoreInput(ref, store, {
|
|
178
178
|
...props,
|
|
179
179
|
getter: (state) => {
|
|
180
180
|
if (props.getter) {
|
|
@@ -230,7 +230,8 @@ function Input({
|
|
|
230
230
|
toStateValue,
|
|
231
231
|
...props
|
|
232
232
|
}) {
|
|
233
|
-
const
|
|
233
|
+
const ref = (0, import_react4.useRef)(null);
|
|
234
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
234
235
|
...props,
|
|
235
236
|
getter,
|
|
236
237
|
setter,
|
|
@@ -247,7 +248,8 @@ function Select({
|
|
|
247
248
|
toStateValue,
|
|
248
249
|
...props
|
|
249
250
|
}) {
|
|
250
|
-
const
|
|
251
|
+
const ref = (0, import_react4.useRef)(null);
|
|
252
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
251
253
|
...props,
|
|
252
254
|
getter,
|
|
253
255
|
setter,
|
|
@@ -264,7 +266,8 @@ function Textarea({
|
|
|
264
266
|
toStateValue,
|
|
265
267
|
...props
|
|
266
268
|
}) {
|
|
267
|
-
const
|
|
269
|
+
const ref = (0, import_react4.useRef)(null);
|
|
270
|
+
const storeProps = useStoreInputWithName(ref, store, {
|
|
268
271
|
...props,
|
|
269
272
|
getter,
|
|
270
273
|
setter,
|
|
@@ -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) {
|