react-store-input 0.1.0 → 0.1.3

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,32 +15,74 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/use_store_input.tsx
31
21
  var use_store_input_exports = {};
32
22
  __export(use_store_input_exports, {
33
- Input: () => Input,
34
- Select: () => Select,
35
- Textarea: () => Textarea,
36
23
  useStoreInput: () => useStoreInput
37
24
  });
38
25
  module.exports = __toCommonJS(use_store_input_exports);
26
+ var import_react3 = require("react");
27
+ var import_date_fns = require("date-fns");
28
+
29
+ // src/use_store_controller.tsx
39
30
  var import_react2 = require("react");
40
- var import_react3 = __toESM(require("react"));
41
31
 
42
- // src/use_store_input_props.tsx
32
+ // src/use_subscribe.tsx
43
33
  var import_react = require("react");
44
- var import_date_fns = require("date-fns");
45
- function useStoreInputProps(store, props) {
34
+ function useSubscribe(store, subscriber) {
35
+ (0, import_react.useEffect)(() => {
36
+ const unsubscribe = store.subscribe(subscriber);
37
+ return () => {
38
+ unsubscribe();
39
+ };
40
+ }, [store]);
41
+ }
42
+
43
+ // src/use_store_controller.tsx
44
+ function useStoreController(store, props) {
45
+ const ref = (0, import_react2.useRef)(null);
46
+ 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
+ useSubscribe(store, (state, key) => {
59
+ if (key === dispatchKey) {
60
+ return;
61
+ }
62
+ if (!ref.current) return;
63
+ props.onSubscribe(state, ref.current);
64
+ });
65
+ const onChange = () => {
66
+ store.dispatch(
67
+ (state) => {
68
+ const element = ref.current;
69
+ if (!element) return;
70
+ props.onDispatch(state, element);
71
+ },
72
+ {
73
+ key: dispatchKey
74
+ }
75
+ );
76
+ };
77
+ return {
78
+ dispatchKey,
79
+ ref,
80
+ onChange
81
+ };
82
+ }
83
+
84
+ // src/use_store_input.tsx
85
+ function useStoreInput(store, props) {
46
86
  const toInputValue = (value) => {
47
87
  if (value === void 0 || value === null) {
48
88
  return "";
@@ -64,7 +104,7 @@ function useStoreInputProps(store, props) {
64
104
  if (props.type === "checkbox" || props.type === "radio") {
65
105
  return void 0;
66
106
  }
67
- return toInputValue(store.state[props.name]);
107
+ return toInputValue(props.getter(store.state));
68
108
  };
69
109
  const toInputChecked = (value) => {
70
110
  if (props.type === "radio") {
@@ -79,121 +119,57 @@ function useStoreInputProps(store, props) {
79
119
  if (props.type !== "checkbox" && props.type !== "radio") {
80
120
  return void 0;
81
121
  }
82
- return toInputChecked(store.state[props.name]);
122
+ return toInputChecked(props.getter(store.state));
83
123
  };
84
- function useSubscriptionRef() {
85
- const ref2 = (0, import_react.useRef)(null);
86
- (0, import_react.useEffect)(() => {
87
- return store.subscribe((state, key) => {
88
- if (key === dispatchKey) {
89
- return;
90
- }
91
- const input = ref2.current;
92
- if (!input) {
93
- return;
94
- }
95
- if (props.type === "checkbox" || props.type === "radio") {
96
- const checked = toInputChecked(state[props.name]);
97
- if (input.checked === checked) {
98
- return;
99
- }
100
- input.checked = checked;
101
- } else {
102
- const value = toInputValue(state[props.name]);
103
- if (input.value === value) {
104
- return;
105
- }
106
- input.value = value;
107
- }
108
- const event = new Event("input", { bubbles: true });
109
- input.dispatchEvent(event);
110
- });
111
- }, []);
112
- return ref2;
113
- }
114
124
  function toStateValue(value) {
115
- if (typeof store.state[props.name] === "number") {
125
+ const selected = props.getter(store.state);
126
+ if (typeof selected === "number") {
116
127
  return Number(value);
117
128
  }
118
- if (store.state[props.name] instanceof Date) {
129
+ if (selected instanceof Date) {
119
130
  return new Date(value);
120
131
  }
121
132
  return value;
122
133
  }
123
- function createChangeEventHandler() {
124
- return (e) => {
125
- const target = e.target;
126
- if (props.type === "checkbox") {
127
- store.dispatch((state) => {
128
- state[props.name] = target.checked;
129
- }, {
130
- key: dispatchKey
131
- });
134
+ const inputProps = useStoreController(store, {
135
+ ref: props.ref,
136
+ onSubscribe: (state, element) => {
137
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
138
+ const checked = toInputChecked(props.getter(state));
139
+ if (element.checked === checked) {
140
+ return;
141
+ }
142
+ element.checked = checked;
132
143
  } else {
133
- store.dispatch((state) => {
134
- state[props.name] = toStateValue(target.value);
135
- }, {
136
- key: dispatchKey
137
- });
144
+ const value = toInputValue(props.getter(state));
145
+ if (element.value === value) {
146
+ return;
147
+ }
148
+ element.value = value;
138
149
  }
139
- props.onChange?.(e);
140
- };
141
- }
142
- const dispatchKey = (0, import_react.useId)();
143
- const ref = useSubscriptionRef();
144
- const name = String(props.name);
145
- const defaultValue = getDefaultValue();
146
- const defaultChecked = getDefaultChecked();
147
- const onChange = createChangeEventHandler();
148
- return {
149
- key: dispatchKey,
150
- ref,
151
- name,
152
- defaultValue,
153
- defaultChecked,
154
- onChange
155
- };
156
- }
157
-
158
- // src/use_store_input.tsx
159
- function useStoreInput(store) {
160
- const input = (0, import_react2.useCallback)(
161
- function Component(props) {
162
- return /* @__PURE__ */ import_react3.default.createElement(Input, { store, ...props });
163
- },
164
- []
165
- );
166
- const select = (0, import_react2.useCallback)(
167
- function Component(props) {
168
- return /* @__PURE__ */ import_react3.default.createElement(Select, { store, ...props });
150
+ const event = new Event("input", { bubbles: true });
151
+ element.dispatchEvent(event);
169
152
  },
170
- []
171
- );
172
- const textarea = (0, import_react2.useCallback)(function Component(props) {
173
- return /* @__PURE__ */ import_react3.default.createElement(Textarea, { store, ...props });
174
- }, []);
153
+ onDispatch: (state, element) => {
154
+ if ("checked" in element && props.type === "checkbox") {
155
+ props.setter(state, element.checked);
156
+ } else {
157
+ props.setter(state, toStateValue(element.value));
158
+ }
159
+ }
160
+ });
175
161
  return {
176
- input,
177
- select,
178
- textarea
162
+ ref: inputProps.ref,
163
+ dispatchKey: inputProps.dispatchKey,
164
+ defaultValue: getDefaultValue(),
165
+ defaultChecked: getDefaultChecked(),
166
+ onChange: (event) => {
167
+ inputProps.onChange();
168
+ props.onChange?.(event);
169
+ }
179
170
  };
180
171
  }
181
- function Input({ store, ...props }) {
182
- const storeProps = useStoreInputProps(store, props);
183
- return /* @__PURE__ */ import_react3.default.createElement("input", { ...props, ...storeProps });
184
- }
185
- function Select({ store, ...props }) {
186
- const storeProps = useStoreInputProps(store, props);
187
- return /* @__PURE__ */ import_react3.default.createElement("select", { ...props, ...storeProps });
188
- }
189
- function Textarea({ store, ...props }) {
190
- const storeProps = useStoreInputProps(store, props);
191
- return /* @__PURE__ */ import_react3.default.createElement("textarea", { ...props, ...storeProps });
192
- }
193
172
  // Annotate the CommonJS export names for ESM import in node:
194
173
  0 && (module.exports = {
195
- Input,
196
- Select,
197
- Textarea,
198
174
  useStoreInput
199
175
  });
@@ -1,17 +1,64 @@
1
1
  // src/use_store_input.tsx
2
- import {
3
- useCallback
4
- } from "react";
5
- import React from "react";
6
-
7
- // src/use_store_input_props.tsx
8
- import {
9
- useEffect,
10
- useId,
11
- useRef
12
- } from "react";
2
+ import "react";
13
3
  import { format } from "date-fns";
14
- function useStoreInputProps(store, props) {
4
+
5
+ // src/use_store_controller.tsx
6
+ import { useEffect as useEffect2, useId, useRef } from "react";
7
+
8
+ // src/use_subscribe.tsx
9
+ import { useEffect } from "react";
10
+ function useSubscribe(store, subscriber) {
11
+ useEffect(() => {
12
+ const unsubscribe = store.subscribe(subscriber);
13
+ return () => {
14
+ unsubscribe();
15
+ };
16
+ }, [store]);
17
+ }
18
+
19
+ // src/use_store_controller.tsx
20
+ function useStoreController(store, props) {
21
+ const ref = useRef(null);
22
+ 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
+ useSubscribe(store, (state, key) => {
35
+ if (key === dispatchKey) {
36
+ return;
37
+ }
38
+ if (!ref.current) return;
39
+ props.onSubscribe(state, ref.current);
40
+ });
41
+ const onChange = () => {
42
+ store.dispatch(
43
+ (state) => {
44
+ const element = ref.current;
45
+ if (!element) return;
46
+ props.onDispatch(state, element);
47
+ },
48
+ {
49
+ key: dispatchKey
50
+ }
51
+ );
52
+ };
53
+ return {
54
+ dispatchKey,
55
+ ref,
56
+ onChange
57
+ };
58
+ }
59
+
60
+ // src/use_store_input.tsx
61
+ function useStoreInput(store, props) {
15
62
  const toInputValue = (value) => {
16
63
  if (value === void 0 || value === null) {
17
64
  return "";
@@ -33,7 +80,7 @@ function useStoreInputProps(store, props) {
33
80
  if (props.type === "checkbox" || props.type === "radio") {
34
81
  return void 0;
35
82
  }
36
- return toInputValue(store.state[props.name]);
83
+ return toInputValue(props.getter(store.state));
37
84
  };
38
85
  const toInputChecked = (value) => {
39
86
  if (props.type === "radio") {
@@ -48,120 +95,56 @@ function useStoreInputProps(store, props) {
48
95
  if (props.type !== "checkbox" && props.type !== "radio") {
49
96
  return void 0;
50
97
  }
51
- return toInputChecked(store.state[props.name]);
98
+ return toInputChecked(props.getter(store.state));
52
99
  };
53
- function useSubscriptionRef() {
54
- const ref2 = useRef(null);
55
- useEffect(() => {
56
- return store.subscribe((state, key) => {
57
- if (key === dispatchKey) {
58
- return;
59
- }
60
- const input = ref2.current;
61
- if (!input) {
62
- return;
63
- }
64
- if (props.type === "checkbox" || props.type === "radio") {
65
- const checked = toInputChecked(state[props.name]);
66
- if (input.checked === checked) {
67
- return;
68
- }
69
- input.checked = checked;
70
- } else {
71
- const value = toInputValue(state[props.name]);
72
- if (input.value === value) {
73
- return;
74
- }
75
- input.value = value;
76
- }
77
- const event = new Event("input", { bubbles: true });
78
- input.dispatchEvent(event);
79
- });
80
- }, []);
81
- return ref2;
82
- }
83
100
  function toStateValue(value) {
84
- if (typeof store.state[props.name] === "number") {
101
+ const selected = props.getter(store.state);
102
+ if (typeof selected === "number") {
85
103
  return Number(value);
86
104
  }
87
- if (store.state[props.name] instanceof Date) {
105
+ if (selected instanceof Date) {
88
106
  return new Date(value);
89
107
  }
90
108
  return value;
91
109
  }
92
- function createChangeEventHandler() {
93
- return (e) => {
94
- const target = e.target;
95
- if (props.type === "checkbox") {
96
- store.dispatch((state) => {
97
- state[props.name] = target.checked;
98
- }, {
99
- key: dispatchKey
100
- });
110
+ const inputProps = useStoreController(store, {
111
+ ref: props.ref,
112
+ onSubscribe: (state, element) => {
113
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
114
+ const checked = toInputChecked(props.getter(state));
115
+ if (element.checked === checked) {
116
+ return;
117
+ }
118
+ element.checked = checked;
101
119
  } else {
102
- store.dispatch((state) => {
103
- state[props.name] = toStateValue(target.value);
104
- }, {
105
- key: dispatchKey
106
- });
120
+ const value = toInputValue(props.getter(state));
121
+ if (element.value === value) {
122
+ return;
123
+ }
124
+ element.value = value;
107
125
  }
108
- props.onChange?.(e);
109
- };
110
- }
111
- const dispatchKey = useId();
112
- const ref = useSubscriptionRef();
113
- const name = String(props.name);
114
- const defaultValue = getDefaultValue();
115
- const defaultChecked = getDefaultChecked();
116
- const onChange = createChangeEventHandler();
117
- return {
118
- key: dispatchKey,
119
- ref,
120
- name,
121
- defaultValue,
122
- defaultChecked,
123
- onChange
124
- };
125
- }
126
-
127
- // src/use_store_input.tsx
128
- function useStoreInput(store) {
129
- const input = useCallback(
130
- function Component(props) {
131
- return /* @__PURE__ */ React.createElement(Input, { store, ...props });
126
+ const event = new Event("input", { bubbles: true });
127
+ element.dispatchEvent(event);
132
128
  },
133
- []
134
- );
135
- const select = useCallback(
136
- function Component(props) {
137
- return /* @__PURE__ */ React.createElement(Select, { store, ...props });
138
- },
139
- []
140
- );
141
- const textarea = useCallback(function Component(props) {
142
- return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
143
- }, []);
129
+ onDispatch: (state, element) => {
130
+ if ("checked" in element && props.type === "checkbox") {
131
+ props.setter(state, element.checked);
132
+ } else {
133
+ props.setter(state, toStateValue(element.value));
134
+ }
135
+ }
136
+ });
144
137
  return {
145
- input,
146
- select,
147
- textarea
138
+ ref: inputProps.ref,
139
+ dispatchKey: inputProps.dispatchKey,
140
+ defaultValue: getDefaultValue(),
141
+ defaultChecked: getDefaultChecked(),
142
+ onChange: (event) => {
143
+ inputProps.onChange();
144
+ props.onChange?.(event);
145
+ }
148
146
  };
149
147
  }
150
- function Input({ store, ...props }) {
151
- const storeProps = useStoreInputProps(store, props);
152
- return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
153
- }
154
- function Select({ store, ...props }) {
155
- const storeProps = useStoreInputProps(store, props);
156
- return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
157
- }
158
- function Textarea({ store, ...props }) {
159
- const storeProps = useStoreInputProps(store, props);
160
- return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
161
- }
162
148
  export {
163
- Input,
164
- Select,
165
- Textarea,
166
149
  useStoreInput
167
150
  };
@@ -0,0 +1,17 @@
1
+ import * as React from 'react';
2
+ import { Store } from './use_store.mjs';
3
+ import { StoreInputProps } from './use_store_input.mjs';
4
+
5
+ type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState>, "getter" | "setter">> & {
6
+ name?: keyof TState;
7
+ };
8
+ declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
9
+ name: string | undefined;
10
+ ref: React.RefObject<TInputElement | null>;
11
+ dispatchKey: string;
12
+ defaultValue: string | number | readonly string[] | undefined;
13
+ defaultChecked: boolean | undefined;
14
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
15
+ };
16
+
17
+ export { useStoreInputWithName };
@@ -0,0 +1,17 @@
1
+ import * as React from 'react';
2
+ import { Store } from './use_store.js';
3
+ import { StoreInputProps } from './use_store_input.js';
4
+
5
+ type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState>, "getter" | "setter">> & {
6
+ name?: keyof TState;
7
+ };
8
+ declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
9
+ name: string | undefined;
10
+ ref: React.RefObject<TInputElement | null>;
11
+ dispatchKey: string;
12
+ defaultValue: string | number | readonly string[] | undefined;
13
+ defaultChecked: boolean | undefined;
14
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
15
+ };
16
+
17
+ export { useStoreInputWithName };