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.
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/use_store_input_with_name.tsx
21
+ var use_store_input_with_name_exports = {};
22
+ __export(use_store_input_with_name_exports, {
23
+ useStoreInputWithName: () => useStoreInputWithName
24
+ });
25
+ module.exports = __toCommonJS(use_store_input_with_name_exports);
26
+
27
+ // src/use_store_input.tsx
28
+ var import_react3 = require("react");
29
+ var import_date_fns = require("date-fns");
30
+
31
+ // src/use_store_controller.tsx
32
+ var import_react2 = require("react");
33
+
34
+ // src/use_subscribe.tsx
35
+ var import_react = require("react");
36
+ function useSubscribe(store, subscriber) {
37
+ (0, import_react.useEffect)(() => {
38
+ const unsubscribe = store.subscribe(subscriber);
39
+ return () => {
40
+ unsubscribe();
41
+ };
42
+ }, [store]);
43
+ }
44
+
45
+ // src/use_store_controller.tsx
46
+ function useStoreController(store, props) {
47
+ const ref = (0, import_react2.useRef)(null);
48
+ 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
+ useSubscribe(store, (state, key) => {
61
+ if (key === dispatchKey) {
62
+ return;
63
+ }
64
+ if (!ref.current) return;
65
+ props.onSubscribe(state, ref.current);
66
+ });
67
+ const onChange = () => {
68
+ store.dispatch(
69
+ (state) => {
70
+ const element = ref.current;
71
+ if (!element) return;
72
+ props.onDispatch(state, element);
73
+ },
74
+ {
75
+ key: dispatchKey
76
+ }
77
+ );
78
+ };
79
+ return {
80
+ ref,
81
+ onChange
82
+ };
83
+ }
84
+
85
+ // src/use_store_input.tsx
86
+ function useStoreInput(store, props) {
87
+ const toInputValue = (value) => {
88
+ if (value === void 0 || value === null) {
89
+ return "";
90
+ }
91
+ if (props.type === "datetime-local") {
92
+ if (value instanceof Date) {
93
+ return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
94
+ }
95
+ if (typeof value === "string") {
96
+ return toInputValue(new Date(value));
97
+ }
98
+ }
99
+ return String(value);
100
+ };
101
+ const getDefaultValue = () => {
102
+ if (props.defaultValue !== void 0) {
103
+ return props.defaultValue;
104
+ }
105
+ if (props.type === "checkbox" || props.type === "radio") {
106
+ return void 0;
107
+ }
108
+ return toInputValue(props.getter(store.state));
109
+ };
110
+ const toInputChecked = (value) => {
111
+ if (props.type === "radio") {
112
+ return value !== void 0 && value === props.value;
113
+ }
114
+ return Boolean(value);
115
+ };
116
+ const getDefaultChecked = () => {
117
+ if (props.defaultChecked) {
118
+ return props.defaultChecked;
119
+ }
120
+ if (props.type !== "checkbox" && props.type !== "radio") {
121
+ return void 0;
122
+ }
123
+ return toInputChecked(props.getter(store.state));
124
+ };
125
+ function toStateValue(value) {
126
+ const selected = props.getter(store.state);
127
+ if (typeof selected === "number") {
128
+ return Number(value);
129
+ }
130
+ if (selected instanceof Date) {
131
+ return new Date(value);
132
+ }
133
+ return value;
134
+ }
135
+ const inputProps = useStoreController(store, {
136
+ ref: props.ref,
137
+ onSubscribe: (state, element) => {
138
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
139
+ const checked = toInputChecked(props.getter(state));
140
+ if (element.checked === checked) {
141
+ return;
142
+ }
143
+ element.checked = checked;
144
+ } else {
145
+ const value = toInputValue(props.getter(state));
146
+ if (element.value === value) {
147
+ return;
148
+ }
149
+ element.value = value;
150
+ }
151
+ const event = new Event("input", { bubbles: true });
152
+ element.dispatchEvent(event);
153
+ },
154
+ onDispatch: (state, element) => {
155
+ if ("checked" in element && props.type === "checkbox") {
156
+ props.setter(state, element.checked);
157
+ } else {
158
+ props.setter(state, toStateValue(element.value));
159
+ }
160
+ }
161
+ });
162
+ return {
163
+ ref: inputProps.ref,
164
+ defaultValue: getDefaultValue(),
165
+ defaultChecked: getDefaultChecked(),
166
+ onChange: (event) => {
167
+ inputProps.onChange();
168
+ props.onChange?.(event);
169
+ }
170
+ };
171
+ }
172
+
173
+ // src/use_store_input_with_name.tsx
174
+ function useStoreInputWithName(store, props) {
175
+ const inputProps = useStoreInput(store, {
176
+ ...props,
177
+ getter: (state) => {
178
+ if (props.getter) {
179
+ return props.getter(state);
180
+ }
181
+ return state[props.name];
182
+ },
183
+ setter: (state, value) => {
184
+ if (props.setter) {
185
+ props.setter(state, value);
186
+ return;
187
+ }
188
+ state[props.name] = value;
189
+ }
190
+ });
191
+ return {
192
+ ...inputProps,
193
+ name: "name" in props ? String(props.name) : void 0
194
+ };
195
+ }
196
+ // Annotate the CommonJS export names for ESM import in node:
197
+ 0 && (module.exports = {
198
+ useStoreInputWithName
199
+ });
@@ -0,0 +1,172 @@
1
+ // src/use_store_input.tsx
2
+ import "react";
3
+ import { format } from "date-fns";
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) {
61
+ const toInputValue = (value) => {
62
+ if (value === void 0 || value === null) {
63
+ return "";
64
+ }
65
+ if (props.type === "datetime-local") {
66
+ if (value instanceof Date) {
67
+ return format(value, "yyyy-MM-dd'T'HH:mm:ss");
68
+ }
69
+ if (typeof value === "string") {
70
+ return toInputValue(new Date(value));
71
+ }
72
+ }
73
+ return String(value);
74
+ };
75
+ const getDefaultValue = () => {
76
+ if (props.defaultValue !== void 0) {
77
+ return props.defaultValue;
78
+ }
79
+ if (props.type === "checkbox" || props.type === "radio") {
80
+ return void 0;
81
+ }
82
+ return toInputValue(props.getter(store.state));
83
+ };
84
+ const toInputChecked = (value) => {
85
+ if (props.type === "radio") {
86
+ return value !== void 0 && value === props.value;
87
+ }
88
+ return Boolean(value);
89
+ };
90
+ const getDefaultChecked = () => {
91
+ if (props.defaultChecked) {
92
+ return props.defaultChecked;
93
+ }
94
+ if (props.type !== "checkbox" && props.type !== "radio") {
95
+ return void 0;
96
+ }
97
+ return toInputChecked(props.getter(store.state));
98
+ };
99
+ function toStateValue(value) {
100
+ const selected = props.getter(store.state);
101
+ if (typeof selected === "number") {
102
+ return Number(value);
103
+ }
104
+ if (selected instanceof Date) {
105
+ return new Date(value);
106
+ }
107
+ return value;
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;
118
+ } else {
119
+ const value = toInputValue(props.getter(state));
120
+ if (element.value === value) {
121
+ return;
122
+ }
123
+ element.value = value;
124
+ }
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
+ });
136
+ return {
137
+ ref: inputProps.ref,
138
+ defaultValue: getDefaultValue(),
139
+ defaultChecked: getDefaultChecked(),
140
+ onChange: (event) => {
141
+ inputProps.onChange();
142
+ props.onChange?.(event);
143
+ }
144
+ };
145
+ }
146
+
147
+ // src/use_store_input_with_name.tsx
148
+ function useStoreInputWithName(store, props) {
149
+ const inputProps = useStoreInput(store, {
150
+ ...props,
151
+ getter: (state) => {
152
+ if (props.getter) {
153
+ return props.getter(state);
154
+ }
155
+ return state[props.name];
156
+ },
157
+ setter: (state, value) => {
158
+ if (props.setter) {
159
+ props.setter(state, value);
160
+ return;
161
+ }
162
+ state[props.name] = value;
163
+ }
164
+ });
165
+ return {
166
+ ...inputProps,
167
+ name: "name" in props ? String(props.name) : void 0
168
+ };
169
+ }
170
+ export {
171
+ useStoreInputWithName
172
+ };
package/package.json CHANGED
@@ -1,45 +1,45 @@
1
1
  {
2
- "name": "react-store-input",
3
- "version": "0.1.1",
4
- "types": "./dist/index.d.ts",
5
- "main": "./dist/index.mjs",
6
- "module": "./dist/index.js",
7
- "sideEffects": false,
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js"
13
- }
14
- },
15
- "scripts": {
16
- "build": "NODE_OPTIONS='--max-old-space-size=16384' tsup",
17
- "dev": "tsup --watch"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "git+https://github.com/dndnsoft/react-store-input.git"
22
- },
23
- "author": "",
24
- "license": "MIT",
25
- "bugs": {
26
- "url": "https://github.com/dndnsoft/react-store-input/issues"
27
- },
28
- "homepage": "https://github.com/dndnsoft/react-store-input#readme",
29
- "description": "",
30
- "devDependencies": {
31
- "@types/node": "^24.10.1",
32
- "@types/react": "^19",
33
- "@types/react-dom": "^19",
34
- "tsup": "^8.5.1",
35
- "typescript": "^5.7.3"
36
- },
37
- "peerDependencies": {
38
- "react": "^19",
39
- "react-dom": "^19"
40
- },
41
- "dependencies": {
42
- "date-fns": "^4.1.0",
43
- "immer": "^11.1.3"
2
+ "name": "react-store-input",
3
+ "version": "0.1.4",
4
+ "types": "./dist/index.d.ts",
5
+ "main": "./dist/index.mjs",
6
+ "module": "./dist/index.js",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
44
13
  }
