react-store-input 0.1.5 → 0.1.7

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 CHANGED
@@ -2,10 +2,9 @@ export { Store, StoreSubscriber, useStore } from './use_store.mjs';
2
2
  export { useSelector } from './use_selector.mjs';
3
3
  export { useSubscribe } from './use_subscribe.mjs';
4
4
  export { StoreControllerProps, useStoreController } from './use_store_controller.mjs';
5
- export { useStoreInputWithName } from './use_store_input_with_name.mjs';
5
+ export { InferNameFromProps, StoreInputWithNameProps, useStoreInputWithName } from './use_store_input_with_name.mjs';
6
6
  export { StoreInputProps, useStoreInput } from './use_store_input.mjs';
7
- export { Input, Select, StoreComponentProps, StoreComponentPropsWithStore, Textarea, useStoreComponent } from './use_store_component.mjs';
7
+ export { Input, Select, StoreComponentPropsWithStore, StoreInputWithNameComponentProps, Textarea, useStoreComponent } from './use_store_component.mjs';
8
8
  export { FormStore, useFormStore } from './use_form_store.mjs';
9
- export { deserialize, serialize } from './serialize.mjs';
10
9
  import 'react';
11
10
  import './create_render.mjs';
package/dist/index.d.ts CHANGED
@@ -2,10 +2,9 @@ export { Store, StoreSubscriber, useStore } from './use_store.js';
2
2
  export { useSelector } from './use_selector.js';
3
3
  export { useSubscribe } from './use_subscribe.js';
4
4
  export { StoreControllerProps, useStoreController } from './use_store_controller.js';
5
- export { useStoreInputWithName } from './use_store_input_with_name.js';
5
+ export { InferNameFromProps, StoreInputWithNameProps, useStoreInputWithName } from './use_store_input_with_name.js';
6
6
  export { StoreInputProps, useStoreInput } from './use_store_input.js';
7
- export { Input, Select, StoreComponentProps, StoreComponentPropsWithStore, Textarea, useStoreComponent } from './use_store_component.js';
7
+ export { Input, Select, StoreComponentPropsWithStore, StoreInputWithNameComponentProps, Textarea, useStoreComponent } from './use_store_component.js';
8
8
  export { FormStore, useFormStore } from './use_form_store.js';
9
- export { deserialize, serialize } from './serialize.js';
10
9
  import 'react';
11
10
  import './create_render.js';
