react-store-input 0.2.6 → 0.5.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 +136 -0
- package/README.md +211 -199
- package/dist/index.d.mts +127 -11
- package/dist/index.d.ts +127 -11
- package/dist/index.js +542 -280
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +529 -272
- 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 +203 -0
- package/dist/text-editor.js.map +1 -0
- package/dist/text-editor.mjs +178 -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.js +0 -135
- 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
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Store } from 'gw-store';
|
|
2
|
-
|
|
3
|
-
type StoreControllerProps<TState> = {
|
|
4
|
-
onSubscribe: (state: TState) => void;
|
|
5
|
-
onDispatch: (state: TState) => void;
|
|
6
|
-
};
|
|
7
|
-
declare function useStoreController<TState>(store: Store<TState>, props: StoreControllerProps<TState>): {
|
|
8
|
-
dispatch: () => void;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { type StoreControllerProps, useStoreController };
|
|
@@ -1,57 +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_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_react = require("react");
|
|
27
|
-
function useStoreController(store, props) {
|
|
28
|
-
const dispatchKey = (0, import_react.useId)();
|
|
29
|
-
(0, import_react.useEffect)(() => {
|
|
30
|
-
const unsub = store.subscribe((state, key) => {
|
|
31
|
-
if (key === dispatchKey) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
props.onSubscribe(state);
|
|
35
|
-
});
|
|
36
|
-
return () => {
|
|
37
|
-
unsub();
|
|
38
|
-
};
|
|
39
|
-
}, []);
|
|
40
|
-
const dispatch = () => {
|
|
41
|
-
store.dispatch(
|
|
42
|
-
(state) => {
|
|
43
|
-
props.onDispatch(state);
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
key: dispatchKey
|
|
47
|
-
}
|
|
48
|
-
);
|
|
49
|
-
};
|
|
50
|
-
return {
|
|
51
|
-
dispatch
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
-
0 && (module.exports = {
|
|
56
|
-
useStoreController
|
|
57
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// src/use_store_controller.tsx
|
|
2
|
-
import { useEffect, useId } from "react";
|
|
3
|
-
function useStoreController(store, props) {
|
|
4
|
-
const dispatchKey = useId();
|
|
5
|
-
useEffect(() => {
|
|
6
|
-
const unsub = store.subscribe((state, key) => {
|
|
7
|
-
if (key === dispatchKey) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
props.onSubscribe(state);
|
|
11
|
-
});
|
|
12
|
-
return () => {
|
|
13
|
-
unsub();
|
|
14
|
-
};
|
|
15
|
-
}, []);
|
|
16
|
-
const dispatch = () => {
|
|
17
|
-
store.dispatch(
|
|
18
|
-
(state) => {
|
|
19
|
-
props.onDispatch(state);
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
key: dispatchKey
|
|
23
|
-
}
|
|
24
|
-
);
|
|
25
|
-
};
|
|
26
|
-
return {
|
|
27
|
-
dispatch
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
export {
|
|
31
|
-
useStoreController
|
|
32
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { HTMLInputTypeAttribute, RefObject } from 'react';
|
|
2
|
-
import { Store } from 'gw-store';
|
|
3
|
-
|
|
4
|
-
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
5
|
-
type?: HTMLInputTypeAttribute;
|
|
6
|
-
defaultValue?: string | number | readonly string[] | undefined;
|
|
7
|
-
value?: string | number | readonly string[] | undefined;
|
|
8
|
-
defaultChecked?: boolean | undefined;
|
|
9
|
-
onChange?: (event: React.ChangeEvent<TInputElement>) => void;
|
|
10
|
-
} & {
|
|
11
|
-
getter: (state: TState) => TValue;
|
|
12
|
-
setter: (state: TState, value: TValue) => void;
|
|
13
|
-
toInputValue?: (value: TValue) => string;
|
|
14
|
-
toStateValue?: (value: string) => TValue;
|
|
15
|
-
};
|
|
16
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
17
|
-
defaultValue: string | number | readonly string[] | undefined;
|
|
18
|
-
defaultChecked: boolean | undefined;
|
|
19
|
-
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export { type StoreInputProps, useStoreInput };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { HTMLInputTypeAttribute, RefObject } from 'react';
|
|
2
|
-
import { Store } from 'gw-store';
|
|
3
|
-
|
|
4
|
-
type StoreInputProps<TInputElement, TState, TValue> = {
|
|
5
|
-
type?: HTMLInputTypeAttribute;
|
|
6
|
-
defaultValue?: string | number | readonly string[] | undefined;
|
|
7
|
-
value?: string | number | readonly string[] | undefined;
|
|
8
|
-
defaultChecked?: boolean | undefined;
|
|
9
|
-
onChange?: (event: React.ChangeEvent<TInputElement>) => void;
|
|
10
|
-
} & {
|
|
11
|
-
getter: (state: TState) => TValue;
|
|
12
|
-
setter: (state: TState, value: TValue) => void;
|
|
13
|
-
toInputValue?: (value: TValue) => string;
|
|
14
|
-
toStateValue?: (value: string) => TValue;
|
|
15
|
-
};
|
|
16
|
-
declare function useStoreInput<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputProps<TInputElement, TState, TValue>): {
|
|
17
|
-
defaultValue: string | number | readonly string[] | undefined;
|
|
18
|
-
defaultChecked: boolean | undefined;
|
|
19
|
-
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export { type StoreInputProps, useStoreInput };
|
package/dist/use_store_input.js
DELETED
|
@@ -1,151 +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.tsx
|
|
21
|
-
var use_store_input_exports = {};
|
|
22
|
-
__export(use_store_input_exports, {
|
|
23
|
-
useStoreInput: () => useStoreInput
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(use_store_input_exports);
|
|
26
|
-
var import_date_fns = require("date-fns");
|
|
27
|
-
|
|
28
|
-
// src/use_store_controller.tsx
|
|
29
|
-
var import_react = require("react");
|
|
30
|
-
function useStoreController(store, props) {
|
|
31
|
-
const dispatchKey = (0, import_react.useId)();
|
|
32
|
-
(0, import_react.useEffect)(() => {
|
|
33
|
-
const unsub = store.subscribe((state, key) => {
|
|
34
|
-
if (key === dispatchKey) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
props.onSubscribe(state);
|
|
38
|
-
});
|
|
39
|
-
return () => {
|
|
40
|
-
unsub();
|
|
41
|
-
};
|
|
42
|
-
}, []);
|
|
43
|
-
const dispatch = () => {
|
|
44
|
-
store.dispatch(
|
|
45
|
-
(state) => {
|
|
46
|
-
props.onDispatch(state);
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
key: dispatchKey
|
|
50
|
-
}
|
|
51
|
-
);
|
|
52
|
-
};
|
|
53
|
-
return {
|
|
54
|
-
dispatch
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/use_store_input.tsx
|
|
59
|
-
function useStoreInput(ref, store, props) {
|
|
60
|
-
const toInputValue = props.toInputValue || ((value) => {
|
|
61
|
-
if (value === void 0 || value === null) {
|
|
62
|
-
return "";
|
|
63
|
-
}
|
|
64
|
-
if (props.type === "datetime-local") {
|
|
65
|
-
const date = new Date(value);
|
|
66
|
-
if (!isNaN(date.getTime())) {
|
|
67
|
-
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return String(value);
|
|
71
|
-
});
|
|
72
|
-
const getDefaultValue = () => {
|
|
73
|
-
if (props.defaultValue !== void 0) {
|
|
74
|
-
return props.defaultValue;
|
|
75
|
-
}
|
|
76
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
77
|
-
return void 0;
|
|
78
|
-
}
|
|
79
|
-
return toInputValue(props.getter(store.state));
|
|
80
|
-
};
|
|
81
|
-
const toInputChecked = (value) => {
|
|
82
|
-
if (props.type === "radio") {
|
|
83
|
-
return value !== void 0 && value === props.value;
|
|
84
|
-
}
|
|
85
|
-
return Boolean(value);
|
|
86
|
-
};
|
|
87
|
-
const getDefaultChecked = () => {
|
|
88
|
-
if (props.defaultChecked) {
|
|
89
|
-
return props.defaultChecked;
|
|
90
|
-
}
|
|
91
|
-
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
92
|
-
return void 0;
|
|
93
|
-
}
|
|
94
|
-
return toInputChecked(props.getter(store.state));
|
|
95
|
-
};
|
|
96
|
-
const toStateValue = props.toStateValue || ((value) => {
|
|
97
|
-
if (props.type === "number" || props.type === "range") {
|
|
98
|
-
return Number(value);
|
|
99
|
-
}
|
|
100
|
-
if (props.type === "datetime-local") {
|
|
101
|
-
return new Date(value);
|
|
102
|
-
}
|
|
103
|
-
return value;
|
|
104
|
-
});
|
|
105
|
-
const inputProps = useStoreController(store, {
|
|
106
|
-
onSubscribe: (state) => {
|
|
107
|
-
const element = ref.current;
|
|
108
|
-
if (!element) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
112
|
-
const checked = toInputChecked(props.getter(state));
|
|
113
|
-
if (element.checked === checked) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
element.checked = checked;
|
|
117
|
-
} else {
|
|
118
|
-
const value = toInputValue(props.getter(state));
|
|
119
|
-
if (element.value === value) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
element.value = value;
|
|
123
|
-
}
|
|
124
|
-
const event = new Event("input", { bubbles: true });
|
|
125
|
-
element.dispatchEvent(event);
|
|
126
|
-
},
|
|
127
|
-
onDispatch: (state) => {
|
|
128
|
-
const element = ref.current;
|
|
129
|
-
if (!element) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
if ("checked" in element && props.type === "checkbox") {
|
|
133
|
-
props.setter(state, element.checked);
|
|
134
|
-
} else {
|
|
135
|
-
props.setter(state, toStateValue(element.value));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
return {
|
|
140
|
-
defaultValue: getDefaultValue(),
|
|
141
|
-
defaultChecked: getDefaultChecked(),
|
|
142
|
-
onChange: (event) => {
|
|
143
|
-
inputProps.dispatch();
|
|
144
|
-
props.onChange?.(event);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
149
|
-
0 && (module.exports = {
|
|
150
|
-
useStoreInput
|
|
151
|
-
});
|
package/dist/use_store_input.mjs
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
// src/use_store_input.tsx
|
|
2
|
-
import { format } from "date-fns";
|
|
3
|
-
|
|
4
|
-
// src/use_store_controller.tsx
|
|
5
|
-
import { useEffect, useId } from "react";
|
|
6
|
-
function useStoreController(store, props) {
|
|
7
|
-
const dispatchKey = useId();
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
const unsub = store.subscribe((state, key) => {
|
|
10
|
-
if (key === dispatchKey) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
props.onSubscribe(state);
|
|
14
|
-
});
|
|
15
|
-
return () => {
|
|
16
|
-
unsub();
|
|
17
|
-
};
|
|
18
|
-
}, []);
|
|
19
|
-
const dispatch = () => {
|
|
20
|
-
store.dispatch(
|
|
21
|
-
(state) => {
|
|
22
|
-
props.onDispatch(state);
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
key: dispatchKey
|
|
26
|
-
}
|
|
27
|
-
);
|
|
28
|
-
};
|
|
29
|
-
return {
|
|
30
|
-
dispatch
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// src/use_store_input.tsx
|
|
35
|
-
function useStoreInput(ref, store, props) {
|
|
36
|
-
const toInputValue = props.toInputValue || ((value) => {
|
|
37
|
-
if (value === void 0 || value === null) {
|
|
38
|
-
return "";
|
|
39
|
-
}
|
|
40
|
-
if (props.type === "datetime-local") {
|
|
41
|
-
const date = new Date(value);
|
|
42
|
-
if (!isNaN(date.getTime())) {
|
|
43
|
-
return format(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return String(value);
|
|
47
|
-
});
|
|
48
|
-
const getDefaultValue = () => {
|
|
49
|
-
if (props.defaultValue !== void 0) {
|
|
50
|
-
return props.defaultValue;
|
|
51
|
-
}
|
|
52
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
53
|
-
return void 0;
|
|
54
|
-
}
|
|
55
|
-
return toInputValue(props.getter(store.state));
|
|
56
|
-
};
|
|
57
|
-
const toInputChecked = (value) => {
|
|
58
|
-
if (props.type === "radio") {
|
|
59
|
-
return value !== void 0 && value === props.value;
|
|
60
|
-
}
|
|
61
|
-
return Boolean(value);
|
|
62
|
-
};
|
|
63
|
-
const getDefaultChecked = () => {
|
|
64
|
-
if (props.defaultChecked) {
|
|
65
|
-
return props.defaultChecked;
|
|
66
|
-
}
|
|
67
|
-
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
68
|
-
return void 0;
|
|
69
|
-
}
|
|
70
|
-
return toInputChecked(props.getter(store.state));
|
|
71
|
-
};
|
|
72
|
-
const toStateValue = props.toStateValue || ((value) => {
|
|
73
|
-
if (props.type === "number" || props.type === "range") {
|
|
74
|
-
return Number(value);
|
|
75
|
-
}
|
|
76
|
-
if (props.type === "datetime-local") {
|
|
77
|
-
return new Date(value);
|
|
78
|
-
}
|
|
79
|
-
return value;
|
|
80
|
-
});
|
|
81
|
-
const inputProps = useStoreController(store, {
|
|
82
|
-
onSubscribe: (state) => {
|
|
83
|
-
const element = ref.current;
|
|
84
|
-
if (!element) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
88
|
-
const checked = toInputChecked(props.getter(state));
|
|
89
|
-
if (element.checked === checked) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
element.checked = checked;
|
|
93
|
-
} else {
|
|
94
|
-
const value = toInputValue(props.getter(state));
|
|
95
|
-
if (element.value === value) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
element.value = value;
|
|
99
|
-
}
|
|
100
|
-
const event = new Event("input", { bubbles: true });
|
|
101
|
-
element.dispatchEvent(event);
|
|
102
|
-
},
|
|
103
|
-
onDispatch: (state) => {
|
|
104
|
-
const element = ref.current;
|
|
105
|
-
if (!element) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
if ("checked" in element && props.type === "checkbox") {
|
|
109
|
-
props.setter(state, element.checked);
|
|
110
|
-
} else {
|
|
111
|
-
props.setter(state, toStateValue(element.value));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
return {
|
|
116
|
-
defaultValue: getDefaultValue(),
|
|
117
|
-
defaultChecked: getDefaultChecked(),
|
|
118
|
-
onChange: (event) => {
|
|
119
|
-
inputProps.dispatch();
|
|
120
|
-
props.onChange?.(event);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
export {
|
|
125
|
-
useStoreInput
|
|
126
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { RefObject } from 'react';
|
|
3
|
-
import { Store } from 'gw-store';
|
|
4
|
-
import { StoreInputProps } from './use_store_input.mjs';
|
|
5
|
-
|
|
6
|
-
type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue> = undefined extends TName ? TValue : TName extends keyof TState ? TState[TName] : TValue;
|
|
7
|
-
type StoreInputWithNameProps<TInputElement, TState, TName extends keyof TState | undefined, TValue> = Omit<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter">> & {
|
|
8
|
-
name?: TName;
|
|
9
|
-
};
|
|
10
|
-
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
11
|
-
name: string | undefined;
|
|
12
|
-
defaultValue: string | number | readonly string[] | undefined;
|
|
13
|
-
defaultChecked: boolean | undefined;
|
|
14
|
-
onChange: (event: react.ChangeEvent<TInputElement, Element>) => void;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export { type InferNameFromProps, type StoreInputWithNameProps, useStoreInputWithName };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { RefObject } from 'react';
|
|
3
|
-
import { Store } from 'gw-store';
|
|
4
|
-
import { StoreInputProps } from './use_store_input.js';
|
|
5
|
-
|
|
6
|
-
type InferNameFromProps<TState, TName extends keyof TState | undefined, TValue> = undefined extends TName ? TValue : TName extends keyof TState ? TState[TName] : TValue;
|
|
7
|
-
type StoreInputWithNameProps<TInputElement, TState, TName extends keyof TState | undefined, TValue> = Omit<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState, InferNameFromProps<TState, TName, TValue>>, "getter" | "setter">> & {
|
|
8
|
-
name?: TName;
|
|
9
|
-
};
|
|
10
|
-
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState, TName extends keyof TState | undefined, TValue>(ref: RefObject<TInputElement | null>, store: Store<TState>, props: StoreInputWithNameProps<TInputElement, TState, TName, TValue>): {
|
|
11
|
-
name: string | undefined;
|
|
12
|
-
defaultValue: string | number | readonly string[] | undefined;
|
|
13
|
-
defaultChecked: boolean | undefined;
|
|
14
|
-
onChange: (event: react.ChangeEvent<TInputElement, Element>) => void;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export { type InferNameFromProps, type StoreInputWithNameProps, useStoreInputWithName };
|
|
@@ -1,177 +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_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_date_fns = require("date-fns");
|
|
29
|
-
|
|
30
|
-
// src/use_store_controller.tsx
|
|
31
|
-
var import_react = require("react");
|
|
32
|
-
function useStoreController(store, props) {
|
|
33
|
-
const dispatchKey = (0, import_react.useId)();
|
|
34
|
-
(0, import_react.useEffect)(() => {
|
|
35
|
-
const unsub = store.subscribe((state, key) => {
|
|
36
|
-
if (key === dispatchKey) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
props.onSubscribe(state);
|
|
40
|
-
});
|
|
41
|
-
return () => {
|
|
42
|
-
unsub();
|
|
43
|
-
};
|
|
44
|
-
}, []);
|
|
45
|
-
const dispatch = () => {
|
|
46
|
-
store.dispatch(
|
|
47
|
-
(state) => {
|
|
48
|
-
props.onDispatch(state);
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
key: dispatchKey
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
dispatch
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/use_store_input.tsx
|
|
61
|
-
function useStoreInput(ref, store, props) {
|
|
62
|
-
const toInputValue = props.toInputValue || ((value) => {
|
|
63
|
-
if (value === void 0 || value === null) {
|
|
64
|
-
return "";
|
|
65
|
-
}
|
|
66
|
-
if (props.type === "datetime-local") {
|
|
67
|
-
const date = new Date(value);
|
|
68
|
-
if (!isNaN(date.getTime())) {
|
|
69
|
-
return (0, import_date_fns.format)(date, "yyyy-MM-dd'T'HH:mm:ss");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return String(value);
|
|
73
|
-
});
|
|
74
|
-
const getDefaultValue = () => {
|
|
75
|
-
if (props.defaultValue !== void 0) {
|
|
76
|
-
return props.defaultValue;
|
|
77
|
-
}
|
|
78
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
79
|
-
return void 0;
|
|
80
|
-
}
|
|
81
|
-
return toInputValue(props.getter(store.state));
|
|
82
|
-
};
|
|
83
|
-
const toInputChecked = (value) => {
|
|
84
|
-
if (props.type === "radio") {
|
|
85
|
-
return value !== void 0 && value === props.value;
|
|
86
|
-
}
|
|
87
|
-
return Boolean(value);
|
|
88
|
-
};
|
|
89
|
-
const getDefaultChecked = () => {
|
|
90
|
-
if (props.defaultChecked) {
|
|
91
|
-
return props.defaultChecked;
|
|
92
|
-
}
|
|
93
|
-
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
94
|
-
return void 0;
|
|
95
|
-
}
|
|
96
|
-
return toInputChecked(props.getter(store.state));
|
|
97
|
-
};
|
|
98
|
-
const toStateValue = props.toStateValue || ((value) => {
|
|
99
|
-
if (props.type === "number" || props.type === "range") {
|
|
100
|
-
return Number(value);
|
|
101
|
-
}
|
|
102
|
-
if (props.type === "datetime-local") {
|
|
103
|
-
return new Date(value);
|
|
104
|
-
}
|
|
105
|
-
return value;
|
|
106
|
-
});
|
|
107
|
-
const inputProps = useStoreController(store, {
|
|
108
|
-
onSubscribe: (state) => {
|
|
109
|
-
const element = ref.current;
|
|
110
|
-
if (!element) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
114
|
-
const checked = toInputChecked(props.getter(state));
|
|
115
|
-
if (element.checked === checked) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
element.checked = checked;
|
|
119
|
-
} else {
|
|
120
|
-
const value = toInputValue(props.getter(state));
|
|
121
|
-
if (element.value === value) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
element.value = value;
|
|
125
|
-
}
|
|
126
|
-
const event = new Event("input", { bubbles: true });
|
|
127
|
-
element.dispatchEvent(event);
|
|
128
|
-
},
|
|
129
|
-
onDispatch: (state) => {
|
|
130
|
-
const element = ref.current;
|
|
131
|
-
if (!element) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if ("checked" in element && props.type === "checkbox") {
|
|
135
|
-
props.setter(state, element.checked);
|
|
136
|
-
} else {
|
|
137
|
-
props.setter(state, toStateValue(element.value));
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
return {
|
|
142
|
-
defaultValue: getDefaultValue(),
|
|
143
|
-
defaultChecked: getDefaultChecked(),
|
|
144
|
-
onChange: (event) => {
|
|
145
|
-
inputProps.dispatch();
|
|
146
|
-
props.onChange?.(event);
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/use_store_input_with_name.tsx
|
|
152
|
-
function useStoreInputWithName(ref, store, props) {
|
|
153
|
-
const inputProps = useStoreInput(ref, store, {
|
|
154
|
-
...props,
|
|
155
|
-
getter: (state) => {
|
|
156
|
-
if (props.getter) {
|
|
157
|
-
return props.getter(state);
|
|
158
|
-
}
|
|
159
|
-
return state[props.name];
|
|
160
|
-
},
|
|
161
|
-
setter: (state, value) => {
|
|
162
|
-
if (props.setter) {
|
|
163
|
-
props.setter(state, value);
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
state[props.name] = value;
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
return {
|
|
170
|
-
...inputProps,
|
|
171
|
-
name: "name" in props ? String(props.name) : void 0
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
175
|
-
0 && (module.exports = {
|
|
176
|
-
useStoreInputWithName
|
|
177
|
-
});
|