45
- }
14
+ },
15
+ "scripts": {
16
+ "build": "NODE_OPTIONS='--max-old-space-size=16384' tsup",
17
+ "dev": "tsup --watch"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/dndnsoft/react-store-input.git"
22
+ },
23
+ "author": "",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/dndnsoft/react-store-input/issues"
27
+ },
28
+ "homepage": "https://github.com/dndnsoft/react-store-input#readme",
29
+ "description": "",
30
+ "devDependencies": {
31
+ "@types/node": "^24.10.1",
32
+ "@types/react": "^19",
33
+ "@types/react-dom": "^19",
34
+ "tsup": "^8.5.1",
35
+ "typescript": "^5.7.3"
36
+ },
37
+ "peerDependencies": {
38
+ "react": "^19",
39
+ "react-dom": "^19"
40
+ },
41
+ "dependencies": {
42
+ "date-fns": "^4.1.0",
43
+ "immer": "^11.1.3"
44
+ }
45
+ }
@@ -1,24 +0,0 @@
1
- import * as React from 'react';
2
- import { Ref, HTMLInputTypeAttribute, ChangeEventHandler } from 'react';
3
- import { Store } from './use_store.mjs';
4
-
5
- type InputType = HTMLInputTypeAttribute;
6
- type InputProps<TElement, TState> = {
7
- ref?: Ref<TElement>;
8
- name: keyof TState;
9
- type?: InputType;
10
- defaultValue?: string | number | readonly string[];
11
- value?: string | number | readonly string[];
12
- defaultChecked?: boolean;
13
- onChange?: ChangeEventHandler<TElement>;
14
- };
15
- declare function useStoreInputProps<TElement, TState>(store: Store<TState>, props: InputProps<TElement, TState>): {
16
- key: string;
17
- ref: React.RefObject<TElement | null>;
18
- name: string;
19
- defaultValue: string | number | readonly string[] | undefined;
20
- defaultChecked: boolean | undefined;
21
- onChange: ChangeEventHandler<TElement>;
22
- };
23
-
24
- export { useStoreInputProps };
@@ -1,24 +0,0 @@
1
- import * as React from 'react';
2
- import { Ref, HTMLInputTypeAttribute, ChangeEventHandler } from 'react';
3
- import { Store } from './use_store.js';
4
-
5
- type InputType = HTMLInputTypeAttribute;
6
- type InputProps<TElement, TState> = {
7
- ref?: Ref<TElement>;
8
- name: keyof TState;
9
- type?: InputType;
10
- defaultValue?: string | number | readonly string[];
11
- value?: string | number | readonly string[];
12
- defaultChecked?: boolean;
13
- onChange?: ChangeEventHandler<TElement>;
14
- };
15
- declare function useStoreInputProps<TElement, TState>(store: Store<TState>, props: InputProps<TElement, TState>): {
16
- key: string;
17
- ref: React.RefObject<TElement | null>;
18
- name: string;
19
- defaultValue: string | number | readonly string[] | undefined;
20
- defaultChecked: boolean | undefined;
21
- onChange: ChangeEventHandler<TElement>;
22
- };
23
-
24
- export { useStoreInputProps };
@@ -1,154 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/use_store_input_props.tsx
21
- var use_store_input_props_exports = {};
22
- __export(use_store_input_props_exports, {
23
- useStoreInputProps: () => useStoreInputProps
24
- });
25
- module.exports = __toCommonJS(use_store_input_props_exports);
26
- var import_react = require("react");
27
- var import_date_fns = require("date-fns");
28
- function useStoreInputProps(store, props) {
29
- const toInputValue = (value) => {
30
- if (value === void 0 || value === null) {
31
- return "";
32
- }
33
- if (props.type === "datetime-local") {
34
- if (value instanceof Date) {
35
- return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
36
- }
37
- if (typeof value === "string") {
38
- return toInputValue(new Date(value));
39
- }
40
- }
41
- return String(value);
42
- };
43
- const getDefaultValue = () => {
44
- if (props.defaultValue !== void 0) {
45
- return props.defaultValue;
46
- }
47
- if (props.type === "checkbox" || props.type === "radio") {
48
- return void 0;
49
- }
50
- return toInputValue(store.state[props.name]);
51
- };
52
- const toInputChecked = (value) => {
53
- if (props.type === "radio") {
54
- return value !== void 0 && value === props.value;
55
- }
56
- return Boolean(value);
57
- };
58
- const getDefaultChecked = () => {
59
- if (props.defaultChecked) {
60
- return props.defaultChecked;
61
- }
62
- if (props.type !== "checkbox" && props.type !== "radio") {
63
- return void 0;
64
- }
65
- return toInputChecked(store.state[props.name]);
66
- };
67
- function useSubscriptionRef() {
68
- const ref2 = (0, import_react.useRef)(null);
69
- (0, import_react.useEffect)(() => {
70
- const input = ref2.current;
71
- if (!input) {
72
- return;
73
- }
74
- if (typeof props.ref === "function") {
75
- props.ref(input);
76
- } else if (props.ref) {
77
- props.ref.current = input;
78
- }
79
- return store.subscribe((state, key) => {
80
- if (key === dispatchKey) {
81
- return;
82
- }
83
- if (props.type === "checkbox" || props.type === "radio") {
84
- const checked = toInputChecked(state[props.name]);
85
- if (input.checked === checked) {
86
- return;
87
- }
88
- input.checked = checked;
89
- } else {
90
- const value = toInputValue(state[props.name]);
91
- if (input.value === value) {
92
- return;
93
- }
94
- input.value = value;
95
- }
96
- const event = new Event("input", { bubbles: true });
97
- input.dispatchEvent(event);
98
- });
99
- }, []);
100
- return ref2;
101
- }
102
- function toStateValue(value) {
103
- if (typeof store.state[props.name] === "number") {
104
- return Number(value);
105
- }
106
- if (store.state[props.name] instanceof Date) {
107
- return new Date(value);
108
- }
109
- return value;
110
- }
111
- function createChangeEventHandler() {
112
- return (e) => {
113
- const target = e.target;
114
- if (props.type === "checkbox") {
115
- store.dispatch(
116
- (state) => {
117
- state[props.name] = target.checked;
118
- },
119
- {
120
- key: dispatchKey
121
- }
122
- );
123
- } else {
124
- store.dispatch(
125
- (state) => {
126
- state[props.name] = toStateValue(target.value);
127
- },
128
- {
129
- key: dispatchKey
130
- }
131
- );
132
- }
133
- props.onChange?.(e);
134
- };
135
- }
136
- const dispatchKey = (0, import_react.useId)();
137
- const ref = useSubscriptionRef();
138
- const name = String(props.name);
139
- const defaultValue = getDefaultValue();
140
- const defaultChecked = getDefaultChecked();
141
- const onChange = createChangeEventHandler();
142
- return {
143
- key: dispatchKey,
144
- ref,
145
- name,
146
- defaultValue,
147
- defaultChecked,
148
- onChange
149
- };
150
- }
151
- // Annotate the CommonJS export names for ESM import in node:
152
- 0 && (module.exports = {
153
- useStoreInputProps
154
- });