react-store-input 0.2.6 → 0.4.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/CHANGELOG.md +114 -0
- package/README.md +179 -201
- package/dist/index.d.mts +127 -10
- package/dist/index.d.ts +127 -10
- package/dist/index.js +554 -249
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +549 -247
- package/dist/index.mjs.map +1 -0
- package/dist/text-editor.d.mts +11 -0
- package/dist/text-editor.d.ts +11 -0
- package/dist/{text_editor.js → text-editor.js} +31 -51
- package/dist/text-editor.js.map +1 -0
- package/dist/text-editor.mjs +90 -0
- package/dist/text-editor.mjs.map +1 -0
- package/package.json +78 -18
- package/dist/create_render.d.mts +0 -9
- package/dist/create_render.d.ts +0 -9
- package/dist/create_render.js +0 -51
- package/dist/create_render.mjs +0 -25
- package/dist/text_editor.d.mts +0 -13
- package/dist/text_editor.d.ts +0 -13
- package/dist/text_editor.mjs +0 -112
- package/dist/use_form_store.d.mts +0 -16
- package/dist/use_form_store.d.ts +0 -16
- package/dist/use_form_store.js +0 -365
- package/dist/use_form_store.mjs +0 -340
- package/dist/use_store_component.d.mts +0 -23
- package/dist/use_store_component.d.ts +0 -23
- package/dist/use_store_component.js +0 -337
- package/dist/use_store_component.mjs +0 -311
- package/dist/use_store_controller.d.mts +0 -11
- package/dist/use_store_controller.d.ts +0 -11
- package/dist/use_store_controller.js +0 -57
- package/dist/use_store_controller.mjs +0 -32
- package/dist/use_store_input.d.mts +0 -22
- package/dist/use_store_input.d.ts +0 -22
- package/dist/use_store_input.js +0 -151
- package/dist/use_store_input.mjs +0 -126
- package/dist/use_store_input_with_name.d.mts +0 -17
- package/dist/use_store_input_with_name.d.ts +0 -17
- package/dist/use_store_input_with_name.js +0 -177
- package/dist/use_store_input_with_name.mjs +0 -150
- package/tsup.config.ts +0 -9
package/dist/use_form_store.mjs
DELETED
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
// src/create_render.tsx
|
|
2
|
-
import { useSelector } from "gw-store";
|
|
3
|
-
import { Fragment, jsx } from "react/jsx-runtime";
|
|
4
|
-
function createRenderWithStore(store) {
|
|
5
|
-
return function createRender(selector, compare) {
|
|
6
|
-
function Component() {
|
|
7
|
-
const result = useSelector(store, (state) => state, compare);
|
|
8
|
-
const content = selector(result);
|
|
9
|
-
return /* @__PURE__ */ jsx(Fragment, { children: content });
|
|
10
|
-
}
|
|
11
|
-
return /* @__PURE__ */ jsx(Component, {});
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// src/use_form_store.tsx
|
|
16
|
-
import { useStore } from "gw-store";
|
|
17
|
-
|
|
18
|
-
// src/use_store_component.tsx
|
|
19
|
-
import { useCallback, useRef as useRef2 } from "react";
|
|
20
|
-
|
|
21
|
-
// src/use_store_input.tsx
|
|
22
|
-
import { format } from "date-fns";
|
|
23
|
-
|
|
24
|
-
// src/use_store_controller.tsx
|
|
25
|
-
import { useEffect, useId } from "react";
|
|
26
|
-
function useStoreController(store, props) {
|
|
27
|
-
const dispatchKey = useId();
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
const unsub = store.subscribe((state, key) => {
|
|
30
|
-
if (key === dispatchKey) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
props.onSubscribe(state);
|
|
34
|
-
});
|
|
35
|
-
return () => {
|
|
36
|
-
unsub();
|
|
37
|
-
};
|
|
38
|
-
}, []);
|
|
39
|
-
const dispatch = () => {
|
|
40
|
-
store.dispatch(
|
|
41
|
-
(state) => {
|
|
42
|
-
props.onDispatch(state);
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
key: dispatchKey
|
|
46
|
-
}
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
return {
|
|
50
|
-
dispatch
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/use_store_input.tsx
|
|
55
|
-
function useStoreInput(ref, store, props) {
|
|
56
|
-
const toInputValue = props.toInputValue || ((value) => {
|
|
57
|
-
if (value === void 0 || value === null) {
|
|
58
|
-
return "";
|
|
59
|
-
}
|
|
60
|
-
if (props.type === "datetime-local") {
|
|
61
|
-
const date = new Date(value);
|
|
62
|
-
if (!isNaN(date.getTime())) {
|
|
63
|
-
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return String(value);
|
|
67
|
-
});
|
|
68
|
-
const getDefaultValue = () => {
|
|
69
|
-
if (props.defaultValue !== void 0) {
|
|
70
|
-
return props.defaultValue;
|
|
71
|
-
}
|
|
72
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
73
|
-
return void 0;
|
|
74
|
-
}
|
|
75
|
-
return toInputValue(props.getter(store.state));
|
|
76
|
-
};
|
|
77
|
-
const toInputChecked = (value) => {
|
|
78
|
-
if (props.type === "radio") {
|
|
79
|
-
return value !== void 0 && value === props.value;
|
|
80
|
-
}
|
|
81
|
-
return Boolean(value);
|
|
82
|
-
};
|
|
83
|
-
const getDefaultChecked = () => {
|
|
84
|
-
if (props.defaultChecked) {
|
|
85
|
-
return props.defaultChecked;
|
|
86
|
-
}
|
|
87
|
-
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
88
|
-
return void 0;
|
|
89
|
-
}
|
|
90
|
-
return toInputChecked(props.getter(store.state));
|
|
91
|
-
};
|
|
92
|
-
const toStateValue = props.toStateValue || ((value) => {
|
|
93
|
-
if (props.type === "number" || props.type === "range") {
|
|
94
|
-
return Number(value);
|
|
95
|
-
}
|
|
96
|
-
if (props.type === "datetime-local") {
|
|
97
|
-
return new Date(value);
|
|
98
|
-
}
|
|
99
|
-
return value;
|
|
100
|
-
});
|
|
101
|
-
const inputProps = useStoreController(store, {
|
|
102
|
-
onSubscribe: (state) => {
|
|
103
|
-
const element = ref.current;
|
|
104
|
-
if (!element) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
108
|
-
const checked = toInputChecked(props.getter(state));
|
|
109
|
-
if (element.checked === checked) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
element.checked = checked;
|
|
113
|
-
} else {
|
|
114
|
-
const value = toInputValue(props.getter(state));
|
|
115
|
-
if (element.value === value) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
element.value = value;
|
|
119
|
-
}
|
|
120
|
-
const event = new Event("input", { bubbles: true });
|
|
121
|
-
element.dispatchEvent(event);
|
|
122
|
-
},
|
|
123
|
-
onDispatch: (state) => {
|
|
124
|
-
const element = ref.current;
|
|
125
|
-
if (!element) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
if ("checked" in element && props.type === "checkbox") {
|
|
129
|
-
props.setter(state, element.checked);
|
|
130
|
-
} else {
|
|
131
|
-
props.setter(state, toStateValue(element.value));
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
return {
|
|
136
|
-
defaultValue: getDefaultValue(),
|
|
137
|
-
defaultChecked: getDefaultChecked(),
|
|
138
|
-
onChange: (event) => {
|
|
139
|
-
inputProps.dispatch();
|
|
140
|
-
props.onChange?.(event);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// src/use_store_input_with_name.tsx
|
|
146
|
-
function useStoreInputWithName(ref, store, props) {
|
|
147
|
-
const inputProps = useStoreInput(ref, store, {
|
|
148
|
-
...props,
|
|
149
|
-
getter: (state) => {
|
|
150
|
-
if (props.getter) {
|
|
151
|
-
return props.getter(state);
|
|
152
|
-
}
|
|
153
|
-
return state[props.name];
|
|
154
|
-
},
|
|
155
|
-
setter: (state, value) => {
|
|
156
|
-
if (props.setter) {
|
|
157
|
-
props.setter(state, value);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
state[props.name] = value;
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
return {
|
|
164
|
-
...inputProps,
|
|
165
|
-
name: "name" in props ? String(props.name) : void 0
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// src/text_editor.tsx
|
|
170
|
-
import {
|
|
171
|
-
TextEditor as GwTextEditor
|
|
172
|
-
} from "gw-react-text-editor";
|
|
173
|
-
import { useImperativeHandle, useRef } from "react";
|
|
174
|
-
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
175
|
-
function TextEditor({
|
|
176
|
-
store,
|
|
177
|
-
name,
|
|
178
|
-
getter,
|
|
179
|
-
setter,
|
|
180
|
-
defaultValue,
|
|
181
|
-
ref,
|
|
182
|
-
...props
|
|
183
|
-
}) {
|
|
184
|
-
const controllerRef = useRef(null);
|
|
185
|
-
useImperativeHandle(
|
|
186
|
-
ref,
|
|
187
|
-
() => controllerRef.current,
|
|
188
|
-
[]
|
|
189
|
-
);
|
|
190
|
-
const { dispatch } = useStoreController(store, {
|
|
191
|
-
onSubscribe: (state) => {
|
|
192
|
-
const controller = controllerRef.current;
|
|
193
|
-
if (!controller) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
const getResult = () => {
|
|
197
|
-
if (getter) {
|
|
198
|
-
return getter(state) || "";
|
|
199
|
-
}
|
|
200
|
-
if (name) {
|
|
201
|
-
return state[name] || "";
|
|
202
|
-
}
|
|
203
|
-
return "";
|
|
204
|
-
};
|
|
205
|
-
const result = getResult();
|
|
206
|
-
if (controller.value !== result) {
|
|
207
|
-
controller.value = result;
|
|
208
|
-
}
|
|
209
|
-
},
|
|
210
|
-
onDispatch: (state) => {
|
|
211
|
-
const controller = controllerRef.current;
|
|
212
|
-
if (!controller) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
if (setter) {
|
|
216
|
-
setter(state, controller.value);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (name) {
|
|
220
|
-
state[name] = controller.value;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
const getDefaultValue = () => {
|
|
225
|
-
if (getter) {
|
|
226
|
-
return getter(store.state);
|
|
227
|
-
}
|
|
228
|
-
if (name) {
|
|
229
|
-
return store.state[name];
|
|
230
|
-
}
|
|
231
|
-
return void 0;
|
|
232
|
-
};
|
|
233
|
-
return /* @__PURE__ */ jsx2(
|
|
234
|
-
GwTextEditor,
|
|
235
|
-
{
|
|
236
|
-
...props,
|
|
237
|
-
ref: controllerRef,
|
|
238
|
-
defaultValue: defaultValue ?? getDefaultValue(),
|
|
239
|
-
onChange: (e) => {
|
|
240
|
-
dispatch();
|
|
241
|
-
props.onChange?.(e);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// src/use_store_component.tsx
|
|
248
|
-
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
249
|
-
function useStoreComponent(store) {
|
|
250
|
-
const input = useCallback(function Component(props) {
|
|
251
|
-
return /* @__PURE__ */ jsx3(Input, { store, ...props });
|
|
252
|
-
}, []);
|
|
253
|
-
const select = useCallback(function Component(props) {
|
|
254
|
-
return /* @__PURE__ */ jsx3(Select, { store, ...props });
|
|
255
|
-
}, []);
|
|
256
|
-
const textarea = useCallback(function Component(props) {
|
|
257
|
-
return /* @__PURE__ */ jsx3(Textarea, { store, ...props });
|
|
258
|
-
}, []);
|
|
259
|
-
const textEditor = useCallback(function Component(props) {
|
|
260
|
-
return /* @__PURE__ */ jsx3(TextEditor, { store, ...props });
|
|
261
|
-
}, []);
|
|
262
|
-
return {
|
|
263
|
-
input,
|
|
264
|
-
select,
|
|
265
|
-
textarea,
|
|
266
|
-
textEditor
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
function Input({
|
|
270
|
-
store,
|
|
271
|
-
getter,
|
|
272
|
-
setter,
|
|
273
|
-
toInputValue,
|
|
274
|
-
toStateValue,
|
|
275
|
-
...props
|
|
276
|
-
}) {
|
|
277
|
-
const ref = useRef2(null);
|
|
278
|
-
const storeProps = useStoreInputWithName(ref, store, {
|
|
279
|
-
...props,
|
|
280
|
-
getter,
|
|
281
|
-
setter,
|
|
282
|
-
toInputValue,
|
|
283
|
-
toStateValue
|
|
284
|
-
});
|
|
285
|
-
return /* @__PURE__ */ jsx3("input", { ref, ...props, ...storeProps });
|
|
286
|
-
}
|
|
287
|
-
function Select({
|
|
288
|
-
store,
|
|
289
|
-
getter,
|
|
290
|
-
setter,
|
|
291
|
-
toInputValue,
|
|
292
|
-
toStateValue,
|
|
293
|
-
...props
|
|
294
|
-
}) {
|
|
295
|
-
const ref = useRef2(null);
|
|
296
|
-
const storeProps = useStoreInputWithName(ref, store, {
|
|
297
|
-
...props,
|
|
298
|
-
getter,
|
|
299
|
-
setter,
|
|
300
|
-
toInputValue,
|
|
301
|
-
toStateValue
|
|
302
|
-
});
|
|
303
|
-
return /* @__PURE__ */ jsx3("select", { ref, ...props, ...storeProps });
|
|
304
|
-
}
|
|
305
|
-
function Textarea({
|
|
306
|
-
store,
|
|
307
|
-
getter,
|
|
308
|
-
setter,
|
|
309
|
-
toInputValue,
|
|
310
|
-
toStateValue,
|
|
311
|
-
...props
|
|
312
|
-
}) {
|
|
313
|
-
const ref = useRef2(null);
|
|
314
|
-
const storeProps = useStoreInputWithName(ref, store, {
|
|
315
|
-
...props,
|
|
316
|
-
getter,
|
|
317
|
-
setter,
|
|
318
|
-
toInputValue,
|
|
319
|
-
toStateValue
|
|
320
|
-
});
|
|
321
|
-
return /* @__PURE__ */ jsx3("textarea", { ref, ...props, ...storeProps });
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// src/use_form_store.tsx
|
|
325
|
-
function useFormStore(initialState) {
|
|
326
|
-
const store = useStore(initialState);
|
|
327
|
-
const storeComponent = useStoreComponent(store);
|
|
328
|
-
return {
|
|
329
|
-
get state() {
|
|
330
|
-
return store.state;
|
|
331
|
-
},
|
|
332
|
-
dispatch: store.dispatch,
|
|
333
|
-
subscribe: store.subscribe,
|
|
334
|
-
render: createRenderWithStore(store),
|
|
335
|
-
...storeComponent
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
export {
|
|
339
|
-
useFormStore
|
|
340
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { DetailedHTMLProps } from 'react';
|
|
3
|
-
import { Store } from 'gw-store';
|
|
4
|
-
import { StoreInputWithNameProps } from './use_store_input_with_name.mjs';
|
|
5
|
-
import { TextEditorProps } from './text_editor.mjs';
|
|
6
|
-
import './use_store_input.mjs';
|
|
7
|
-
import 'gw-react-text-editor';
|
|
8
|
-
|
|
9
|
-
type StoreInputWithNameComponentProps<TElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & Omit<DetailedHTMLProps<React.InputHTMLAttributes<TElement>, TElement>, keyof StoreInputWithNameProps<TElement, TState, TName, TValue>>;
|
|
10
|
-
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
11
|
-
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLInputElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
12
|
-
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLSelectElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
13
|
-
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLTextAreaElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
14
|
-
textEditor: (props: Omit<TextEditorProps<TState>, "store">) => react_jsx_runtime.JSX.Element;
|
|
15
|
-
};
|
|
16
|
-
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameComponentProps<TElement, TState, TName, TValue> & {
|
|
17
|
-
store: Store<TState>;
|
|
18
|
-
};
|
|
19
|
-
declare function Input<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
20
|
-
declare function Select<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
21
|
-
declare function Textarea<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
22
|
-
|
|
23
|
-
export { Input, Select, type StoreComponentPropsWithStore, type StoreInputWithNameComponentProps, Textarea, useStoreComponent };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { DetailedHTMLProps } from 'react';
|
|
3
|
-
import { Store } from 'gw-store';
|
|
4
|
-
import { StoreInputWithNameProps } from './use_store_input_with_name.js';
|
|
5
|
-
import { TextEditorProps } from './text_editor.js';
|
|
6
|
-
import './use_store_input.js';
|
|
7
|
-
import 'gw-react-text-editor';
|
|
8
|
-
|
|
9
|
-
type StoreInputWithNameComponentProps<TElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameProps<TElement, TState, TName, TValue> & Omit<DetailedHTMLProps<React.InputHTMLAttributes<TElement>, TElement>, keyof StoreInputWithNameProps<TElement, TState, TName, TValue>>;
|
|
10
|
-
declare function useStoreComponent<TState>(store: Store<TState>): {
|
|
11
|
-
input: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLInputElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
12
|
-
select: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLSelectElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
13
|
-
textarea: <TName extends keyof TState | undefined, TValue>(props: StoreInputWithNameComponentProps<HTMLTextAreaElement, TState, TName, TValue>) => react_jsx_runtime.JSX.Element;
|
|
14
|
-
textEditor: (props: Omit<TextEditorProps<TState>, "store">) => react_jsx_runtime.JSX.Element;
|
|
15
|
-
};
|
|
16
|
-
type StoreComponentPropsWithStore<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState, TName extends keyof TState | undefined, TValue> = StoreInputWithNameComponentProps<TElement, TState, TName, TValue> & {
|
|
17
|
-
store: Store<TState>;
|
|
18
|
-
};
|
|
19
|
-
declare function Input<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLInputElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
20
|
-
declare function Select<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLSelectElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
21
|
-
declare function Textarea<TState, TName extends keyof TState | undefined, TValue>({ store, getter, setter, toInputValue, toStateValue, ...props }: StoreComponentPropsWithStore<HTMLTextAreaElement, TState, TName, TValue>): react_jsx_runtime.JSX.Element;
|
|
22
|
-
|
|
23
|
-
export { Input, Select, type StoreComponentPropsWithStore, type StoreInputWithNameComponentProps, Textarea, useStoreComponent };
|