react-store-input 0.1.0

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,143 @@
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
+ return store.subscribe((state, key) => {
71
+ if (key === dispatchKey) {
72
+ return;
73
+ }
74
+ const input = ref2.current;
75
+ if (!input) {
76
+ return;
77
+ }
78
+ if (props.type === "checkbox" || props.type === "radio") {
79
+ const checked = toInputChecked(state[props.name]);
80
+ if (input.checked === checked) {
81
+ return;
82
+ }
83
+ input.checked = checked;
84
+ } else {
85
+ const value = toInputValue(state[props.name]);
86
+ if (input.value === value) {
87
+ return;
88
+ }
89
+ input.value = value;
90
+ }
91
+ const event = new Event("input", { bubbles: true });
92
+ input.dispatchEvent(event);
93
+ });
94
+ }, []);
95
+ return ref2;
96
+ }
97
+ function toStateValue(value) {
98
+ if (typeof store.state[props.name] === "number") {
99
+ return Number(value);
100
+ }
101
+ if (store.state[props.name] instanceof Date) {
102
+ return new Date(value);
103
+ }
104
+ return value;
105
+ }
106
+ function createChangeEventHandler() {
107
+ return (e) => {
108
+ const target = e.target;
109
+ if (props.type === "checkbox") {
110
+ store.dispatch((state) => {
111
+ state[props.name] = target.checked;
112
+ }, {
113
+ key: dispatchKey
114
+ });
115
+ } else {
116
+ store.dispatch((state) => {
117
+ state[props.name] = toStateValue(target.value);
118
+ }, {
119
+ key: dispatchKey
120
+ });
121
+ }
122
+ props.onChange?.(e);
123
+ };
124
+ }
125
+ const dispatchKey = (0, import_react.useId)();
126
+ const ref = useSubscriptionRef();
127
+ const name = String(props.name);
128
+ const defaultValue = getDefaultValue();
129
+ const defaultChecked = getDefaultChecked();
130
+ const onChange = createChangeEventHandler();
131
+ return {
132
+ key: dispatchKey,
133
+ ref,
134
+ name,
135
+ defaultValue,
136
+ defaultChecked,
137
+ onChange
138
+ };
139
+ }
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ useStoreInputProps
143
+ });
@@ -0,0 +1,122 @@
1
+ // src/use_store_input_props.tsx
2
+ import {
3
+ useEffect,
4
+ useId,
5
+ useRef
6
+ } from "react";
7
+ import { format } from "date-fns";
8
+ function useStoreInputProps(store, props) {
9
+ const toInputValue = (value) => {
10
+ if (value === void 0 || value === null) {
11
+ return "";
12
+ }
13
+ if (props.type === "datetime-local") {
14
+ if (value instanceof Date) {
15
+ return format(value, "yyyy-MM-dd'T'HH:mm:ss");
16
+ }
17
+ if (typeof value === "string") {
18
+ return toInputValue(new Date(value));
19
+ }
20
+ }
21
+ return String(value);
22
+ };
23
+ const getDefaultValue = () => {
24
+ if (props.defaultValue !== void 0) {
25
+ return props.defaultValue;
26
+ }
27
+ if (props.type === "checkbox" || props.type === "radio") {
28
+ return void 0;
29
+ }
30
+ return toInputValue(store.state[props.name]);
31
+ };
32
+ const toInputChecked = (value) => {
33
+ if (props.type === "radio") {
34
+ return value !== void 0 && value === props.value;
35
+ }
36
+ return Boolean(value);
37
+ };
38
+ const getDefaultChecked = () => {
39
+ if (props.defaultChecked) {
40
+ return props.defaultChecked;
41
+ }
42
+ if (props.type !== "checkbox" && props.type !== "radio") {
43
+ return void 0;
44
+ }
45
+ return toInputChecked(store.state[props.name]);
46
+ };
47
+ function useSubscriptionRef() {
48
+ const ref2 = useRef(null);
49
+ useEffect(() => {
50
+ return store.subscribe((state, key) => {
51
+ if (key === dispatchKey) {
52
+ return;
53
+ }
54
+ const input = ref2.current;
55
+ if (!input) {
56
+ return;
57
+ }
58
+ if (props.type === "checkbox" || props.type === "radio") {
59
+ const checked = toInputChecked(state[props.name]);
60
+ if (input.checked === checked) {
61
+ return;
62
+ }
63
+ input.checked = checked;
64
+ } else {
65
+ const value = toInputValue(state[props.name]);
66
+ if (input.value === value) {
67
+ return;
68
+ }
69
+ input.value = value;
70
+ }
71
+ const event = new Event("input", { bubbles: true });
72
+ input.dispatchEvent(event);
73
+ });
74
+ }, []);
75
+ return ref2;
76
+ }
77
+ function toStateValue(value) {
78
+ if (typeof store.state[props.name] === "number") {
79
+ return Number(value);
80
+ }
81
+ if (store.state[props.name] instanceof Date) {
82
+ return new Date(value);
83
+ }
84
+ return value;
85
+ }
86
+ function createChangeEventHandler() {
87
+ return (e) => {
88
+ const target = e.target;
89
+ if (props.type === "checkbox") {
90
+ store.dispatch((state) => {
91
+ state[props.name] = target.checked;
92
+ }, {
93
+ key: dispatchKey
94
+ });
95
+ } else {
96
+ store.dispatch((state) => {
97
+ state[props.name] = toStateValue(target.value);
98
+ }, {
99
+ key: dispatchKey
100
+ });
101
+ }
102
+ props.onChange?.(e);
103
+ };
104
+ }
105
+ const dispatchKey = useId();
106
+ const ref = useSubscriptionRef();
107
+ const name = String(props.name);
108
+ const defaultValue = getDefaultValue();
109
+ const defaultChecked = getDefaultChecked();
110
+ const onChange = createChangeEventHandler();
111
+ return {
112
+ key: dispatchKey,
113
+ ref,
114
+ name,
115
+ defaultValue,
116
+ defaultChecked,
117
+ onChange
118
+ };
119
+ }
120
+ export {
121
+ useStoreInputProps
122
+ };
@@ -0,0 +1,5 @@
1
+ import { Store, StoreSubscriber } from './use_store.mjs';
2
+
3
+ declare function useSubscribe<T>(store: Store<T>, subscriber: StoreSubscriber<T>): void;
4
+
5
+ export { useSubscribe };
@@ -0,0 +1,5 @@
1
+ import { Store, StoreSubscriber } from './use_store.js';
2
+
3
+ declare function useSubscribe<T>(store: Store<T>, subscriber: StoreSubscriber<T>): void;
4
+
5
+ export { useSubscribe };
@@ -0,0 +1,38 @@
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_subscribe.tsx
21
+ var use_subscribe_exports = {};
22
+ __export(use_subscribe_exports, {
23
+ useSubscribe: () => useSubscribe
24
+ });
25
+ module.exports = __toCommonJS(use_subscribe_exports);
26
+ var import_react = require("react");
27
+ function useSubscribe(store, subscriber) {
28
+ (0, import_react.useEffect)(() => {
29
+ const unsubscribe = store.subscribe(subscriber);
30
+ return () => {
31
+ unsubscribe();
32
+ };
33
+ }, [store]);
34
+ }
35
+ // Annotate the CommonJS export names for ESM import in node:
36
+ 0 && (module.exports = {
37
+ useSubscribe
38
+ });
@@ -0,0 +1,13 @@
1
+ // src/use_subscribe.tsx
2
+ import { useEffect } from "react";
3
+ function useSubscribe(store, subscriber) {
4
+ useEffect(() => {
5
+ const unsubscribe = store.subscribe(subscriber);
6
+ return () => {
7
+ unsubscribe();
8
+ };
9
+ }, [store]);
10
+ }
11
+ export {
12
+ useSubscribe
13
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "react-store-input",
3
+ "version": "0.1.0",
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"
44
+ }
45
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/**/*.ts", "src/**/*.tsx"], // Your entry file(s)
5
+ format: ["cjs", "esm"], // Output both CommonJS (.cjs) and ESM (.js/.mjs)
6
+ dts: true, // Generate .d.ts files for both formats
7
+ splitting: false,
8
+ clean: true, // Clean dist folder before build
9
+ });