react-store-input 0.1.3 → 0.1.5

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/README.md CHANGED
@@ -14,29 +14,29 @@ This is a simple example of how to use this package.
14
14
  import { useFormStore } from "dn-react-input";
15
15
 
16
16
  export default function App() {
17
- const store = useFormStore({
18
- email: "",
19
- password: "",
20
- });
21
-
22
- const submit = async () => {
23
- const { email, password } = store.state;
24
-
25
- alert(`Email: ${email}\nPassword: ${password}`);
26
- };
27
-
28
- return (
29
- <form
30
- onSubmit={(e) => {
31
- e.preventDefault();
32
- submit();
33
- }}
34
- >
35
- <store.input name="email" type="email" />
36
- <store.input name="password" type="password" />
37
- <button type="submit">Submit</button>
38
- </form>
39
- );
17
+ const store = useFormStore({
18
+ email: "",
19
+ password: "",
20
+ });
21
+
22
+ const submit = async () => {
23
+ const { email, password } = store.state;
24
+
25
+ alert(`Email: ${email}\nPassword: ${password}`);
26
+ };
27
+
28
+ return (
29
+ <form
30
+ onSubmit={(e) => {
31
+ e.preventDefault();
32
+ submit();
33
+ }}
34
+ >
35
+ <store.input name="email" type="email" />
36
+ <store.input name="password" type="password" />
37
+ <button type="submit">Submit</button>
38
+ </form>
39
+ );
40
40
  }
41
41
  ```
42
42
 
@@ -96,20 +96,20 @@ function Component() {
96
96
  }
97
97
  ```
98
98
 
99
- If you want to avoid passing the store to each input component, use `useStoreInput`. This hook provides input components that are already connected to the store.
99
+ If you want to avoid passing the store to each input component, use `useStoreComponent`. This hook provides input components that are already connected to the store.
100
100
 
101
101
  ```tsx
102
- import { useStoreInput } from "dn-react-input";
102
+ import { useStoreComponent } from "dn-react-input";
103
103
 
104
104
  function Component() {
105
105
  ...
106
- const Input = useStoreInput(store);
106
+ const component = useStoreComponent(store);
107
107
 
108
108
  return (
109
109
  <form>
110
- <Input.input name="email" type="email" />
111
- <Input.input name="password" type="password" />
112
- <Input.input name="rememberMe" type="checkbox" />
110
+ <component.input name="email" type="email" />
111
+ <component.input name="password" type="password" />
112
+ <component.input name="rememberMe" type="checkbox" />
113
113
  </form>
114
114
  );
115
115
  }
@@ -169,6 +169,28 @@ function Component() {
169
169
  }
170
170
  ```
171
171
 
172
+ `Store.render` is a shortcut for `createRender` when you use `useFormStore`.
173
+
174
+ ```tsx
175
+ function Component() {
176
+ const store = useFormStore({
177
+ email: "",
178
+ password: "",
179
+ });
180
+
181
+ return (
182
+ <div>
183
+ {store.render((state) => (
184
+ <p>{state.email}</p>
185
+ ))}
186
+ {store.render((state) => (
187
+ <p>{state.password}</p>
188
+ ))}
189
+ </div>
190
+ );
191
+ }
192
+ ```
193
+
172
194
  ## How to subscribe to state changes?
173
195
 
174
196
  You can subscribe to state changes using the `subscribe` method of the store.
@@ -220,3 +242,44 @@ function Component() {
220
242
  return <button onClick={updateEmail}>Update Email</button>;
221
243
  }
