react-store-input 0.1.1 → 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.
- package/dist/index.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +132 -100
- package/dist/index.mjs +120 -94
- package/dist/use_form_store.d.mts +2 -2
- package/dist/use_form_store.d.ts +2 -2
- package/dist/use_form_store.js +126 -94
- package/dist/use_form_store.mjs +117 -89
- package/dist/use_store_component.d.mts +21 -0
- package/dist/use_store_component.d.ts +21 -0
- package/dist/use_store_component.js +258 -0
- package/dist/use_store_component.mjs +222 -0
- package/dist/use_store_controller.d.mts +16 -0
- package/dist/use_store_controller.d.ts +16 -0
- package/dist/use_store_controller.js +82 -0
- package/dist/use_store_controller.mjs +57 -0
- package/dist/use_store_input.d.mts +19 -14
- package/dist/use_store_input.d.ts +19 -14
- package/dist/use_store_input.js +95 -133
- package/dist/use_store_input.mjs +97 -128
- package/dist/use_store_input_with_name.d.mts +17 -0
- package/dist/use_store_input_with_name.d.ts +17 -0
- package/dist/use_store_input_with_name.js +201 -0
- package/dist/use_store_input_with_name.mjs +174 -0
- package/package.json +43 -43
- package/dist/use_store_input_props.d.mts +0 -24
- package/dist/use_store_input_props.d.ts +0 -24
- package/dist/use_store_input_props.js +0 -154
- package/dist/use_store_input_props.mjs +0 -133
|
@@ -0,0 +1,258 @@
|
|
|
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_component.tsx
|
|
31
|
+
var use_store_component_exports = {};
|
|
32
|
+
__export(use_store_component_exports, {
|
|
33
|
+
Input: () => Input,
|
|
34
|
+
Select: () => Select,
|
|
35
|
+
Textarea: () => Textarea,
|
|
36
|
+
useStoreComponent: () => useStoreComponent
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(use_store_component_exports);
|
|
39
|
+
var import_react4 = require("react");
|
|
40
|
+
var import_react5 = __toESM(require("react"));
|
|
41
|
+
|
|
42
|
+
// src/use_store_input.tsx
|
|
43
|
+
var import_react3 = require("react");
|
|
44
|
+
var import_date_fns = require("date-fns");
|
|
45
|
+
|
|
46
|
+
// src/use_store_controller.tsx
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
|
|
49
|
+
// src/use_subscribe.tsx
|
|
50
|
+
var import_react = require("react");
|
|
51
|
+
function useSubscribe(store, subscriber) {
|
|
52
|
+
(0, import_react.useEffect)(() => {
|
|
53
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
54
|
+
return () => {
|
|
55
|
+
unsubscribe();
|
|
56
|
+
};
|
|
57
|
+
}, [store]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/use_store_controller.tsx
|
|
61
|
+
function useStoreController(store, props) {
|
|
62
|
+
const ref = (0, import_react2.useRef)(null);
|
|
63
|
+
const dispatchKey = (0, import_react2.useId)();
|
|
64
|
+
(0, import_react2.useEffect)(() => {
|
|
65
|
+
const element = ref.current;
|
|
66
|
+
if (!element) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (typeof props.ref === "function") {
|
|
70
|
+
props.ref(element);
|
|
71
|
+
} else if (props.ref) {
|
|
72
|
+
props.ref.current = element;
|
|
73
|
+
}
|
|
74
|
+
}, []);
|
|
75
|
+
useSubscribe(store, (state, key) => {
|
|
76
|
+
if (key === dispatchKey) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!ref.current) return;
|
|
80
|
+
props.onSubscribe(state, ref.current);
|
|
81
|
+
});
|
|
82
|
+
const onChange = () => {
|
|
83
|
+
store.dispatch(
|
|
84
|
+
(state) => {
|
|
85
|
+
const element = ref.current;
|
|
86
|
+
if (!element) return;
|
|
87
|
+
props.onDispatch(state, element);
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
key: dispatchKey
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
dispatchKey,
|
|
96
|
+
ref,
|
|
97
|
+
onChange
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/use_store_input.tsx
|
|
102
|
+
function useStoreInput(store, props) {
|
|
103
|
+
const toInputValue = (value) => {
|
|
104
|
+
if (value === void 0 || value === null) {
|
|
105
|
+
return "";
|
|
106
|
+
}
|
|
107
|
+
if (props.type === "datetime-local") {
|
|
108
|
+
if (value instanceof Date) {
|
|
109
|
+
return (0, import_date_fns.format)(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === "string") {
|
|
112
|
+
return toInputValue(new Date(value));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return String(value);
|
|
116
|
+
};
|
|
117
|
+
const getDefaultValue = () => {
|
|
118
|
+
if (props.defaultValue !== void 0) {
|
|
119
|
+
return props.defaultValue;
|
|
120
|
+
}
|
|
121
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
return toInputValue(props.getter(store.state));
|
|
125
|
+
};
|
|
126
|
+
const toInputChecked = (value) => {
|
|
127
|
+
if (props.type === "radio") {
|
|
128
|
+
return value !== void 0 && value === props.value;
|
|
129
|
+
}
|
|
130
|
+
return Boolean(value);
|
|
131
|
+
};
|
|
132
|
+
const getDefaultChecked = () => {
|
|
133
|
+
if (props.defaultChecked) {
|
|
134
|
+
return props.defaultChecked;
|
|
135
|
+
}
|
|
136
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
137
|
+
return void 0;
|
|
138
|
+
}
|
|
139
|
+
return toInputChecked(props.getter(store.state));
|
|
140
|
+
};
|
|
141
|
+
function toStateValue(value) {
|
|
142
|
+
const selected = props.getter(store.state);
|
|
143
|
+
if (typeof selected === "number") {
|
|
144
|
+
return Number(value);
|
|
145
|
+
}
|
|
146
|
+
if (selected instanceof Date) {
|
|
147
|
+
return new Date(value);
|
|
148
|
+
}
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
const inputProps = useStoreController(store, {
|
|
152
|
+
ref: props.ref,
|
|
153
|
+
onSubscribe: (state, element) => {
|
|
154
|
+
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
155
|
+
const checked = toInputChecked(props.getter(state));
|
|
156
|
+
if (element.checked === checked) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
element.checked = checked;
|
|
160
|
+
} else {
|
|
161
|
+
const value = toInputValue(props.getter(state));
|
|
162
|
+
if (element.value === value) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
element.value = value;
|
|
166
|
+
}
|
|
167
|
+
const event = new Event("input", { bubbles: true });
|
|
168
|
+
element.dispatchEvent(event);
|
|
169
|
+
},
|
|
170
|
+
onDispatch: (state, element) => {
|
|
171
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
172
|
+
props.setter(state, element.checked);
|
|
173
|
+
} else {
|
|
174
|
+
props.setter(state, toStateValue(element.value));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
ref: inputProps.ref,
|
|
180
|
+
dispatchKey: inputProps.dispatchKey,
|
|
181
|
+
defaultValue: getDefaultValue(),
|
|
182
|
+
defaultChecked: getDefaultChecked(),
|
|
183
|
+
onChange: (event) => {
|
|
184
|
+
inputProps.onChange();
|
|
185
|
+
props.onChange?.(event);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/use_store_input_with_name.tsx
|
|
191
|
+
function useStoreInputWithName(store, props) {
|
|
192
|
+
const inputProps = useStoreInput(store, {
|
|
193
|
+
...props,
|
|
194
|
+
getter: (state) => {
|
|
195
|
+
if (props.getter) {
|
|
196
|
+
return props.getter(state);
|
|
197
|
+
}
|
|
198
|
+
return state[props.name];
|
|
199
|
+
},
|
|
200
|
+
setter: (state, value) => {
|
|
201
|
+
if (props.setter) {
|
|
202
|
+
props.setter(state, value);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
state[props.name] = value;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
return {
|
|
209
|
+
...inputProps,
|
|
210
|
+
name: "name" in props ? String(props.name) : void 0
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/use_store_component.tsx
|
|
215
|
+
function useStoreComponent(store) {
|
|
216
|
+
const input = (0, import_react4.useCallback)(function Component(props) {
|
|
217
|
+
return /* @__PURE__ */ import_react5.default.createElement(Input, { store, ...props });
|
|
218
|
+
}, []);
|
|
219
|
+
const select = (0, import_react4.useCallback)(function Component(props) {
|
|
220
|
+
return /* @__PURE__ */ import_react5.default.createElement(Select, { store, ...props });
|
|
221
|
+
}, []);
|
|
222
|
+
const textarea = (0, import_react4.useCallback)(function Component(props) {
|
|
223
|
+
return /* @__PURE__ */ import_react5.default.createElement(Textarea, { store, ...props });
|
|
224
|
+
}, []);
|
|
225
|
+
return {
|
|
226
|
+
input,
|
|
227
|
+
select,
|
|
228
|
+
textarea
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function Input({
|
|
232
|
+
store,
|
|
233
|
+
...props
|
|
234
|
+
}) {
|
|
235
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
236
|
+
return /* @__PURE__ */ import_react5.default.createElement("input", { ...props, ...storeProps });
|
|
237
|
+
}
|
|
238
|
+
function Select({
|
|
239
|
+
store,
|
|
240
|
+
...props
|
|
241
|
+
}) {
|
|
242
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
243
|
+
return /* @__PURE__ */ import_react5.default.createElement("select", { ...props, ...storeProps });
|
|
244
|
+
}
|
|
245
|
+
function Textarea({
|
|
246
|
+
store,
|
|
247
|
+
...props
|
|
248
|
+
}) {
|
|
249
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
250
|
+
return /* @__PURE__ */ import_react5.default.createElement("textarea", { ...props, ...storeProps });
|
|
251
|
+
}
|
|
252
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
253
|
+
0 && (module.exports = {
|
|
254
|
+
Input,
|
|
255
|
+
Select,
|
|
256
|
+
Textarea,
|
|
257
|
+
useStoreComponent
|
|
258
|
+
});
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// src/use_store_component.tsx
|
|
2
|
+
import {
|
|
3
|
+
useCallback
|
|
4
|
+
} from "react";
|
|
5
|
+
import React from "react";
|
|
6
|
+
|
|
7
|
+
// src/use_store_input.tsx
|
|
8
|
+
import "react";
|
|
9
|
+
import { format } from "date-fns";
|
|
10
|
+
|
|
11
|
+
// src/use_store_controller.tsx
|
|
12
|
+
import { useEffect as useEffect2, useId, useRef } from "react";
|
|
13
|
+
|
|
14
|
+
// src/use_subscribe.tsx
|
|
15
|
+
import { useEffect } from "react";
|
|
16
|
+
function useSubscribe(store, subscriber) {
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
19
|
+
return () => {
|
|
20
|
+
unsubscribe();
|
|
21
|
+
};
|
|
22
|
+
}, [store]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/use_store_controller.tsx
|
|
26
|
+
function useStoreController(store, props) {
|
|
27
|
+
const ref = useRef(null);
|
|
28
|
+
const dispatchKey = useId();
|
|
29
|
+
useEffect2(() => {
|
|
30
|
+
const element = ref.current;
|
|
31
|
+
if (!element) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (typeof props.ref === "function") {
|
|
35
|
+
props.ref(element);
|
|
36
|
+
} else if (props.ref) {
|
|
37
|
+
props.ref.current = element;
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
useSubscribe(store, (state, key) => {
|
|
41
|
+
if (key === dispatchKey) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!ref.current) return;
|
|
45
|
+
props.onSubscribe(state, ref.current);
|
|
46
|
+
});
|
|
47
|
+
const onChange = () => {
|
|
48
|
+
store.dispatch(
|
|
49
|
+
(state) => {
|
|
50
|
+
const element = ref.current;
|
|
51
|
+
if (!element) return;
|
|
52
|
+
props.onDispatch(state, element);
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
key: dispatchKey
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
dispatchKey,
|
|
61
|
+
ref,
|
|
62
|
+
onChange
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/use_store_input.tsx
|
|
67
|
+
function useStoreInput(store, props) {
|
|
68
|
+
const toInputValue = (value) => {
|
|
69
|
+
if (value === void 0 || value === null) {
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
if (props.type === "datetime-local") {
|
|
73
|
+
if (value instanceof Date) {
|
|
74
|
+
return format(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
75
|
+
}
|
|
76
|
+
if (typeof value === "string") {
|
|
77
|
+
return toInputValue(new Date(value));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return String(value);
|
|
81
|
+
};
|
|
82
|
+
const getDefaultValue = () => {
|
|
83
|
+
if (props.defaultValue !== void 0) {
|
|
84
|
+
return props.defaultValue;
|
|
85
|
+
}
|
|
86
|
+
if (props.type === "checkbox" || props.type === "radio") {
|
|
87
|
+
return void 0;
|
|
88
|
+
}
|
|
89
|
+
return toInputValue(props.getter(store.state));
|
|
90
|
+
};
|
|
91
|
+
const toInputChecked = (value) => {
|
|
92
|
+
if (props.type === "radio") {
|
|
93
|
+
return value !== void 0 && value === props.value;
|
|
94
|
+
}
|
|
95
|
+
return Boolean(value);
|
|
96
|
+
};
|
|
97
|
+
const getDefaultChecked = () => {
|
|
98
|
+
if (props.defaultChecked) {
|
|
99
|
+
return props.defaultChecked;
|
|
100
|
+
}
|
|
101
|
+
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
return toInputChecked(props.getter(store.state));
|
|
105
|
+
};
|
|
106
|
+
function toStateValue(value) {
|
|
107
|
+
const selected = props.getter(store.state);
|
|
108
|
+
if (typeof selected === "number") {
|
|
109
|
+
return Number(value);
|
|
110
|
+
}
|
|
111
|
+
if (selected instanceof Date) {
|
|
112
|
+
return new Date(value);
|
|
113
|
+
}
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
const inputProps = useStoreController(store, {
|
|
117
|
+
ref: props.ref,
|
|
118
|
+
onSubscribe: (state, element) => {
|
|
119
|
+
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
120
|
+
const checked = toInputChecked(props.getter(state));
|
|
121
|
+
if (element.checked === checked) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
element.checked = checked;
|
|
125
|
+
} else {
|
|
126
|
+
const value = toInputValue(props.getter(state));
|
|
127
|
+
if (element.value === value) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
element.value = value;
|
|
131
|
+
}
|
|
132
|
+
const event = new Event("input", { bubbles: true });
|
|
133
|
+
element.dispatchEvent(event);
|
|
134
|
+
},
|
|
135
|
+
onDispatch: (state, element) => {
|
|
136
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
137
|
+
props.setter(state, element.checked);
|
|
138
|
+
} else {
|
|
139
|
+
props.setter(state, toStateValue(element.value));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return {
|
|
144
|
+
ref: inputProps.ref,
|
|
145
|
+
dispatchKey: inputProps.dispatchKey,
|
|
146
|
+
defaultValue: getDefaultValue(),
|
|
147
|
+
defaultChecked: getDefaultChecked(),
|
|
148
|
+
onChange: (event) => {
|
|
149
|
+
inputProps.onChange();
|
|
150
|
+
props.onChange?.(event);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/use_store_input_with_name.tsx
|
|
156
|
+
function useStoreInputWithName(store, props) {
|
|
157
|
+
const inputProps = useStoreInput(store, {
|
|
158
|
+
...props,
|
|
159
|
+
getter: (state) => {
|
|
160
|
+
if (props.getter) {
|
|
161
|
+
return props.getter(state);
|
|
162
|
+
}
|
|
163
|
+
return state[props.name];
|
|
164
|
+
},
|
|
165
|
+
setter: (state, value) => {
|
|
166
|
+
if (props.setter) {
|
|
167
|
+
props.setter(state, value);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
state[props.name] = value;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
...inputProps,
|
|
175
|
+
name: "name" in props ? String(props.name) : void 0
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/use_store_component.tsx
|
|
180
|
+
function useStoreComponent(store) {
|
|
181
|
+
const input = useCallback(function Component(props) {
|
|
182
|
+
return /* @__PURE__ */ React.createElement(Input, { store, ...props });
|
|
183
|
+
}, []);
|
|
184
|
+
const select = useCallback(function Component(props) {
|
|
185
|
+
return /* @__PURE__ */ React.createElement(Select, { store, ...props });
|
|
186
|
+
}, []);
|
|
187
|
+
const textarea = useCallback(function Component(props) {
|
|
188
|
+
return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
|
|
189
|
+
}, []);
|
|
190
|
+
return {
|
|
191
|
+
input,
|
|
192
|
+
select,
|
|
193
|
+
textarea
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function Input({
|
|
197
|
+
store,
|
|
198
|
+
...props
|
|
199
|
+
}) {
|
|
200
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
201
|
+
return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
|
|
202
|
+
}
|
|
203
|
+
function Select({
|
|
204
|
+
store,
|
|
205
|
+
...props
|
|
206
|
+
}) {
|
|
207
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
208
|
+
return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
|
|
209
|
+
}
|
|
210
|
+
function Textarea({
|
|
211
|
+
store,
|
|
212
|
+
...props
|
|
213
|
+
}) {
|
|
214
|
+
const storeProps = useStoreInputWithName(store, props);
|
|
215
|
+
return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
|
|
216
|
+
}
|
|
217
|
+
export {
|
|
218
|
+
Input,
|
|
219
|
+
Select,
|
|
220
|
+
Textarea,
|
|
221
|
+
useStoreComponent
|
|
222
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Ref } from 'react';
|
|
3
|
+
import { Store } from './use_store.mjs';
|
|
4
|
+
|
|
5
|
+
type StoreControllerProps<TController, TState> = {
|
|
6
|
+
ref?: Ref<TController>;
|
|
7
|
+
onSubscribe: (state: TState, element: TController) => void;
|
|
8
|
+
onDispatch: (state: TState, element: TController) => void;
|
|
9
|
+
};
|
|
10
|
+
declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
|
|
11
|
+
dispatchKey: string;
|
|
12
|
+
ref: React.RefObject<TController | null>;
|
|
13
|
+
onChange: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { type StoreControllerProps, useStoreController };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Ref } from 'react';
|
|
3
|
+
import { Store } from './use_store.js';
|
|
4
|
+
|
|
5
|
+
type StoreControllerProps<TController, TState> = {
|
|
6
|
+
ref?: Ref<TController>;
|
|
7
|
+
onSubscribe: (state: TState, element: TController) => void;
|
|
8
|
+
onDispatch: (state: TState, element: TController) => void;
|
|
9
|
+
};
|
|
10
|
+
declare function useStoreController<TController, TState>(store: Store<TState>, props: StoreControllerProps<TController, TState>): {
|
|
11
|
+
dispatchKey: string;
|
|
12
|
+
ref: React.RefObject<TController | null>;
|
|
13
|
+
onChange: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export { type StoreControllerProps, useStoreController };
|
|
@@ -0,0 +1,82 @@
|
|
|
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_controller.tsx
|
|
21
|
+
var use_store_controller_exports = {};
|
|
22
|
+
__export(use_store_controller_exports, {
|
|
23
|
+
useStoreController: () => useStoreController
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_store_controller_exports);
|
|
26
|
+
var import_react2 = require("react");
|
|
27
|
+
|
|
28
|
+
// src/use_subscribe.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
function useSubscribe(store, subscriber) {
|
|
31
|
+
(0, import_react.useEffect)(() => {
|
|
32
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
33
|
+
return () => {
|
|
34
|
+
unsubscribe();
|
|
35
|
+
};
|
|
36
|
+
}, [store]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/use_store_controller.tsx
|
|
40
|
+
function useStoreController(store, props) {
|
|
41
|
+
const ref = (0, import_react2.useRef)(null);
|
|
42
|
+
const dispatchKey = (0, import_react2.useId)();
|
|
43
|
+
(0, import_react2.useEffect)(() => {
|
|
44
|
+
const element = ref.current;
|
|
45
|
+
if (!element) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (typeof props.ref === "function") {
|
|
49
|
+
props.ref(element);
|
|
50
|
+
} else if (props.ref) {
|
|
51
|
+
props.ref.current = element;
|
|
52
|
+
}
|
|
53
|
+
}, []);
|
|
54
|
+
useSubscribe(store, (state, key) => {
|
|
55
|
+
if (key === dispatchKey) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!ref.current) return;
|
|
59
|
+
props.onSubscribe(state, ref.current);
|
|
60
|
+
});
|
|
61
|
+
const onChange = () => {
|
|
62
|
+
store.dispatch(
|
|
63
|
+
(state) => {
|
|
64
|
+
const element = ref.current;
|
|
65
|
+
if (!element) return;
|
|
66
|
+
props.onDispatch(state, element);
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
key: dispatchKey
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
dispatchKey,
|
|
75
|
+
ref,
|
|
76
|
+
onChange
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
useStoreController
|
|
82
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/use_store_controller.tsx
|
|
2
|
+
import { useEffect as useEffect2, useId, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
// src/use_subscribe.tsx
|
|
5
|
+
import { useEffect } from "react";
|
|
6
|
+
function useSubscribe(store, subscriber) {
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
9
|
+
return () => {
|
|
10
|
+
unsubscribe();
|
|
11
|
+
};
|
|
12
|
+
}, [store]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/use_store_controller.tsx
|
|
16
|
+
function useStoreController(store, props) {
|
|
17
|
+
const ref = useRef(null);
|
|
18
|
+
const dispatchKey = useId();
|
|
19
|
+
useEffect2(() => {
|
|
20
|
+
const element = ref.current;
|
|
21
|
+
if (!element) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (typeof props.ref === "function") {
|
|
25
|
+
props.ref(element);
|
|
26
|
+
} else if (props.ref) {
|
|
27
|
+
props.ref.current = element;
|
|
28
|
+
}
|
|
29
|
+
}, []);
|
|
30
|
+
useSubscribe(store, (state, key) => {
|
|
31
|
+
if (key === dispatchKey) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (!ref.current) return;
|
|
35
|
+
props.onSubscribe(state, ref.current);
|
|
36
|
+
});
|
|
37
|
+
const onChange = () => {
|
|
38
|
+
store.dispatch(
|
|
39
|
+
(state) => {
|
|
40
|
+
const element = ref.current;
|
|
41
|
+
if (!element) return;
|
|
42
|
+
props.onDispatch(state, element);
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
key: dispatchKey
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
dispatchKey,
|
|
51
|
+
ref,
|
|
52
|
+
onChange
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
useStoreController
|
|
57
|
+
};
|
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React$1 from 'react';
|
|
2
|
+
import { Ref, HTMLInputTypeAttribute } from 'react';
|
|
2
3
|
import { Store } from './use_store.mjs';
|
|
3
4
|
|
|
4
|
-
type StoreInputProps<
|
|
5
|
-
|
|
5
|
+
type StoreInputProps<TInputElement, TState> = {
|
|
6
|
+
ref?: Ref<TInputElement>;
|
|
7
|
+
type?: HTMLInputTypeAttribute;
|
|
8
|
+
defaultValue?: string | number | readonly string[] | undefined;
|
|
9
|
+
value?: string | number | readonly string[] | undefined;
|
|
10
|
+
defaultChecked?: boolean | undefined;
|
|
11
|
+
onChange?: (event: React.ChangeEvent<TInputElement>) => void;
|
|
12
|
+
} & {
|
|
13
|
+
getter: (state: TState) => unknown;
|
|
14
|
+
setter: (state: TState, value: unknown) => void;
|
|
6
15
|
};
|
|
7
|
-
declare function useStoreInput<TState>(store: Store<TState>): {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: StoreInputProps<TInputElement, TState>): {
|
|
17
|
+
ref: React$1.RefObject<TInputElement | null>;
|
|
18
|
+
dispatchKey: string;
|
|
19
|
+
defaultValue: string | number | readonly string[] | undefined;
|
|
20
|
+
defaultChecked: boolean | undefined;
|
|
21
|
+
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
11
22
|
};
|
|
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
23
|
|
|
19
|
-
export {
|
|
24
|
+
export { type StoreInputProps, useStoreInput };
|