react-store-input 0.1.1 → 0.1.4

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,73 @@ 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
+ ref,
79
+ onChange
80
+ };
81
+ }
82
+
83
+ // src/use_store_input.tsx
84
+ function useStoreInput(store, props) {
46
85
  const toInputValue = (value) => {
47
86
  if (value === void 0 || value === null) {
48
87
  return "";
@@ -64,7 +103,7 @@ function useStoreInputProps(store, props) {
64
103
  if (props.type === "checkbox" || props.type === "radio") {
65
104
  return void 0;
66
105
  }
67
- return toInputValue(store.state[props.name]);
106
+ return toInputValue(props.getter(store.state));
68
107
  };
69
108
  const toInputChecked = (value) => {
70
109
  if (props.type === "radio") {
@@ -79,135 +118,56 @@ function useStoreInputProps(store, props) {
79
118
  if (props.type !== "checkbox" && props.type !== "radio") {
80
119
  return void 0;
81
120
  }
82
- return toInputChecked(store.state[props.name]);
121
+ return toInputChecked(props.getter(store.state));
83
122
  };
84
- function useSubscriptionRef() {
85
- const ref2 = (0, import_react.useRef)(null);
86
- (0, import_react.useEffect)(() => {
87
- const input = ref2.current;
88
- if (!input) {
89
- return;
90
- }
91
- if (typeof props.ref === "function") {
92
- props.ref(input);
93
- } else if (props.ref) {
94
- props.ref.current = input;
95
- }
96
- return store.subscribe((state, key) => {
97
- if (key === dispatchKey) {
98
- return;
99
- }
100
- if (props.type === "checkbox" || props.type === "radio") {
101
- const checked = toInputChecked(state[props.name]);
102
- if (input.checked === checked) {
103
- return;
104
- }
105
- input.checked = checked;
106
- } else {
107
- const value = toInputValue(state[props.name]);
108
- if (input.value === value) {
109
- return;
110
- }
111
- input.value = value;
112
- }
113
- const event = new Event("input", { bubbles: true });
114
- input.dispatchEvent(event);
115
- });
116
- }, []);
117
- return ref2;
118
- }
119
123
  function toStateValue(value) {
120
- if (typeof store.state[props.name] === "number") {
124
+ const selected = props.getter(store.state);
125
+ if (typeof selected === "number") {
121
126
  return Number(value);
122
127
  }
123
- if (store.state[props.name] instanceof Date) {
128
+ if (selected instanceof Date) {
124
129
  return new Date(value);
125
130
  }
126
131
  return value;
127
132
  }
128
- function createChangeEventHandler() {
129
- return (e) => {
130
- const target = e.target;
131
- if (props.type === "checkbox") {
132
- store.dispatch(
133
- (state) => {
134
- state[props.name] = target.checked;
135
- },
136
- {
137
- key: dispatchKey
138
- }
139
- );
133
+ const inputProps = useStoreController(store, {
134
+ ref: props.ref,
135
+ onSubscribe: (state, element) => {
136
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
137
+ const checked = toInputChecked(props.getter(state));
138
+ if (element.checked === checked) {
139
+ return;
140
+ }
141
+ element.checked = checked;
140
142
  } else {
141
- store.dispatch(
142
- (state) => {
143
- state[props.name] = toStateValue(target.value);
144
- },
145
- {
146
- key: dispatchKey
147
- }
148
- );
143
+ const value = toInputValue(props.getter(state));
144
+ if (element.value === value) {
145
+ return;
146
+ }
147
+ element.value = value;
149
148
  }
150
- props.onChange?.(e);
151
- };
152
- }
153
- const dispatchKey = (0, import_react.useId)();
154
- const ref = useSubscriptionRef();
155
- const name = String(props.name);
156
- const defaultValue = getDefaultValue();
157
- const defaultChecked = getDefaultChecked();
158
- const onChange = createChangeEventHandler();
159
- return {
160
- key: dispatchKey,
161
- ref,
162
- name,
163
- defaultValue,
164
- defaultChecked,
165
- onChange
166
- };
167
- }
168
-
169
- // src/use_store_input.tsx
170
- function useStoreInput(store) {
171
- const input = (0, import_react2.useCallback)(function Component(props) {
172
- return /* @__PURE__ */ import_react3.default.createElement(Input, { store, ...props });
173
- }, []);
174
- const select = (0, import_react2.useCallback)(function Component(props) {
175
- return /* @__PURE__ */ import_react3.default.createElement(Select, { store, ...props });
176
- }, []);
177
- const textarea = (0, import_react2.useCallback)(function Component(props) {
178
- return /* @__PURE__ */ import_react3.default.createElement(Textarea, { store, ...props });
179
- }, []);
149
+ const event = new Event("input", { bubbles: true });
150
+ element.dispatchEvent(event);
151
+ },
152
+ onDispatch: (state, element) => {
153
+ if ("checked" in element && props.type === "checkbox") {
154
+ props.setter(state, element.checked);
155
+ } else {
156
+ props.setter(state, toStateValue(element.value));
157
+ }
158
+ }
159
+ });
180
160
  return {
181
- input,
182
- select,
183
- textarea
161
+ ref: inputProps.ref,
162
+ defaultValue: getDefaultValue(),
163
+ defaultChecked: getDefaultChecked(),
164
+ onChange: (event) => {
165
+ inputProps.onChange();
166
+ props.onChange?.(event);
167
+ }
184
168
  };
185
169
  }
186
- function Input({
187
- store,
188
- ...props
189
- }) {
190
- const storeProps = useStoreInputProps(store, props);
191
- return /* @__PURE__ */ import_react3.default.createElement("input", { ...props, ...storeProps });
192
- }
193
- function Select({
194
- store,
195
- ...props
196
- }) {
197
- const storeProps = useStoreInputProps(store, props);
198
- return /* @__PURE__ */ import_react3.default.createElement("select", { ...props, ...storeProps });
199
- }
200
- function Textarea({
201
- store,
202
- ...props
203
- }) {
204
- const storeProps = useStoreInputProps(store, props);
205
- return /* @__PURE__ */ import_react3.default.createElement("textarea", { ...props, ...storeProps });
206
- }
207
170
  // Annotate the CommonJS export names for ESM import in node:
208
171
  0 && (module.exports = {
209
- Input,
210
- Select,
211
- Textarea,
212
172
  useStoreInput
213
173
  });
@@ -1,17 +1,63 @@
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
+ ref,
55
+ onChange
56
+ };
57
+ }
58
+
59
+ // src/use_store_input.tsx
60
+ function useStoreInput(store, props) {
15
61
  const toInputValue = (value) => {
16
62
  if (value === void 0 || value === null) {
17
63
  return "";
@@ -33,7 +79,7 @@ function useStoreInputProps(store, props) {
33
79
  if (props.type === "checkbox" || props.type === "radio") {
34
80
  return void 0;
35
81
  }
36
- return toInputValue(store.state[props.name]);
82
+ return toInputValue(props.getter(store.state));
37
83
  };
38
84
  const toInputChecked = (value) => {
39
85
  if (props.type === "radio") {
@@ -48,134 +94,55 @@ function useStoreInputProps(store, props) {
48
94
  if (props.type !== "checkbox" && props.type !== "radio") {
49
95
  return void 0;
50
96
  }
51
- return toInputChecked(store.state[props.name]);
97
+ return toInputChecked(props.getter(store.state));
52
98
  };
53
- function useSubscriptionRef() {
54
- const ref2 = useRef(null);
55
- useEffect(() => {
56
- const input = ref2.current;
57
- if (!input) {
58
- return;
59
- }
60
- if (typeof props.ref === "function") {
61
- props.ref(input);
62
- } else if (props.ref) {
63
- props.ref.current = input;
64
- }
65
- return store.subscribe((state, key) => {
66
- if (key === dispatchKey) {
67
- return;
68
- }
69
- if (props.type === "checkbox" || props.type === "radio") {
70
- const checked = toInputChecked(state[props.name]);
71
- if (input.checked === checked) {
72
- return;
73
- }
74
- input.checked = checked;
75
- } else {
76
- const value = toInputValue(state[props.name]);
77
- if (input.value === value) {
78
- return;
79
- }
80
- input.value = value;
81
- }
82
- const event = new Event("input", { bubbles: true });
83
- input.dispatchEvent(event);
84
- });
85
- }, []);
86
- return ref2;
87
- }
88
99
  function toStateValue(value) {
89
- if (typeof store.state[props.name] === "number") {
100
+ const selected = props.getter(store.state);
101
+ if (typeof selected === "number") {
90
102
  return Number(value);
91
103
  }
92
- if (store.state[props.name] instanceof Date) {
104
+ if (selected instanceof Date) {
93
105
  return new Date(value);
94
106
  }
95
107
  return value;
96
108
  }
97
- function createChangeEventHandler() {
98
- return (e) => {
99
- const target = e.target;
100
- if (props.type === "checkbox") {
101
- store.dispatch(
102
- (state) => {
103
- state[props.name] = target.checked;
104
- },
105
- {
106
- key: dispatchKey
107
- }
108
- );
109
+ const inputProps = useStoreController(store, {
110
+ ref: props.ref,
111
+ onSubscribe: (state, element) => {
112
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
113
+ const checked = toInputChecked(props.getter(state));
114
+ if (element.checked === checked) {
115
+ return;
116
+ }
117
+ element.checked = checked;
109
118
  } else {
110
- store.dispatch(
111
- (state) => {
112
- state[props.name] = toStateValue(target.value);
113
- },
114
- {
115
- key: dispatchKey
116
- }
117
- );
119
+ const value = toInputValue(props.getter(state));
120
+ if (element.value === value) {
121
+ return;
122
+ }
123
+ element.value = value;
118
124
  }
119
- props.onChange?.(e);
120
- };
121
- }
122
- const dispatchKey = useId();
123
- const ref = useSubscriptionRef();
124
- const name = String(props.name);
125
- const defaultValue = getDefaultValue();
126
- const defaultChecked = getDefaultChecked();
127
- const onChange = createChangeEventHandler();
128
- return {
129
- key: dispatchKey,
130
- ref,
131
- name,
132
- defaultValue,
133
- defaultChecked,
134
- onChange
135
- };
136
- }
137
-
138
- // src/use_store_input.tsx
139
- function useStoreInput(store) {
140
- const input = useCallback(function Component(props) {
141
- return /* @__PURE__ */ React.createElement(Input, { store, ...props });
142
- }, []);
143
- const select = useCallback(function Component(props) {
144
- return /* @__PURE__ */ React.createElement(Select, { store, ...props });
145
- }, []);
146
- const textarea = useCallback(function Component(props) {
147
- return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
148
- }, []);
125
+ const event = new Event("input", { bubbles: true });
126
+ element.dispatchEvent(event);
127
+ },
128
+ onDispatch: (state, element) => {
129
+ if ("checked" in element && props.type === "checkbox") {
130
+ props.setter(state, element.checked);
131
+ } else {
132
+ props.setter(state, toStateValue(element.value));
133
+ }
134
+ }
135
+ });
149
136
  return {
150
- input,
151
- select,
152
- textarea
137
+ ref: inputProps.ref,
138
+ defaultValue: getDefaultValue(),
139
+ defaultChecked: getDefaultChecked(),
140
+ onChange: (event) => {
141
+ inputProps.onChange();
142
+ props.onChange?.(event);
143
+ }
153
144
  };
154
145
  }
155
- function Input({
156
- store,
157
- ...props
158
- }) {
159
- const storeProps = useStoreInputProps(store, props);
160
- return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
161
- }
162
- function Select({
163
- store,
164
- ...props
165
- }) {
166
- const storeProps = useStoreInputProps(store, props);
167
- return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
168
- }
169
- function Textarea({
170
- store,
171
- ...props
172
- }) {
173
- const storeProps = useStoreInputProps(store, props);
174
- return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
175
- }
176
146
  export {
177
- Input,
178
- Select,
179
- Textarea,
180
147
  useStoreInput
181
148
  };
@@ -0,0 +1,16 @@
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
+ defaultValue: string | number | readonly string[] | undefined;
12
+ defaultChecked: boolean | undefined;
13
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
14
+ };
15
+
16
+ export { useStoreInputWithName };
@@ -0,0 +1,16 @@
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
+ defaultValue: string | number | readonly string[] | undefined;
12
+ defaultChecked: boolean | undefined;
13
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
14
+ };
15
+
16
+ export { useStoreInputWithName };