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
|
@@ -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.js';
|
|
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 };
|
package/dist/use_store_input.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,32 +15,74 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
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
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/use_store_input.tsx
|
|
31
21
|
var use_store_input_exports = {};
|
|
32
22
|
__export(use_store_input_exports, {
|
|
33
|
-
Input: () => Input,
|
|
34
|
-
Select: () => Select,
|
|
35
|
-
Textarea: () => Textarea,
|
|
36
23
|
useStoreInput: () => useStoreInput
|
|
37
24
|
});
|
|
38
25
|
module.exports = __toCommonJS(use_store_input_exports);
|
|
26
|
+
var import_react3 = require("react");
|
|
27
|
+
var import_date_fns = require("date-fns");
|
|
28
|
+
|
|
29
|
+
// src/use_store_controller.tsx
|
|
39
30
|
var import_react2 = require("react");
|
|
40
|
-
var import_react3 = __toESM(require("react"));
|
|
41
31
|
|
|
42
|
-
// src/
|
|
32
|
+
// src/use_subscribe.tsx
|
|
43
33
|
var import_react = require("react");
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
function useSubscribe(store, subscriber) {
|
|
35
|
+
(0, import_react.useEffect)(() => {
|
|
36
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
37
|
+
return () => {
|
|
38
|
+
unsubscribe();
|
|
39
|
+
};
|
|
40
|
+
}, [store]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/use_store_controller.tsx
|
|
44
|
+
function useStoreController(store, props) {
|
|
45
|
+
const ref = (0, import_react2.useRef)(null);
|
|
46
|
+
const dispatchKey = (0, import_react2.useId)();
|
|
47
|
+
(0, import_react2.useEffect)(() => {
|
|
48
|
+
const element = ref.current;
|
|
49
|
+
if (!element) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (typeof props.ref === "function") {
|
|
53
|
+
props.ref(element);
|
|
54
|
+
} else if (props.ref) {
|
|
55
|
+
props.ref.current = element;
|
|
56
|
+
}
|
|
57
|
+
}, []);
|
|
58
|
+
useSubscribe(store, (state, key) => {
|
|
59
|
+
if (key === dispatchKey) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!ref.current) return;
|
|
63
|
+
props.onSubscribe(state, ref.current);
|
|
64
|
+
});
|
|
65
|
+
const onChange = () => {
|
|
66
|
+
store.dispatch(
|
|
67
|
+
(state) => {
|
|
68
|
+
const element = ref.current;
|
|
69
|
+
if (!element) return;
|
|
70
|
+
props.onDispatch(state, element);
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
key: dispatchKey
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
dispatchKey,
|
|
79
|
+
ref,
|
|
80
|
+
onChange
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/use_store_input.tsx
|
|
85
|
+
function useStoreInput(store, props) {
|
|
46
86
|
const toInputValue = (value) => {
|
|
47
87
|
if (value === void 0 || value === null) {
|
|
48
88
|
return "";
|
|
@@ -64,7 +104,7 @@ function useStoreInputProps(store, props) {
|
|
|
64
104
|
if (props.type === "checkbox" || props.type === "radio") {
|
|
65
105
|
return void 0;
|
|
66
106
|
}
|
|
67
|
-
return toInputValue(store.state
|
|
107
|
+
return toInputValue(props.getter(store.state));
|
|
68
108
|
};
|
|
69
109
|
const toInputChecked = (value) => {
|
|
70
110
|
if (props.type === "radio") {
|
|
@@ -79,135 +119,57 @@ function useStoreInputProps(store, props) {
|
|
|
79
119
|
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
80
120
|
return void 0;
|
|
81
121
|
}
|
|
82
|
-
return toInputChecked(store.state
|
|
122
|
+
return toInputChecked(props.getter(store.state));
|
|
83
123
|
};
|
|
84
|
-
function useSubscriptionRef() {
|
|
85
|
-
const ref2 = (0, import_react.useRef)(null);
|
|
86
|
-
(0, import_react.useEffect)(() => {
|
|
87
|
-
const input = ref2.current;
|
|
88
|
-
if (!input) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (typeof props.ref === "function") {
|
|
92
|
-
props.ref(input);
|
|
93
|
-
} else if (props.ref) {
|
|
94
|
-
props.ref.current = input;
|
|
95
|
-
}
|
|
96
|
-
return store.subscribe((state, key) => {
|
|
97
|
-
if (key === dispatchKey) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
101
|
-
const checked = toInputChecked(state[props.name]);
|
|
102
|
-
if (input.checked === checked) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
input.checked = checked;
|
|
106
|
-
} else {
|
|
107
|
-
const value = toInputValue(state[props.name]);
|
|
108
|
-
if (input.value === value) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
input.value = value;
|
|
112
|
-
}
|
|
113
|
-
const event = new Event("input", { bubbles: true });
|
|
114
|
-
input.dispatchEvent(event);
|
|
115
|
-
});
|
|
116
|
-
}, []);
|
|
117
|
-
return ref2;
|
|
118
|
-
}
|
|
119
124
|
function toStateValue(value) {
|
|
120
|
-
|
|
125
|
+
const selected = props.getter(store.state);
|
|
126
|
+
if (typeof selected === "number") {
|
|
121
127
|
return Number(value);
|
|
122
128
|
}
|
|
123
|
-
if (
|
|
129
|
+
if (selected instanceof Date) {
|
|
124
130
|
return new Date(value);
|
|
125
131
|
}
|
|
126
132
|
return value;
|
|
127
133
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (props.type === "checkbox") {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
key: dispatchKey
|
|
138
|
-
}
|
|
139
|
-
);
|
|
134
|
+
const inputProps = useStoreController(store, {
|
|
135
|
+
ref: props.ref,
|
|
136
|
+
onSubscribe: (state, element) => {
|
|
137
|
+
if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
|
|
138
|
+
const checked = toInputChecked(props.getter(state));
|
|
139
|
+
if (element.checked === checked) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
element.checked = checked;
|
|
140
143
|
} else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
key: dispatchKey
|
|
147
|
-
}
|
|
148
|
-
);
|
|
144
|
+
const value = toInputValue(props.getter(state));
|
|
145
|
+
if (element.value === value) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
element.value = value;
|
|
149
149
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
ref,
|
|
162
|
-
name,
|
|
163
|
-
defaultValue,
|
|
164
|
-
defaultChecked,
|
|
165
|
-
onChange
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// src/use_store_input.tsx
|
|
170
|
-
function useStoreInput(store) {
|
|
171
|
-
const input = (0, import_react2.useCallback)(function Component(props) {
|
|
172
|
-
return /* @__PURE__ */ import_react3.default.createElement(Input, { store, ...props });
|
|
173
|
-
}, []);
|
|
174
|
-
const select = (0, import_react2.useCallback)(function Component(props) {
|
|
175
|
-
return /* @__PURE__ */ import_react3.default.createElement(Select, { store, ...props });
|
|
176
|
-
}, []);
|
|
177
|
-
const textarea = (0, import_react2.useCallback)(function Component(props) {
|
|
178
|
-
return /* @__PURE__ */ import_react3.default.createElement(Textarea, { store, ...props });
|
|
179
|
-
}, []);
|
|
150
|
+
const event = new Event("input", { bubbles: true });
|
|
151
|
+
element.dispatchEvent(event);
|
|
152
|
+
},
|
|
153
|
+
onDispatch: (state, element) => {
|
|
154
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
155
|
+
props.setter(state, element.checked);
|
|
156
|
+
} else {
|
|
157
|
+
props.setter(state, toStateValue(element.value));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
180
161
|
return {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
162
|
+
ref: inputProps.ref,
|
|
163
|
+
dispatchKey: inputProps.dispatchKey,
|
|
164
|
+
defaultValue: getDefaultValue(),
|
|
165
|
+
defaultChecked: getDefaultChecked(),
|
|
166
|
+
onChange: (event) => {
|
|
167
|
+
inputProps.onChange();
|
|
168
|
+
props.onChange?.(event);
|
|
169
|
+
}
|
|
184
170
|
};
|
|
185
171
|
}
|
|
186
|
-
function Input({
|
|
187
|
-
store,
|
|
188
|
-
...props
|
|
189
|
-
}) {
|
|
190
|
-
const storeProps = useStoreInputProps(store, props);
|
|
191
|
-
return /* @__PURE__ */ import_react3.default.createElement("input", { ...props, ...storeProps });
|
|
192
|
-
}
|
|
193
|
-
function Select({
|
|
194
|
-
store,
|
|
195
|
-
...props
|
|
196
|
-
}) {
|
|
197
|
-
const storeProps = useStoreInputProps(store, props);
|
|
198
|
-
return /* @__PURE__ */ import_react3.default.createElement("select", { ...props, ...storeProps });
|
|
199
|
-
}
|
|
200
|
-
function Textarea({
|
|
201
|
-
store,
|
|
202
|
-
...props
|
|
203
|
-
}) {
|
|
204
|
-
const storeProps = useStoreInputProps(store, props);
|
|
205
|
-
return /* @__PURE__ */ import_react3.default.createElement("textarea", { ...props, ...storeProps });
|
|
206
|
-
}
|
|
207
172
|
// Annotate the CommonJS export names for ESM import in node:
|
|
208
173
|
0 && (module.exports = {
|
|
209
|
-
Input,
|
|
210
|
-
Select,
|
|
211
|
-
Textarea,
|
|
212
174
|
useStoreInput
|
|
213
175
|
});
|
package/dist/use_store_input.mjs
CHANGED
|
@@ -1,17 +1,64 @@
|
|
|
1
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";
|
|
2
|
+
import "react";
|
|
13
3
|
import { format } from "date-fns";
|
|
14
|
-
|
|
4
|
+
|
|
5
|
+
// src/use_store_controller.tsx
|
|
6
|
+
import { useEffect as useEffect2, useId, useRef } from "react";
|
|
7
|
+
|
|
8
|
+
// src/use_subscribe.tsx
|
|
9
|
+
import { useEffect } from "react";
|
|
10
|
+
function useSubscribe(store, subscriber) {
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const unsubscribe = store.subscribe(subscriber);
|
|
13
|
+
return () => {
|
|
14
|
+
unsubscribe();
|
|
15
|
+
};
|
|
16
|
+
}, [store]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/use_store_controller.tsx
|
|
20
|
+
function useStoreController(store, props) {
|
|
21
|
+
const ref = useRef(null);
|
|
22
|
+
const dispatchKey = useId();
|
|
23
|
+
useEffect2(() => {
|
|
24
|
+
const element = ref.current;
|
|
25
|
+
if (!element) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (typeof props.ref === "function") {
|
|
29
|
+
props.ref(element);
|
|
30
|
+
} else if (props.ref) {
|
|
31
|
+
props.ref.current = element;
|
|
32
|
+
}
|
|
33
|
+
}, []);
|
|
34
|
+
useSubscribe(store, (state, key) => {
|
|
35
|
+
if (key === dispatchKey) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!ref.current) return;
|
|
39
|
+
props.onSubscribe(state, ref.current);
|
|
40
|
+
});
|
|
41
|
+
const onChange = () => {
|
|
42
|
+
store.dispatch(
|
|
43
|
+
(state) => {
|
|
44
|
+
const element = ref.current;
|
|
45
|
+
if (!element) return;
|
|
46
|
+
props.onDispatch(state, element);
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: dispatchKey
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
dispatchKey,
|
|
55
|
+
ref,
|
|
56
|
+
onChange
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/use_store_input.tsx
|
|
61
|
+
function useStoreInput(store, props) {
|
|
15
62
|
const toInputValue = (value) => {
|
|
16
63
|
if (value === void 0 || value === null) {
|
|
17
64
|
return "";
|
|
@@ -33,7 +80,7 @@ function useStoreInputProps(store, props) {
|
|
|
33
80
|
if (props.type === "checkbox" || props.type === "radio") {
|
|
34
81
|
return void 0;
|
|
35
82
|
}
|
|
36
|
-
return toInputValue(store.state
|
|
83
|
+
return toInputValue(props.getter(store.state));
|
|
37
84
|
};
|
|
38
85
|
const toInputChecked = (value) => {
|
|
39
86
|
if (props.type === "radio") {
|
|
@@ -48,134 +95,56 @@ function useStoreInputProps(store, props) {
|
|
|
48
95
|
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
49
96
|
return void 0;
|
|
50
97
|
}
|
|
51
|
-
return toInputChecked(store.state
|
|
98
|
+
return toInputChecked(props.getter(store.state));
|
|
52
99
|
};
|
|
53
|
-
function useSubscriptionRef() {
|
|
54
|
-
const ref2 = useRef(null);
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
const input = ref2.current;
|
|
57
|
-
if (!input) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
if (typeof props.ref === "function") {
|
|
61
|
-
props.ref(input);
|
|
62
|
-
} else if (props.ref) {
|
|
63
|
-
props.ref.current = input;
|
|
64
|
-
}
|
|
65
|
-
return store.subscribe((state, key) => {
|
|
66
|
-
if (key === dispatchKey) {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
70
|
-
const checked = toInputChecked(state[props.name]);
|
|
71
|
-
if (input.checked === checked) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
input.checked = checked;
|
|
75
|
-
} else {
|
|
76
|
-
const value = toInputValue(state[props.name]);
|
|
77
|
-
if (input.value === value) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
input.value = value;
|
|
81
|
-
}
|
|
82
|
-
const event = new Event("input", { bubbles: true });
|
|
83
|
-
input.dispatchEvent(event);
|
|
84
|
-
});
|
|
85
|
-
}, []);
|
|
86
|
-
return ref2;
|
|
87
|
-
}
|
|
88
100
|
function toStateValue(value) {
|
|
89
|
-
|
|
101
|
+
const selected = props.getter(store.state);
|
|
102
|
+
if (typeof selected === "number") {
|
|
90
103
|
return Number(value);
|
|
91
104
|
}
|
|
92
|
-
if (
|
|
105
|
+
if (selected instanceof Date) {
|
|
93
106
|
return new Date(value);
|
|
94
107
|
}
|
|
95
108
|
return value;
|
|
96
109
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (props.type === "checkbox") {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
key: dispatchKey
|
|
107
|
-
}
|
|
108
|
-
);
|
|
110
|
+
const inputProps = useStoreController(store, {
|
|
111
|
+
ref: props.ref,
|
|
112
|
+
onSubscribe: (state, element) => {
|
|
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;
|
|
109
119
|
} else {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
key: dispatchKey
|
|
116
|
-
}
|
|
117
|
-
);
|
|
120
|
+
const value = toInputValue(props.getter(state));
|
|
121
|
+
if (element.value === value) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
element.value = value;
|
|
118
125
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
ref,
|
|
131
|
-
name,
|
|
132
|
-
defaultValue,
|
|
133
|
-
defaultChecked,
|
|
134
|
-
onChange
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// src/use_store_input.tsx
|
|
139
|
-
function useStoreInput(store) {
|
|
140
|
-
const input = useCallback(function Component(props) {
|
|
141
|
-
return /* @__PURE__ */ React.createElement(Input, { store, ...props });
|
|
142
|
-
}, []);
|
|
143
|
-
const select = useCallback(function Component(props) {
|
|
144
|
-
return /* @__PURE__ */ React.createElement(Select, { store, ...props });
|
|
145
|
-
}, []);
|
|
146
|
-
const textarea = useCallback(function Component(props) {
|
|
147
|
-
return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
|
|
148
|
-
}, []);
|
|
126
|
+
const event = new Event("input", { bubbles: true });
|
|
127
|
+
element.dispatchEvent(event);
|
|
128
|
+
},
|
|
129
|
+
onDispatch: (state, element) => {
|
|
130
|
+
if ("checked" in element && props.type === "checkbox") {
|
|
131
|
+
props.setter(state, element.checked);
|
|
132
|
+
} else {
|
|
133
|
+
props.setter(state, toStateValue(element.value));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
149
137
|
return {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
138
|
+
ref: inputProps.ref,
|
|
139
|
+
dispatchKey: inputProps.dispatchKey,
|
|
140
|
+
defaultValue: getDefaultValue(),
|
|
141
|
+
defaultChecked: getDefaultChecked(),
|
|
142
|
+
onChange: (event) => {
|
|
143
|
+
inputProps.onChange();
|
|
144
|
+
props.onChange?.(event);
|
|
145
|
+
}
|
|
153
146
|
};
|
|
154
147
|
}
|
|
155
|
-
function Input({
|
|
156
|
-
store,
|
|
157
|
-
...props
|
|
158
|
-
}) {
|
|
159
|
-
const storeProps = useStoreInputProps(store, props);
|
|
160
|
-
return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
|
|
161
|
-
}
|
|
162
|
-
function Select({
|
|
163
|
-
store,
|
|
164
|
-
...props
|
|
165
|
-
}) {
|
|
166
|
-
const storeProps = useStoreInputProps(store, props);
|
|
167
|
-
return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
|
|
168
|
-
}
|
|
169
|
-
function Textarea({
|
|
170
|
-
store,
|
|
171
|
-
...props
|
|
172
|
-
}) {
|
|
173
|
-
const storeProps = useStoreInputProps(store, props);
|
|
174
|
-
return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
|
|
175
|
-
}
|
|
176
148
|
export {
|
|
177
|
-
Input,
|
|
178
|
-
Select,
|
|
179
|
-
Textarea,
|
|
180
149
|
useStoreInput
|
|
181
150
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Store } from './use_store.mjs';
|
|
3
|
+
import { StoreInputProps } from './use_store_input.mjs';
|
|
4
|
+
|
|
5
|
+
type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState>, "getter" | "setter">> & {
|
|
6
|
+
name?: keyof TState;
|
|
7
|
+
};
|
|
8
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
|
|
9
|
+
name: string | undefined;
|
|
10
|
+
ref: React.RefObject<TInputElement | null>;
|
|
11
|
+
dispatchKey: string;
|
|
12
|
+
defaultValue: string | number | readonly string[] | undefined;
|
|
13
|
+
defaultChecked: boolean | undefined;
|
|
14
|
+
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useStoreInputWithName };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Store } from './use_store.js';
|
|
3
|
+
import { StoreInputProps } from './use_store_input.js';
|
|
4
|
+
|
|
5
|
+
type Props<TInputElement, TState> = Omit<StoreInputProps<TInputElement, TState>, "getter" | "setter"> & Partial<Pick<StoreInputProps<TInputElement, TState>, "getter" | "setter">> & {
|
|
6
|
+
name?: keyof TState;
|
|
7
|
+
};
|
|
8
|
+
declare function useStoreInputWithName<TInputElement extends HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement, TState>(store: Store<TState>, props: Props<TInputElement, TState>): {
|
|
9
|
+
name: string | undefined;
|
|
10
|
+
ref: React.RefObject<TInputElement | null>;
|
|
11
|
+
dispatchKey: string;
|
|
12
|
+
defaultValue: string | number | readonly string[] | undefined;
|
|
13
|
+
defaultChecked: boolean | undefined;
|
|
14
|
+
onChange: (event: React.ChangeEvent<TInputElement>) => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useStoreInputWithName };
|