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,133 +0,0 @@
|
|
|
1
|
-
// src/use_store_input_props.tsx
|
|
2
|
-
import {
|
|
3
|
-
useEffect,
|
|
4
|
-
useId,
|
|
5
|
-
useRef
|
|
6
|
-
} from "react";
|
|
7
|
-
import { format } from "date-fns";
|
|
8
|
-
function useStoreInputProps(store, props) {
|
|
9
|
-
const toInputValue = (value) => {
|
|
10
|
-
if (value === void 0 || value === null) {
|
|
11
|
-
return "";
|
|
12
|
-
}
|
|
13
|
-
if (props.type === "datetime-local") {
|
|
14
|
-
if (value instanceof Date) {
|
|
15
|
-
return format(value, "yyyy-MM-dd'T'HH:mm:ss");
|
|
16
|
-
}
|
|
17
|
-
if (typeof value === "string") {
|
|
18
|
-
return toInputValue(new Date(value));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return String(value);
|
|
22
|
-
};
|
|
23
|
-
const getDefaultValue = () => {
|
|
24
|
-
if (props.defaultValue !== void 0) {
|
|
25
|
-
return props.defaultValue;
|
|
26
|
-
}
|
|
27
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
28
|
-
return void 0;
|
|
29
|
-
}
|
|
30
|
-
return toInputValue(store.state[props.name]);
|
|
31
|
-
};
|
|
32
|
-
const toInputChecked = (value) => {
|
|
33
|
-
if (props.type === "radio") {
|
|
34
|
-
return value !== void 0 && value === props.value;
|
|
35
|
-
}
|
|
36
|
-
return Boolean(value);
|
|
37
|
-
};
|
|
38
|
-
const getDefaultChecked = () => {
|
|
39
|
-
if (props.defaultChecked) {
|
|
40
|
-
return props.defaultChecked;
|
|
41
|
-
}
|
|
42
|
-
if (props.type !== "checkbox" && props.type !== "radio") {
|
|
43
|
-
return void 0;
|
|
44
|
-
}
|
|
45
|
-
return toInputChecked(store.state[props.name]);
|
|
46
|
-
};
|
|
47
|
-
function useSubscriptionRef() {
|
|
48
|
-
const ref2 = useRef(null);
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
const input = ref2.current;
|
|
51
|
-
if (!input) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (typeof props.ref === "function") {
|
|
55
|
-
props.ref(input);
|
|
56
|
-
} else if (props.ref) {
|
|
57
|
-
props.ref.current = input;
|
|
58
|
-
}
|
|
59
|
-
return store.subscribe((state, key) => {
|
|
60
|
-
if (key === dispatchKey) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
if (props.type === "checkbox" || props.type === "radio") {
|
|
64
|
-
const checked = toInputChecked(state[props.name]);
|
|
65
|
-
if (input.checked === checked) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
input.checked = checked;
|
|
69
|
-
} else {
|
|
70
|
-
const value = toInputValue(state[props.name]);
|
|
71
|
-
if (input.value === value) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
input.value = value;
|
|
75
|
-
}
|
|
76
|
-
const event = new Event("input", { bubbles: true });
|
|
77
|
-
input.dispatchEvent(event);
|
|
78
|
-
});
|
|
79
|
-
}, []);
|
|
80
|
-
return ref2;
|
|
81
|
-
}
|
|
82
|
-
function toStateValue(value) {
|
|
83
|
-
if (typeof store.state[props.name] === "number") {
|
|
84
|
-
return Number(value);
|
|
85
|
-
}
|
|
86
|
-
if (store.state[props.name] instanceof Date) {
|
|
87
|
-
return new Date(value);
|
|
88
|
-
}
|
|
89
|
-
return value;
|
|
90
|
-
}
|
|
91
|
-
function createChangeEventHandler() {
|
|
92
|
-
return (e) => {
|
|
93
|
-
const target = e.target;
|
|
94
|
-
if (props.type === "checkbox") {
|
|
95
|
-
store.dispatch(
|
|
96
|
-
(state) => {
|
|
97
|
-
state[props.name] = target.checked;
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
key: dispatchKey
|
|
101
|
-
}
|
|
102
|
-
);
|
|
103
|
-
} else {
|
|
104
|
-
store.dispatch(
|
|
105
|
-
(state) => {
|
|
106
|
-
state[props.name] = toStateValue(target.value);
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
key: dispatchKey
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
props.onChange?.(e);
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
const dispatchKey = useId();
|
|
117
|
-
const ref = useSubscriptionRef();
|
|
118
|
-
const name = String(props.name);
|
|
119
|
-
const defaultValue = getDefaultValue();
|
|
120
|
-
const defaultChecked = getDefaultChecked();
|
|
121
|
-
const onChange = createChangeEventHandler();
|
|
122
|
-
return {
|
|
123
|
-
key: dispatchKey,
|
|
124
|
-
ref,
|
|
125
|
-
name,
|
|
126
|
-
defaultValue,
|
|
127
|
-
defaultChecked,
|
|
128
|
-
onChange
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
export {
|
|
132
|
-
useStoreInputProps
|
|
133
|
-
};
|