package/dist/index.js CHANGED
@@ -33,8 +33,6 @@ __export(index_exports, {
33
33
  Input: () => Input,
34
34
  Select: () => Select,
35
35
  Textarea: () => Textarea,
36
- deserialize: () => deserialize,
37
- serialize: () => serialize,
38
36
  useFormStore: () => useFormStore,
39
37
  useSelector: () => useSelector,
40
38
  useStore: () => useStore,
@@ -130,7 +128,7 @@ function useStoreController(store, props) {
130
128
  if (!ref.current) return;
131
129
  props.onSubscribe(state, ref.current);
132
130
  });
133
- const onChange = () => {
131
+ const dispatch = () => {
134
132
  store.dispatch(
135
133
  (state) => {
136
134
  const element = ref.current;
@@ -144,7 +142,7 @@ function useStoreController(store, props) {
144
142
  };
145
143
  return {
146
144
  ref,
147
- onChange
145
+ dispatch
148
146
  };
149
147
  }
150
148
 
@@ -152,20 +150,18 @@ function useStoreController(store, props) {
152
150
  var import_react5 = require("react");
153
151
  var import_date_fns = require("date-fns");
154
152
  function useStoreInput(store, props) {
155
- const toInputValue = (value) => {
153
+ const toInputValue = props.toInputValue || ((value) => {
156
154
  if (value === void 0 || value === null) {
157
155
  return "";
158
156
  }
159
157
  if (props.type === "datetime-local") {
160
- if (value instanceof Date) {
161
- return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
162
- }
163
- if (typeof value === "string") {
164
- return toInputValue(new Date(value));
158
+ const date = new Date(value);
159
+ if (!isNaN(date.getTime())) {
160
+ return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
165
161
  }
166
162
  }
167
163
  return String(value);
168
- };
164
+ });
169
165
  const getDefaultValue = () => {
170
166
  if (props.defaultValue !== void 0) {
171
167
  return props.defaultValue;
@@ -190,16 +186,15 @@ function useStoreInput(store, props) {
190
186
  }
191
187
  return toInputChecked(props.getter(store.state));
192
188
  };
193
- function toStateValue(value) {
194
- const selected = props.getter(store.state);
195
- if (typeof selected === "number") {
189
+ const toStateValue = props.toStateValue || ((value) => {
190
+ if (props.type === "number" || props.type === "range") {
196
191
  return Number(value);
197
192
  }
198
- if (selected instanceof Date) {
193
+ if (props.type === "datetime-local") {
199
194
  return new Date(value);
200
195
  }
201
196
  return value;
202
- }
197
+ });
203
198
  const inputProps = useStoreController(store, {
204
199
  ref: props.ref,
205
200
  onSubscribe: (state, element) => {
@@ -232,7 +227,7 @@ function useStoreInput(store, props) {
232
227
  defaultValue: getDefaultValue(),
233
228
  defaultChecked: getDefaultChecked(),
234
229
  onChange: (event) => {
235
- inputProps.onChange();
230
+ inputProps.dispatch();
236
231
  props.onChange?.(event);
237
232
  }
238
233
  };
@@ -266,15 +261,24 @@ function useStoreInputWithName(store, props) {
266
261
  var import_react6 = require("react");
267
262
  var import_react7 = __toESM(require("react"));
268
263
  function useStoreComponent(store) {
269
- const input = (0, import_react6.useCallback)(function Component(props) {
270
- return /* @__PURE__ */ import_react7.default.createElement(Input, { store, ...props });
271
- }, []);
272
- const select = (0, import_react6.useCallback)(function Component(props) {
273
- return /* @__PURE__ */ import_react7.default.createElement(Select, { store, ...props });
274
- }, []);
275
- const textarea = (0, import_react6.useCallback)(function Component(props) {
276
- return /* @__PURE__ */ import_react7.default.createElement(Textarea, { store, ...props });
277
- }, []);
264
+ const input = (0, import_react6.useCallback)(
265
+ function Component(props) {
266
+ return /* @__PURE__ */ import_react7.default.createElement(Input, { store, ...props });
267
+ },
268
+ []
269
+ );
270
+ const select = (0, import_react6.useCallback)(
271
+ function Component(props) {
272
+ return /* @__PURE__ */ import_react7.default.createElement(Select, { store, ...props });
273
+ },
274
+ []
275
+ );
276
+ const textarea = (0, import_react6.useCallback)(
277
+ function Component(props) {
278
+ return /* @__PURE__ */ import_react7.default.createElement(Textarea, { store, ...props });
279
+ },
280
+ []
281
+ );
278
282
  return {
279
283
  input,
280
284
  select,
@@ -285,27 +289,51 @@ function Input({
285
289
  store,
286
290
  getter,
287
291
  setter,
292
+ toInputValue,
293
+ toStateValue,
288
294
  ...props
289
295
  }) {
290
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
296
+ const storeProps = useStoreInputWithName(store, {
297
+ ...props,
298
+ getter,
299
+ setter,
300
+ toInputValue,
301
+ toStateValue
302
+ });
291
303
  return /* @__PURE__ */ import_react7.default.createElement("input", { ...props, ...storeProps });
292
304
  }
293
305
  function Select({
294
306
  store,
295
307
  getter,
296
308
  setter,
309
+ toInputValue,
310
+ toStateValue,
297
311
  ...props
298
312
  }) {
299
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
313
+ const storeProps = useStoreInputWithName(store, {
314
+ ...props,
315
+ getter,
316
+ setter,
317
+ toInputValue,
318
+ toStateValue
319
+ });
300
320
  return /* @__PURE__ */ import_react7.default.createElement("select", { ...props, ...storeProps });
301
321
  }
302
322
  function Textarea({
303
323
  store,
304
324
  getter,
305
325
  setter,
326
+ toInputValue,
327
+ toStateValue,
306
328
  ...props
307
329
  }) {
308
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
330
+ const storeProps = useStoreInputWithName(store, {
331
+ ...props,
332
+ getter,
333
+ setter,
334
+ toInputValue,
335
+ toStateValue
336
+ });
309
337
  return /* @__PURE__ */ import_react7.default.createElement("textarea", { ...props, ...storeProps });
310
338
  }
311
339
 
@@ -336,100 +364,11 @@ function useFormStore(initialState) {
336
364
  ...storeComponent
337
365
  };
338
366
  }
339
-
340
- // src/serialize.ts
341
- function serialize(value) {
342
- if (value === void 0) {
343
- return void 0;
344
- }
345
- if (value === null) {
346
- return {
347
- type: "null",
348
- value: null
349
- };
350
- }
351
- if (typeof value === "string") {
352
- return {
353
- type: "string",
354
- value
355
- };
356
- }
357
- if (typeof value === "number") {
358
- return {
359
- type: "number",
360
- value
361
- };
362
- }
363
- if (typeof value === "boolean") {
364
- return {
365
- type: "boolean",
366
- value
367
- };
368
- }
369
- if (value instanceof Date) {
370
- return {
371
- type: "date",
372
- value: value.toISOString()
373
- };
374
- }
375
- if (Array.isArray(value)) {
376
- return {
377
- type: "array",
378
- value: value.map((item) => serialize(item))
379
- };
380
- }
381
- if (typeof value === "object") {
382
- return {
383
- type: "object",
384
- value: Object.entries(value).reduce((acc, [key, value2]) => {
385
- return {
386
- ...acc,
387
- [key]: serialize(value2)
388
- };
389
- }, {})
390
- };
391
- }
392
- return void 0;
393
- }
394
- function deserialize(data) {
395
- if (data === void 0) {
396
- return void 0;
397
- }
398
- if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
399
- const { type, value } = data;
400
- switch (type) {
401
- case "null":
402
- return null;
403
- case "string":
404
- return value;
405
- case "number":
406
- return value;
407
- case "boolean":
408
- return value;
409
- case "date":
410
- return new Date(value);
411
- case "array":
412
- return value.map((item) => deserialize(item));
413
- case "object":
414
- return Object.entries(value).reduce((acc, [key, value2]) => {
415
- return {
416
- ...acc,
417
- [key]: deserialize(value2)
418
- };
419
- }, {});
420
- default:
421
- return void 0;
422
- }
423
- }
424
- return void 0;
425
- }
426
367
  // Annotate the CommonJS export names for ESM import in node:
427
368
  0 && (module.exports = {
428
369
  Input,
429
370
  Select,
430
371
  Textarea,
431
- deserialize,
432
- serialize,
433
372
  useFormStore,
434
373
  useSelector,
435
374
  useStore,
package/dist/index.mjs CHANGED
@@ -82,7 +82,7 @@ function useStoreController(store, props) {
82
82
  if (!ref.current) return;
83
83
  props.onSubscribe(state, ref.current);
84
84
  });
85
- const onChange = () => {
85
+ const dispatch = () => {
86
86
  store.dispatch(
87
87
  (state) => {
88
88
  const element = ref.current;
@@ -96,7 +96,7 @@ function useStoreController(store, props) {
96
96
  };
97
97
  return {
98
98
  ref,
99
- onChange
99
+ dispatch
100
100
  };
101
101
  }
102
102
 
@@ -104,20 +104,18 @@ function useStoreController(store, props) {
104
104
  import "react";
105
105
  import { format } from "date-fns";
106
106
  function useStoreInput(store, props) {
107
- const toInputValue = (value) => {
107
+ const toInputValue = props.toInputValue || ((value) => {
108
108
  if (value === void 0 || value === null) {
109
109
  return "";
110
110
  }
111
111
  if (props.type === "datetime-local") {
112
- if (value instanceof Date) {
113
- return format(value, "yyyy-MM-dd'T'HH:mm:ss");
114
- }
115
- if (typeof value === "string") {
116
- return toInputValue(new Date(value));
112
+ const date = new Date(value);
113
+ if (!isNaN(date.getTime())) {
114
+ return format(date, "yyyy-MM-dd'T'HH:mm:ss");
117
115
  }
118
116
  }
119
117
  return String(value);
120
- };
118
+ });
121
119
  const getDefaultValue = () => {
122
120
  if (props.defaultValue !== void 0) {
123
121
  return props.defaultValue;
@@ -142,16 +140,15 @@ function useStoreInput(store, props) {
142
140
  }
143
141
  return toInputChecked(props.getter(store.state));
144
142
  };
145
- function toStateValue(value) {
146
- const selected = props.getter(store.state);
147
- if (typeof selected === "number") {
143
+ const toStateValue = props.toStateValue || ((value) => {
144
+ if (props.type === "number" || props.type === "range") {
148
145
  return Number(value);
149
146
  }
150
- if (selected instanceof Date) {
147
+ if (props.type === "datetime-local") {
151
148
  return new Date(value);
152
149
  }
153
150
  return value;
154
- }
151
+ });
155
152
  const inputProps = useStoreController(store, {
156
153
  ref: props.ref,
157
154
  onSubscribe: (state, element) => {
@@ -184,7 +181,7 @@ function useStoreInput(store, props) {
184
181
  defaultValue: getDefaultValue(),
185
182
  defaultChecked: getDefaultChecked(),
186
183
  onChange: (event) => {
187
- inputProps.onChange();
184
+ inputProps.dispatch();
188
185
  props.onChange?.(event);
189
186
  }
190
187
  };
@@ -215,20 +212,27 @@ function useStoreInputWithName(store, props) {
215
212
  }
216
213
 
217
214
  // src/use_store_component.tsx
218
- import {
219
- useCallback
220
- } from "react";
215
+ import { useCallback } from "react";
221
216
  import React from "react";
222
217
  function useStoreComponent(store) {
223
- const input = useCallback(function Component(props) {
224
- return /* @__PURE__ */ React.createElement(Input, { store, ...props });
225
- }, []);
226
- const select = useCallback(function Component(props) {
227
- return /* @__PURE__ */ React.createElement(Select, { store, ...props });
228
- }, []);
229
- const textarea = useCallback(function Component(props) {
230
- return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
231
- }, []);
218
+ const input = useCallback(
219
+ function Component(props) {
220
+ return /* @__PURE__ */ React.createElement(Input, { store, ...props });
221
+ },
222
+ []
223
+ );
224
+ const select = useCallback(
225
+ function Component(props) {
226
+ return /* @__PURE__ */ React.createElement(Select, { store, ...props });
227
+ },
228
+ []
229
+ );
230
+ const textarea = useCallback(
231
+ function Component(props) {
232
+ return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
233
+ },
234
+ []
235
+ );
232
236
  return {
233
237
  input,
234
238
  select,
@@ -239,27 +243,51 @@ function Input({
239
243
  store,
240
244
  getter,
241
245
  setter,
246
+ toInputValue,
247
+ toStateValue,
242
248
  ...props
243
249
  }) {
244
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
250
+ const storeProps = useStoreInputWithName(store, {
251
+ ...props,
252
+ getter,
253
+ setter,
254
+ toInputValue,
255
+ toStateValue
256
+ });
245
257
  return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
246
258
  }
247
259
  function Select({
248
260
  store,
249
261
  getter,
250
262
  setter,
263
+ toInputValue,
264
+ toStateValue,
251
265
  ...props
252
266
  }) {
253
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
267
+ const storeProps = useStoreInputWithName(store, {
268
+ ...props,
269
+ getter,
270
+ setter,
271
+ toInputValue,
272
+ toStateValue
273
+ });
254
274
  return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
255
275
  }
256
276
  function Textarea({
257
277
  store,
258
278
  getter,
259
279
  setter,
280
+ toInputValue,
281
+ toStateValue,
260
282
  ...props
261
283
  }) {
262
- const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
284
+ const storeProps = useStoreInputWithName(store, {
285
+ ...props,
286
+ getter,
287
+ setter,
288
+ toInputValue,
289
+ toStateValue
290
+ });
263
291
  return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
264
292
  }
265
293
 
@@ -290,99 +318,10 @@ function useFormStore(initialState) {
290
318
  ...storeComponent
291
319
  };
292
320
  }
293
-
294
- // src/serialize.ts
295
- function serialize(value) {
296
- if (value === void 0) {
297
- return void 0;
298
- }
299
- if (value === null) {
300
- return {
301
- type: "null",
302
- value: null
303
- };
304
- }
305
- if (typeof value === "string") {
306
- return {
307
- type: "string",
308
- value
309
- };
310
- }
311
- if (typeof value === "number") {
312
- return {
313
- type: "number",
314
- value
315
- };
316
- }
317
- if (typeof value === "boolean") {
318
- return {
319
- type: "boolean",
320
- value
321
- };
322
- }
323
- if (value instanceof Date) {
324
- return {
325
- type: "date",
326
- value: value.toISOString()
327
- };
328
- }
329
- if (Array.isArray(value)) {
330
- return {
331
- type: "array",
332
- value: value.map((item) => serialize(item))
333
- };
334
- }
335
- if (typeof value === "object") {
336
- return {
337
- type: "object",
338
- value: Object.entries(value).reduce((acc, [key, value2]) => {
339
- return {
340
- ...acc,
341
- [key]: serialize(value2)
342
- };
343
- }, {})
344
- };
345
- }
346
- return void 0;
347
- }
348
- function deserialize(data) {
349
- if (data === void 0) {
350
- return void 0;
351
- }
352
- if (typeof data === "object" && data !== null && "type" in data && "value" in data) {
353
- const { type, value } = data;
354
- switch (type) {
355
- case "null":
356
- return null;
357
- case "string":
358
- return value;
359
- case "number":
360
- return value;
361
- case "boolean":
362
- return value;
363
- case "date":
364
- return new Date(value);
365
- case "array":
366
- return value.map((item) => deserialize(item));
367
- case "object":
368
- return Object.entries(value).reduce((acc, [key, value2]) => {
369
- return {
370
- ...acc,
371
- [key]: deserialize(value2)
372
- };
373
- }, {});
374
- default:
375
- return void 0;
376
- }
377
- }
378
- return void 0;
379
- }
380
321
  export {
381
322
  Input,
382
323
  Select,
383
324
  Textarea,
384
- deserialize,
385
- serialize,
386
325
  useFormStore,
387
326
  useSelector,
388
327
  useStore,
@@ -2,6 +2,8 @@ import { CreateRender } from './create_render.mjs';
2
2
  import { Store } from './use_store.mjs';
3
3
  import { useStoreComponent } from './use_store_component.mjs';
4
4
  import 'react';
5
+ import './use_store_input_with_name.mjs';
6
+ import './use_store_input.mjs';
5
7
 
6
8
  type FormStore<TState> = Store<TState> & ReturnType<typeof useStoreComponent<TState>> & {
7
9
  render: CreateRender<TState>;
@@ -2,6 +2,8 @@ import { CreateRender } from './create_render.js';
2
2
  import { Store } from './use_store.js';
3
3
  import { useStoreComponent } from './use_store_component.js';
4
4
  import 'react';
5
+ import './use_store_input_with_name.js';
6
+ import './use_store_input.js';
5
7
 
6
8
  type FormStore<TState> = Store<TState> & ReturnType<typeof useStoreComponent<TState>> & {
7
9
  render: CreateRender<TState>;