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.
- package/LICENSE +7 -0
- package/README.md +39 -0
- package/dist/create_render.d.mts +7 -0
- package/dist/create_render.d.ts +7 -0
- package/dist/create_render.js +79 -0
- package/dist/create_render.mjs +42 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +401 -0
- package/dist/index.mjs +360 -0
- package/dist/serialize.d.mts +4 -0
- package/dist/serialize.d.ts +4 -0
- package/dist/serialize.js +116 -0
- package/dist/serialize.mjs +90 -0
- package/dist/use_form_store.d.mts +11 -0
- package/dist/use_form_store.d.ts +11 -0
- package/dist/use_form_store.js +294 -0
- package/dist/use_form_store.mjs +263 -0
- package/dist/use_selector.d.mts +5 -0
- package/dist/use_selector.d.ts +5 -0
- package/dist/use_selector.js +54 -0
- package/dist/use_selector.mjs +29 -0
- package/dist/use_store.d.mts +12 -0
- package/dist/use_store.d.ts +12 -0
- package/dist/use_store.js +71 -0
- package/dist/use_store.mjs +46 -0
- package/dist/use_store_input.d.mts +19 -0
- package/dist/use_store_input.d.ts +19 -0
- package/dist/use_store_input.js +199 -0
- package/dist/use_store_input.mjs +167 -0
- package/dist/use_store_input_props.d.mts +23 -0
- package/dist/use_store_input_props.d.ts +23 -0
- package/dist/use_store_input_props.js +143 -0
- package/dist/use_store_input_props.mjs +122 -0
- package/dist/use_subscribe.d.mts +5 -0
- package/dist/use_subscribe.d.ts +5 -0
- package/dist/use_subscribe.js +38 -0
- package/dist/use_subscribe.mjs +13 -0
- package/package.json +45 -0
- package/tsup.config.ts +9 -0
|
@@ -0,0 +1,71 @@
|
|
|
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.tsx
|
|
21
|
+
var use_store_exports = {};
|
|
22
|
+
__export(use_store_exports, {
|
|
23
|
+
useStore: () => useStore
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_store_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_immer = require("immer");
|
|
28
|
+
function useStore(initialState) {
|
|
29
|
+
const stateRef = (0, import_react.useRef)(initialState);
|
|
30
|
+
const subscribers = (0, import_react.useRef)([]);
|
|
31
|
+
const dispatch = (recipe, options = {}) => {
|
|
32
|
+
if (typeof recipe === "function") {
|
|
33
|
+
stateRef.current = (0, import_immer.produce)(stateRef.current, recipe);
|
|
34
|
+
} else {
|
|
35
|
+
stateRef.current = (0, import_immer.produce)(
|
|
36
|
+
stateRef.current,
|
|
37
|
+
(draft) => {
|
|
38
|
+
for (const key in recipe) {
|
|
39
|
+
draft[key] = recipe[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
notify(options.key);
|
|
45
|
+
};
|
|
46
|
+
const subscribe = (subscriber) => {
|
|
47
|
+
subscribers.current.push(subscriber);
|
|
48
|
+
return () => {
|
|
49
|
+
const index = subscribers.current.indexOf(subscriber);
|
|
50
|
+
if (index !== -1) {
|
|
51
|
+
subscribers.current.splice(index, 1);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const notify = (key) => {
|
|
56
|
+
for (const listener of subscribers.current) {
|
|
57
|
+
listener(stateRef.current, key);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
get state() {
|
|
62
|
+
return stateRef.current;
|
|
63
|
+
},
|
|
64
|
+
dispatch,
|
|
65
|
+
subscribe
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
useStore
|
|
71
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/use_store.tsx
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { produce } from "immer";
|
|
4
|
+
function useStore(initialState) {
|
|
5
|
+
const stateRef = useRef(initialState);
|
|
6
|
+
const subscribers = useRef([]);
|
|
7
|
+
const dispatch = (recipe, options = {}) => {
|
|
8
|
+
if (typeof recipe === "function") {
|
|
9
|
+
stateRef.current = produce(stateRef.current, recipe);
|
|
10
|
+
} else {
|
|
11
|
+
stateRef.current = produce(
|
|
12
|
+
stateRef.current,
|
|
13
|
+
(draft) => {
|
|
14
|
+
for (const key in recipe) {
|
|
15
|
+
draft[key] = recipe[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
notify(options.key);
|
|
21
|
+
};
|
|
22
|
+
const subscribe = (subscriber) => {
|
|
23
|
+
subscribers.current.push(subscriber);
|
|
24
|
+
return () => {
|
|
25
|
+
const index = subscribers.current.indexOf(subscriber);
|
|
26
|
+
if (index !== -1) {
|
|
27
|
+
subscribers.current.splice(index, 1);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const notify = (key) => {
|
|
32
|
+
for (const listener of subscribers.current) {
|
|
33
|
+
listener(stateRef.current, key);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
get state() {
|
|
38
|
+
return stateRef.current;
|
|
39
|
+
},
|
|
40
|
+
dispatch,
|
|
41
|
+
subscribe
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
useStore
|
|
46
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React__default, { InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes } from 'react';
|
|
2
|
+
import { Store } from './use_store.mjs';
|
|
3
|
+
|
|
4
|
+
type StoreInputProps<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = Omit<TElement extends HTMLInputElement ? InputHTMLAttributes<HTMLInputElement> : TElement extends HTMLSelectElement ? SelectHTMLAttributes<HTMLSelectElement> : TextareaHTMLAttributes<HTMLTextAreaElement>, "name"> & {
|
|
5
|
+
name: keyof TState;
|
|
6
|
+
};
|
|
7
|
+
declare function useStoreInput<TState>(store: Store<TState>): {
|
|
8
|
+
input: React__default.FC<StoreInputProps<HTMLInputElement, TState>>;
|
|
9
|
+
select: React__default.FC<StoreInputProps<HTMLSelectElement, TState>>;
|
|
10
|
+
textarea: React__default.FC<StoreInputProps<HTMLTextAreaElement, TState>>;
|
|
11
|
+
};
|
|
12
|
+
type StoreInputPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreInputProps<TElement, TState> & {
|
|
13
|
+
store: Store<TState>;
|
|
14
|
+
};
|
|
15
|
+
declare function Input<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
|
|
16
|
+
declare function Select<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
|
|
17
|
+
declare function Textarea<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
|
|
18
|
+
|
|
19
|
+
export { Input, Select, type StoreInputPropsWithStore, Textarea, useStoreInput };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React__default, { InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes } from 'react';
|
|
2
|
+
import { Store } from './use_store.js';
|
|
3
|
+
|
|
4
|
+
type StoreInputProps<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = Omit<TElement extends HTMLInputElement ? InputHTMLAttributes<HTMLInputElement> : TElement extends HTMLSelectElement ? SelectHTMLAttributes<HTMLSelectElement> : TextareaHTMLAttributes<HTMLTextAreaElement>, "name"> & {
|
|
5
|
+
name: keyof TState;
|
|
6
|
+
};
|
|
7
|
+
declare function useStoreInput<TState>(store: Store<TState>): {
|
|
8
|
+
input: React__default.FC<StoreInputProps<HTMLInputElement, TState>>;
|
|
9
|
+
select: React__default.FC<StoreInputProps<HTMLSelectElement, TState>>;
|
|
10
|
+
textarea: React__default.FC<StoreInputProps<HTMLTextAreaElement, TState>>;
|
|
11
|
+
};
|
|
12
|
+
type StoreInputPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = StoreInputProps<TElement, TState> & {
|
|
13
|
+
store: Store<TState>;
|
|
14
|
+
};
|
|
15
|
+
declare function Input<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLInputElement, TState>): React__default.JSX.Element;
|
|
16
|
+
declare function Select<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLSelectElement, TState>): React__default.JSX.Element;
|
|
17
|
+
declare function Textarea<TState>({ store, ...props }: StoreInputPropsWithStore<HTMLTextAreaElement, TState>): React__default.JSX.Element;
|
|
18
|
+
|
|
19
|
+
export { Input, Select, type StoreInputPropsWithStore, Textarea, useStoreInput };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
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
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/use_store_input.tsx
|
|
31
|
+
var use_store_input_exports = {};
|
|
32
|
+
__export(use_store_input_exports, {
|
|
33
|
+
Input: () => Input,
|
|
34
|
+
Select: () => Select,
|
|
35
|
+
Textarea: () => Textarea,
|
|
36
|
+
useStoreInput: () => useStoreInput
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(use_store_input_exports);
|
|
39
|
+
var import_react2 = require("react");
|
|
40
|
+
var import_react3 = __toESM(require("react"));
|
|
41
|
+
|
|
42
|
+
// src/use_store_input_props.tsx
|
|
43
|
+
var import_react = require("react");
|
|
44
|
+
var import_date_fns = require("date-fns");
|
|
45
|
+
function useStoreInputProps(store, props) {
|
|
46
|
+
const toInputValue = (value) => {
|
|
47
|
+
if (value === void 0 || value === null) {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
if (props.type === "datetime-local") {
|
|
51
|
+
if (value instanceof Date) {
|
|
52
|
+
return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "string") {
|
|
55
|
+
return toInputValue(new Date(value));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return String(value);
|
|
59
|
+
};
|
|
60
|
+
const getDefaultValue = () => {
|
|
61
|
+
if (props.defaultValue !== void 0) {
|
|
62
|
+
return props.defaultValue;
|
|
63
|
+
}
|
|
64
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
return toInputValue(store.state[props.name]);
|
|
68
|
+
};
|
|
69
|
+
const toInputChecked = (value) => {
|
|
70
|
+
if (props.type === "radio") {
|
|
71
|
+
return value !== void 0 && value === props.value;
|
|
72
|
+
}
|
|
73
|
+
return Boolean(value);
|
|
74
|
+
};
|
|
75
|
+
const getDefaultChecked = () => {
|
|
76
|
+
if (props.defaultChecked) {
|
|
77
|
+
return props.defaultChecked;
|
|
78
|
+
}
|
|
79
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
return toInputChecked(store.state[props.name]);
|
|
83
|
+
};
|
|
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
|
+
function toStateValue(value) {
|
|
115
|
+
if (typeof store.state[props.name] === "number") {
|
|
116
|
+
return Number(value);
|
|
117
|
+
}
|
|
118
|
+
if (store.state[props.name] instanceof Date) {
|
|
119
|
+
return new Date(value);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
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
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
store.dispatch((state) => {
|
|
134
|
+
state[props.name] = toStateValue(target.value);
|
|
135
|
+
}, {
|
|
136
|
+
key: dispatchKey
|
|
137
|
+
});
|
|
138
|
+
}
|
|
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 });
|
|
169
|
+
},
|
|
170
|
+
[]
|
|
171
|
+
);
|
|
172
|
+
const textarea = (0, import_react2.useCallback)(function Component(props) {
|
|
173
|
+
return /* @__PURE__ */ import_react3.default.createElement(Textarea, { store, ...props });
|
|
174
|
+
}, []);
|
|
175
|
+
return {
|
|
176
|
+
input,
|
|
177
|
+
select,
|
|
178
|
+
textarea
|
|
179
|
+
};
|
|
180
|
+
}
|
|
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
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
194
|
+
0 && (module.exports = {
|
|
195
|
+
Input,
|
|
196
|
+
Select,
|
|
197
|
+
Textarea,
|
|
198
|
+
useStoreInput
|
|
199
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
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";
|
|
13
|
+
import { format } from "date-fns";
|
|
14
|
+
function useStoreInputProps(store, props) {
|
|
15
|
+
const toInputValue = (value) => {
|
|
16
|
+
if (value === void 0 || value === null) {
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
if (props.type === "datetime-local") {
|
|
20
|
+
if (value instanceof Date) {
|
|
21
|
+
return format(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === "string") {
|
|
24
|
+
return toInputValue(new Date(value));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return String(value);
|
|
28
|
+
};
|
|
29
|
+
const getDefaultValue = () => {
|
|
30
|
+
if (props.defaultValue !== void 0) {
|
|
31
|
+
return props.defaultValue;
|
|
32
|
+
}
|
|
33
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
return toInputValue(store.state[props.name]);
|
|
37
|
+
};
|
|
38
|
+
const toInputChecked = (value) => {
|
|
39
|
+
if (props.type === "radio") {
|
|
40
|
+
return value !== void 0 && value === props.value;
|
|
41
|
+
}
|
|
42
|
+
return Boolean(value);
|
|
43
|
+
};
|
|
44
|
+
const getDefaultChecked = () => {
|
|
45
|
+
if (props.defaultChecked) {
|
|
46
|
+
return props.defaultChecked;
|
|
47
|
+
}
|
|
48
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
return toInputChecked(store.state[props.name]);
|
|
52
|
+
};
|
|
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
|
+
function toStateValue(value) {
|
|
84
|
+
if (typeof store.state[props.name] === "number") {
|
|
85
|
+
return Number(value);
|
|
86
|
+
}
|
|
87
|
+
if (store.state[props.name] instanceof Date) {
|
|
88
|
+
return new Date(value);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
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
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
store.dispatch((state) => {
|
|
103
|
+
state[props.name] = toStateValue(target.value);
|
|
104
|
+
}, {
|
|
105
|
+
key: dispatchKey
|
|
106
|
+
});
|
|
107
|
+
}
|
|
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 });
|
|
132
|
+
},
|
|
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
|
+
}, []);
|
|
144
|
+
return {
|
|
145
|
+
input,
|
|
146
|
+
select,
|
|
147
|
+
textarea
|
|
148
|
+
};
|
|
149
|
+
}
|
|
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
|
+
export {
|
|
163
|
+
Input,
|
|
164
|
+
Select,
|
|
165
|
+
Textarea,
|
|
166
|
+
useStoreInput
|
|
167
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { HTMLInputTypeAttribute, ChangeEventHandler } from 'react';
|
|
3
|
+
import { Store } from './use_store.mjs';
|
|
4
|
+
|
|
5
|
+
type InputType = HTMLInputTypeAttribute;
|
|
6
|
+
type InputProps<TElement, TState> = {
|
|
7
|
+
name: keyof TState;
|
|
8
|
+
type?: InputType;
|
|
9
|
+
defaultValue?: string | number | readonly string[];
|
|
10
|
+
value?: string | number | readonly string[];
|
|
11
|
+
defaultChecked?: boolean;
|
|
12
|
+
onChange?: ChangeEventHandler<TElement>;
|
|
13
|
+
};
|
|
14
|
+
declare function useStoreInputProps<TElement, TState>(store: Store<TState>, props: InputProps<TElement, TState>): {
|
|
15
|
+
key: string;
|
|
16
|
+
ref: React.RefObject<TElement | null>;
|
|
17
|
+
name: string;
|
|
18
|
+
defaultValue: string | number | readonly string[] | undefined;
|
|
19
|
+
defaultChecked: boolean | undefined;
|
|
20
|
+
onChange: ChangeEventHandler<TElement>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { useStoreInputProps };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { HTMLInputTypeAttribute, ChangeEventHandler } from 'react';
|
|
3
|
+
import { Store } from './use_store.js';
|
|
4
|
+
|
|
5
|
+
type InputType = HTMLInputTypeAttribute;
|
|
6
|
+
type InputProps<TElement, TState> = {
|
|
7
|
+
name: keyof TState;
|
|
8
|
+
type?: InputType;
|
|
9
|
+
defaultValue?: string | number | readonly string[];
|
|
10
|
+
value?: string | number | readonly string[];
|
|
11
|
+
defaultChecked?: boolean;
|
|
12
|
+
onChange?: ChangeEventHandler<TElement>;
|
|
13
|
+
};
|
|
14
|
+
declare function useStoreInputProps<TElement, TState>(store: Store<TState>, props: InputProps<TElement, TState>): {
|
|
15
|
+
key: string;
|
|
16
|
+
ref: React.RefObject<TElement | null>;
|
|
17
|
+
name: string;
|
|
18
|
+
defaultValue: string | number | readonly string[] | undefined;
|
|
19
|
+
defaultChecked: boolean | undefined;
|
|
20
|
+
onChange: ChangeEventHandler<TElement>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { useStoreInputProps };
|