222
244
  ```
245
+
246
+ ## How to create custom input components?
247
+
248
+ You can create custom input components using the `useStoreInput` hook. This hook provides the necessary props to connect your custom input component to the store: `name`, `value`, `defaultValue`, `defaultChecked`, `onChange`, and `ref` which already subscribed to the store.
249
+
250
+ ```tsx
251
+ import { useStoreInput } from "dn-react-input";
252
+
253
+ function CustomInput({ store }: { store: Store<{ email: string }> }) {
254
+ const inputProps = useStoreInput(store, {
255
+ name: "email",
256
+ });
257
+
258
+ return <input {...inputProps} />;
259
+ }
260
+ ```
261
+
262
+ ## How to creatre custom controller components?
263
+
264
+ If your custom component is not an html input element, you can use the `useStoreController` hook. This hook provides the necessary props to connect your custom controller component to the store: `ref`, `onSubscribe`, and `onDispatch`.
265
+
266
+ ```tsx
267
+ import { useStoreController } from "dn-react-input";
268
+
269
+ type State = {
270
+ count: number;
271
+ };
272
+
273
+ function CustomController({ store }: { store: Store<State> }) {
274
+ const controllerProps = useStoreController<HTMLDivElement, State>(store, {
275
+ onSubscribe: (state, element) => {
276
+ element.textContent = `Count: ${state.count}`;
277
+ },
278
+ onDispatch: (state, element) => {
279
+ state.count += Number(element.textContent.replace("Count: ", ""));
280
+ },
281
+ });
282
+
283
+ return <div {...controllerProps} />;
284
+ }
285
+ ```
package/dist/index.js CHANGED
@@ -121,18 +121,8 @@ function useSelector(store, selector, compare = (a, b) => a === b) {
121
121
  var import_react4 = require("react");
122
122
  function useStoreController(store, props) {
123
123
  const ref = (0, import_react4.useRef)(null);
124
+ (0, import_react4.useImperativeHandle)(props.ref, () => ref.current, []);
124
125
  const dispatchKey = (0, import_react4.useId)();
125
- (0, import_react4.useEffect)(() => {
126
- const element = ref.current;
127
- if (!element) {
128
- return;
129
- }
130
- if (typeof props.ref === "function") {
131
- props.ref(element);
132
- } else if (props.ref) {
133
- props.ref.current = element;
134
- }
135
- }, []);
136
126
  useSubscribe(store, (state, key) => {
137
127
  if (key === dispatchKey) {
138
128
  return;
@@ -153,7 +143,6 @@ function useStoreController(store, props) {
153
143
  );
154
144
  };
155
145
  return {
156
- dispatchKey,
157
146
  ref,
158
147
  onChange
159
148
  };
@@ -240,7 +229,6 @@ function useStoreInput(store, props) {
240
229
  });
241
230
  return {
242
231
  ref: inputProps.ref,
243
- dispatchKey: inputProps.dispatchKey,
244
232
  defaultValue: getDefaultValue(),
245
233
  defaultChecked: getDefaultChecked(),
246
234
  onChange: (event) => {
@@ -295,23 +283,29 @@ function useStoreComponent(store) {
295
283
  }
296
284
  function Input({
297
285
  store,
286
+ getter,
287
+ setter,
298
288
  ...props
299
289
  }) {
300
- const storeProps = useStoreInputWithName(store, props);
290
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
301
291
  return /* @__PURE__ */ import_react7.default.createElement("input", { ...props, ...storeProps });
302
292
  }
303
293
  function Select({
304
294
  store,
295
+ getter,
296
+ setter,
305
297
  ...props
306
298
  }) {
307
- const storeProps = useStoreInputWithName(store, props);
299
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
308
300
  return /* @__PURE__ */ import_react7.default.createElement("select", { ...props, ...storeProps });
309
301
  }
310
302
  function Textarea({
311
303
  store,
304
+ getter,
305
+ setter,
312
306
  ...props
313
307
  }) {
314
- const storeProps = useStoreInputWithName(store, props);
308
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
315
309
  return /* @__PURE__ */ import_react7.default.createElement("textarea", { ...props, ...storeProps });
316
310
  }
317
311
 
package/dist/index.mjs CHANGED
@@ -70,21 +70,11 @@ function useSelector(store, selector, compare = (a, b) => a === b) {
70
70
  }
71
71
 
72
72
  // src/use_store_controller.tsx
73
- import { useEffect as useEffect2, useId, useRef as useRef2 } from "react";
73
+ import { useId, useImperativeHandle, useRef as useRef2 } from "react";
74
74
  function useStoreController(store, props) {
75
75
  const ref = useRef2(null);
76
+ useImperativeHandle(props.ref, () => ref.current, []);
76
77
  const dispatchKey = useId();
77
- useEffect2(() => {
78
- const element = ref.current;
79
- if (!element) {
80
- return;
81
- }
82
- if (typeof props.ref === "function") {
83
- props.ref(element);
84
- } else if (props.ref) {
85
- props.ref.current = element;
86
- }
87
- }, []);
88
78
  useSubscribe(store, (state, key) => {
89
79
  if (key === dispatchKey) {
90
80
  return;
@@ -105,7 +95,6 @@ function useStoreController(store, props) {
105
95
  );
106
96
  };
107
97
  return {
108
- dispatchKey,
109
98
  ref,
110
99
  onChange
111
100
  };
@@ -192,7 +181,6 @@ function useStoreInput(store, props) {
192
181
  });
193
182
  return {
194
183
  ref: inputProps.ref,
195
- dispatchKey: inputProps.dispatchKey,
196
184
  defaultValue: getDefaultValue(),
197
185
  defaultChecked: getDefaultChecked(),
198
186
  onChange: (event) => {
@@ -249,23 +237,29 @@ function useStoreComponent(store) {
249
237
  }
250
238
  function Input({
251
239
  store,
240
+ getter,
241
+ setter,
252
242
  ...props
253
243
  }) {
254
- const storeProps = useStoreInputWithName(store, props);
244
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
255
245
  return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
256
246
  }
257
247
  function Select({
258
248
  store,
249
+ getter,
250
+ setter,
259
251
  ...props
260
252
  }) {
261
- const storeProps = useStoreInputWithName(store, props);
253
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
262
254
  return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
263
255
  }
264
256
  function Textarea({
265
257
  store,
258
+ getter,
259
+ setter,
266
260
  ...props
267
261
  }) {
268
- const storeProps = useStoreInputWithName(store, props);
262
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
269
263
  return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
270
264
  }
271
265
 
@@ -130,18 +130,8 @@ var import_date_fns = require("date-fns");
130
130
  var import_react5 = require("react");
131
131
  function useStoreController(store, props) {
132
132
  const ref = (0, import_react5.useRef)(null);
133
+ (0, import_react5.useImperativeHandle)(props.ref, () => ref.current, []);
133
134
  const dispatchKey = (0, import_react5.useId)();
134
- (0, import_react5.useEffect)(() => {
135
- const element = ref.current;
136
- if (!element) {
137
- return;
138
- }
139
- if (typeof props.ref === "function") {
140
- props.ref(element);
141
- } else if (props.ref) {
142
- props.ref.current = element;
143
- }
144
- }, []);
145
135
  useSubscribe(store, (state, key) => {
146
136
  if (key === dispatchKey) {
147
137
  return;
@@ -162,7 +152,6 @@ function useStoreController(store, props) {
162
152
  );
163
153
  };
164
154
  return {
165
- dispatchKey,
166
155
  ref,
167
156
  onChange
168
157
  };
@@ -247,7 +236,6 @@ function useStoreInput(store, props) {
247
236
  });
248
237
  return {
249
238
  ref: inputProps.ref,
250
- dispatchKey: inputProps.dispatchKey,
251
239
  defaultValue: getDefaultValue(),
252
240
  defaultChecked: getDefaultChecked(),
253
241
  onChange: (event) => {
@@ -300,23 +288,29 @@ function useStoreComponent(store) {
300
288
  }
301
289
  function Input({
302
290
  store,
291
+ getter,
292
+ setter,
303
293
  ...props
304
294
  }) {
305
- const storeProps = useStoreInputWithName(store, props);
295
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
306
296
  return /* @__PURE__ */ import_react8.default.createElement("input", { ...props, ...storeProps });
307
297
  }
308
298
  function Select({
309
299
  store,
300
+ getter,
301
+ setter,
310
302
  ...props
311
303
  }) {
312
- const storeProps = useStoreInputWithName(store, props);
304
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
313
305
  return /* @__PURE__ */ import_react8.default.createElement("select", { ...props, ...storeProps });
314
306
  }
315
307
  function Textarea({
316
308
  store,
309
+ getter,
310
+ setter,
317
311
  ...props
318
312
  }) {
319
- const storeProps = useStoreInputWithName(store, props);
313
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
320
314
  return /* @__PURE__ */ import_react8.default.createElement("textarea", { ...props, ...storeProps });
321
315
  }
322
316
 
@@ -93,21 +93,11 @@ import "react";
93
93
  import { format } from "date-fns";
94
94
 
95
95
  // src/use_store_controller.tsx
96
- import { useEffect as useEffect2, useId, useRef as useRef2 } from "react";
96
+ import { useId, useImperativeHandle, useRef as useRef2 } from "react";
97
97
  function useStoreController(store, props) {
98
98
  const ref = useRef2(null);
99
+ useImperativeHandle(props.ref, () => ref.current, []);
99
100
  const dispatchKey = useId();
100
- useEffect2(() => {
101
- const element = ref.current;
102
- if (!element) {
103
- return;
104
- }
105
- if (typeof props.ref === "function") {
106
- props.ref(element);
107
- } else if (props.ref) {
108
- props.ref.current = element;
109
- }
110
- }, []);
111
101
  useSubscribe(store, (state, key) => {
112
102
  if (key === dispatchKey) {
113
103
  return;
@@ -128,7 +118,6 @@ function useStoreController(store, props) {
128
118
  );
129
119
  };
130
120
  return {
131
- dispatchKey,
132
121
  ref,
133
122
  onChange
134
123
  };
@@ -213,7 +202,6 @@ function useStoreInput(store, props) {
213
202
  });
214
203
  return {
215
204
  ref: inputProps.ref,
216
- dispatchKey: inputProps.dispatchKey,
217
205
  defaultValue: getDefaultValue(),
218
206
  defaultChecked: getDefaultChecked(),
219
207
  onChange: (event) => {
@@ -266,23 +254,29 @@ function useStoreComponent(store) {
266
254
  }
267
255
  function Input({
268
256
  store,
257
+ getter,
258
+ setter,
269
259
  ...props
270
260
  }) {
271
- const storeProps = useStoreInputWithName(store, props);
261
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
272
262
  return /* @__PURE__ */ React2.createElement("input", { ...props, ...storeProps });
273
263
  }
274
264
  function Select({
275
265
  store,
266
+ getter,
267
+ setter,
276
268
  ...props
277
269
  }) {
278
- const storeProps = useStoreInputWithName(store, props);
270
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
279
271
  return /* @__PURE__ */ React2.createElement("select", { ...props, ...storeProps });
280
272
  }
281
273
  function Textarea({
282
274
  store,
275
+ getter,
276
+ setter,
283
277
  ...props
284
278
  }) {
285
- const storeProps = useStoreInputWithName(store, props);
279
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
286
280
  return /* @__PURE__ */ React2.createElement("textarea", { ...props, ...storeProps });
287
281
  }
288
282
 
@@ -14,8 +14,8 @@ declare function useStoreComponent<TState>(store: Store<TState>): {
14
14
  type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreComponentProps<TElement, TState> & {
15
15
  store: Store<TState>;
16
16
  };
17
- declare function Input<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
18
- declare function Select<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
19
- declare function Textarea<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
17
+ declare function Input<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
18
+ declare function Select<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
19
+ declare function Textarea<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
20
20
 
21
21
  export { Input, Select, type StoreComponentProps, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
@@ -14,8 +14,8 @@ declare function useStoreComponent<TState>(store: Store<TState>): {
14
14
  type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreComponentProps<TElement, TState> & {
15
15
  store: Store<TState>;
16
16
  };
17
- declare function Input<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
18
- declare function Select<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
19
- declare function Textarea<TState>({ store, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
17
+ declare function Input<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
18
+ declare function Select<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
19
+ declare function Textarea<TState>({ store, getter, setter, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
20
20
 
21
21
  export { Input, Select, type StoreComponentProps, type StoreComponentPropsWithStore, Textarea, useStoreComponent };
@@ -60,18 +60,8 @@ function useSubscribe(store, subscriber) {
60
60
  // src/use_store_controller.tsx
61
61
  function useStoreController(store, props) {
62
62
  const ref = (0, import_react2.useRef)(null);
63
+ (0, import_react2.useImperativeHandle)(props.ref, () => ref.current, []);
63
64
  const dispatchKey = (0, import_react2.useId)();
64
- (0, import_react2.useEffect)(() => {
65
- const element = ref.current;
66
- if (!element) {
67
- return;
68
- }
69
- if (typeof props.ref === "function") {
70
- props.ref(element);
71
- } else if (props.ref) {
72
- props.ref.current = element;
73
- }
74
- }, []);
75
65
  useSubscribe(store, (state, key) => {
76
66
  if (key === dispatchKey) {
77
67
  return;
@@ -92,7 +82,6 @@ function useStoreController(store, props) {
92
82
  );
93
83
  };
94
84
  return {
95
- dispatchKey,
96
85
  ref,
97
86
  onChange
98
87
  };
@@ -177,7 +166,6 @@ function useStoreInput(store, props) {
177
166
  });
178
167
  return {
179
168
  ref: inputProps.ref,
180
- dispatchKey: inputProps.dispatchKey,
181
169
  defaultValue: getDefaultValue(),
182
170
  defaultChecked: getDefaultChecked(),
183
171
  onChange: (event) => {
@@ -230,23 +218,29 @@ function useStoreComponent(store) {
230
218
  }
231
219
  function Input({
232
220
  store,
221
+ getter,
222
+ setter,
233
223
  ...props
234
224
  }) {
235
- const storeProps = useStoreInputWithName(store, props);
225
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
236
226
  return /* @__PURE__ */ import_react5.default.createElement("input", { ...props, ...storeProps });
237
227
  }
238
228
  function Select({
239
229
  store,
230
+ getter,
231
+ setter,
240
232
  ...props
241
233
  }) {
242
- const storeProps = useStoreInputWithName(store, props);
234
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
243
235
  return /* @__PURE__ */ import_react5.default.createElement("select", { ...props, ...storeProps });
244
236
  }
245
237
  function Textarea({
246
238
  store,
239
+ getter,
240
+ setter,
247
241
  ...props
248
242
  }) {
249
- const storeProps = useStoreInputWithName(store, props);
243
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
250
244
  return /* @__PURE__ */ import_react5.default.createElement("textarea", { ...props, ...storeProps });
251
245
  }
252
246
  // Annotate the CommonJS export names for ESM import in node:
@@ -9,7 +9,7 @@ import "react";
9
9
  import { format } from "date-fns";
10
10
 
11
11
  // src/use_store_controller.tsx
12
- import { useEffect as useEffect2, useId, useRef } from "react";
12
+ import { useId, useImperativeHandle, useRef } from "react";
13
13
 
14
14
  // src/use_subscribe.tsx
15
15
  import { useEffect } from "react";
@@ -25,18 +25,8 @@ function useSubscribe(store, subscriber) {
25
25
  // src/use_store_controller.tsx
26
26
  function useStoreController(store, props) {
27
27
  const ref = useRef(null);
28
+ useImperativeHandle(props.ref, () => ref.current, []);
28
29
  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
30
  useSubscribe(store, (state, key) => {
41
31
  if (key === dispatchKey) {
42
32
  return;
@@ -57,7 +47,6 @@ function useStoreController(store, props) {
57
47
  );
58
48
  };
59
49
  return {
60
- dispatchKey,
61
50
  ref,
62
51
  onChange
63
52
  };
@@ -142,7 +131,6 @@ function useStoreInput(store, props) {
142
131
  });
143
132
  return {
144
133
  ref: inputProps.ref,
145
- dispatchKey: inputProps.dispatchKey,
146
134
  defaultValue: getDefaultValue(),
147
135
  defaultChecked: getDefaultChecked(),
148
136
  onChange: (event) => {
@@ -195,23 +183,29 @@ function useStoreComponent(store) {
195
183
  }
196
184
  function Input({
197
185
  store,
186
+ getter,
187
+ setter,
198
188
  ...props
199
189
  }) {
200
- const storeProps = useStoreInputWithName(store, props);
190
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
201
191
  return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
202
192
  }
203
193
  function Select({
204
194
  store,
195
+ getter,
196
+ setter,
205
197
  ...props
206
198
  }) {
207
- const storeProps = useStoreInputWithName(store, props);
199
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
208
200
  return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
209
201
  }
210
202
  function Textarea({
211
203
  store,
204
+ getter,
205
+ setter,
212
206
  ...props
213
207
  }) {
214
- const storeProps = useStoreInputWithName(store, props);
208
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
215
209
  return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
216
210
  }
217
211
  export {
@@ -8,7 +8,6 @@ type StoreControllerProps<TController, TState> = {
8
8
  onDispatch: (state: TState, element: TController) => void;
9
9
  };
10
10
  declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
11
- dispatchKey: string;
12
11
  ref: React.RefObject<TController | null>;
13
12
  onChange: () => void;
14
13
  };
@@ -8,7 +8,6 @@ type StoreControllerProps<TController, TState> = {
8
8
  onDispatch: (state: TState, element: TController) => void;
9
9
  };
10
10
  declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
11
- dispatchKey: string;
12
11
  ref: React.RefObject<TController | null>;
13
12
  onChange: () => void;
14
13
  };
@@ -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;
@@ -71,7 +61,6 @@ function useStoreController(store, props) {
71
61
  );
72
62
  };
73
63
  return {
74
- dispatchKey,
75
64
  ref,
76
65
  onChange
77
66
  };
@@ -1,5 +1,5 @@
1
1
  // src/use_store_controller.tsx
2
- import { useEffect as useEffect2, useId, useRef } from "react";
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;
@@ -47,7 +37,6 @@ function useStoreController(store, props) {
47
37
  );
48
38
  };
49
39
  return {
50
- dispatchKey,
51
40
  ref,
52
41
  onChange
53
42
  };
@@ -15,7 +15,6 @@ type StoreInputProps<TInputElement, TState> = {
15
15
  };
16
16
  declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: StoreInputProps<TInputElement, TState>): {
17
17
  ref: React$1.RefObject<TInputElement | null>;
18
- dispatchKey: string;
19
18
  defaultValue: string | number | readonly string[] | undefined;
20
19
  defaultChecked: boolean | undefined;
21
20
  onChange: (event: React.ChangeEvent<TInputElement>) => void;
@@ -15,7 +15,6 @@ type StoreInputProps<TInputElement, TState> = {
15
15
  };
16
16
  declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: StoreInputProps<TInputElement, TState>): {
17
17
  ref: React$1.RefObject<TInputElement | null>;
18
- dispatchKey: string;
19
18
  defaultValue: string | number | readonly string[] | undefined;
20
19
  defaultChecked: boolean | undefined;
21
20
  onChange: (event: React.ChangeEvent<TInputElement>) => void;
@@ -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;
@@ -75,7 +65,6 @@ function useStoreController(store, props) {
75
65
  );
76
66
  };
77
67
  return {
78
- dispatchKey,
79
68
  ref,
80
69
  onChange
81
70
  };
@@ -160,7 +149,6 @@ function useStoreInput(store, props) {
160
149
  });
161
150
  return {
162
151
  ref: inputProps.ref,
163
- dispatchKey: inputProps.dispatchKey,
164
152
  defaultValue: getDefaultValue(),
165
153
  defaultChecked: getDefaultChecked(),
166
154
  onChange: (event) => {
@@ -3,7 +3,7 @@ import "react";
3
3
  import { format } from "date-fns";
4
4
 
5
5
  // src/use_store_controller.tsx
6
- import { useEffect as useEffect2, useId, useRef } from "react";
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;
@@ -51,7 +41,6 @@ function useStoreController(store, props) {
51
41
  );
52
42
  };
53
43
  return {
54
- dispatchKey,
55
44
  ref,
56
45
  onChange
57
46
  };
@@ -136,7 +125,6 @@ function useStoreInput(store, props) {
136
125
  });
137
126
  return {
138
127
  ref: inputProps.ref,
139
- dispatchKey: inputProps.dispatchKey,
140
128
  defaultValue: getDefaultValue(),
141
129
  defaultChecked: getDefaultChecked(),
142
130
  onChange: (event) => {
@@ -8,7 +8,6 @@ type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>,
8
8
  declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
9
9
  name: string | undefined;
10
10
  ref: React.RefObject<TInputElement | null>;
11
- dispatchKey: string;
12
11
  defaultValue: string | number | readonly string[] | undefined;
13
12
  defaultChecked: boolean | undefined;
14
13
  onChange: (event: React.ChangeEvent<TInputElement>) => void;
@@ -8,7 +8,6 @@ type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>,
8
8
  declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
9
9
  name: string | undefined;
10
10
  ref: React.RefObject<TInputElement | null>;
11
- dispatchKey: string;
12
11
  defaultValue: string | number | readonly string[] | undefined;
13
12
  defaultChecked: boolean | undefined;
14
13
  onChange: (event: React.ChangeEvent<TInputElement>) => void;
@@ -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;
@@ -77,7 +67,6 @@ function useStoreController(store, props) {
77
67
  );
78
68
  };
79
69
  return {
80
- dispatchKey,
81
70
  ref,
82
71
  onChange
83
72
  };
@@ -162,7 +151,6 @@ function useStoreInput(store, props) {
162
151
  });
163
152
  return {
164
153
  ref: inputProps.ref,
165
- dispatchKey: inputProps.dispatchKey,
166
154
  defaultValue: getDefaultValue(),
167
155
  defaultChecked: getDefaultChecked(),
168
156
  onChange: (event) => {
@@ -3,7 +3,7 @@ import "react";
3
3
  import { format } from "date-fns";
4
4
 
5
5
  // src/use_store_controller.tsx
6
- import { useEffect as useEffect2, useId, useRef } from "react";
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;
@@ -51,7 +41,6 @@ function useStoreController(store, props) {
51
41
  );
52
42
  };
53
43
  return {
54
- dispatchKey,
55
44
  ref,
56
45
  onChange
57
46
  };
@@ -136,7 +125,6 @@ function useStoreInput(store, props) {
136
125
  });
137
126
  return {
138
127
  ref: inputProps.ref,
139
- dispatchKey: inputProps.dispatchKey,
140
128
  defaultValue: getDefaultValue(),
141
129
  defaultChecked: getDefaultChecked(),
142
130
  onChange: (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-store-input",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "types": "./dist/index.d.ts",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.js",