react-store-input 0.1.1 → 0.1.4

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.
@@ -0,0 +1,226 @@
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
+ ref,
61
+ onChange
62
+ };
63
+ }
64
+
65
+ // src/use_store_input.tsx
66
+ function useStoreInput(store, props) {
67
+ const toInputValue = (value) => {
68
+ if (value === void 0 || value === null) {
69
+ return "";
70
+ }
71
+ if (props.type === "datetime-local") {
72
+ if (value instanceof Date) {
73
+ return format(value, "yyyy-MM-dd'T'HH:mm:ss");
74
+ }
75
+ if (typeof value === "string") {
76
+ return toInputValue(new Date(value));
77
+ }
78
+ }
79
+ return String(value);
80
+ };
81
+ const getDefaultValue = () => {
82
+ if (props.defaultValue !== void 0) {
83
+ return props.defaultValue;
84
+ }
85
+ if (props.type === "checkbox" || props.type === "radio") {
86
+ return void 0;
87
+ }
88
+ return toInputValue(props.getter(store.state));
89
+ };
90
+ const toInputChecked = (value) => {
91
+ if (props.type === "radio") {
92
+ return value !== void 0 && value === props.value;
93
+ }
94
+ return Boolean(value);
95
+ };
96
+ const getDefaultChecked = () => {
97
+ if (props.defaultChecked) {
98
+ return props.defaultChecked;
99
+ }
100
+ if (props.type !== "checkbox" && props.type !== "radio") {
101
+ return void 0;
102
+ }
103
+ return toInputChecked(props.getter(store.state));
104
+ };
105
+ function toStateValue(value) {
106
+ const selected = props.getter(store.state);
107
+ if (typeof selected === "number") {
108
+ return Number(value);
109
+ }
110
+ if (selected instanceof Date) {
111
+ return new Date(value);
112
+ }
113
+ return value;
114
+ }
115
+ const inputProps = useStoreController(store, {
116
+ ref: props.ref,
117
+ onSubscribe: (state, element) => {
118
+ if ("checked" in element && (props.type === "checkbox" || props.type === "radio")) {
119
+ const checked = toInputChecked(props.getter(state));
120
+ if (element.checked === checked) {
121
+ return;
122
+ }
123
+ element.checked = checked;
124
+ } else {
125
+ const value = toInputValue(props.getter(state));
126
+ if (element.value === value) {
127
+ return;
128
+ }
129
+ element.value = value;
130
+ }
131
+ const event = new Event("input", { bubbles: true });
132
+ element.dispatchEvent(event);
133
+ },
134
+ onDispatch: (state, element) => {
135
+ if ("checked" in element && props.type === "checkbox") {
136
+ props.setter(state, element.checked);
137
+ } else {
138
+ props.setter(state, toStateValue(element.value));
139
+ }
140
+ }
141
+ });
142
+ return {
143
+ ref: inputProps.ref,
144
+ defaultValue: getDefaultValue(),
145
+ defaultChecked: getDefaultChecked(),
146
+ onChange: (event) => {
147
+ inputProps.onChange();
148
+ props.onChange?.(event);
149
+ }
150
+ };
151
+ }
152
+
153
+ // src/use_store_input_with_name.tsx
154
+ function useStoreInputWithName(store, props) {
155
+ const inputProps = useStoreInput(store, {
156
+ ...props,
157
+ getter: (state) => {
158
+ if (props.getter) {
159
+ return props.getter(state);
160
+ }
161
+ return state[props.name];
162
+ },
163
+ setter: (state, value) => {
164
+ if (props.setter) {
165
+ props.setter(state, value);
166
+ return;
167
+ }
168
+ state[props.name] = value;
169
+ }
170
+ });
171
+ return {
172
+ ...inputProps,
173
+ name: "name" in props ? String(props.name) : void 0
174
+ };
175
+ }
176
+
177
+ // src/use_store_component.tsx
178
+ function useStoreComponent(store) {
179
+ const input = useCallback(function Component(props) {
180
+ return /* @__PURE__ */ React.createElement(Input, { store, ...props });
181
+ }, []);
182
+ const select = useCallback(function Component(props) {
183
+ return /* @__PURE__ */ React.createElement(Select, { store, ...props });
184
+ }, []);
185
+ const textarea = useCallback(function Component(props) {
186
+ return /* @__PURE__ */ React.createElement(Textarea, { store, ...props });
187
+ }, []);
188
+ return {
189
+ input,
190
+ select,
191
+ textarea
192
+ };
193
+ }
194
+ function Input({
195
+ store,
196
+ getter,
197
+ setter,
198
+ ...props
199
+ }) {
200
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
201
+ return /* @__PURE__ */ React.createElement("input", { ...props, ...storeProps });
202
+ }
203
+ function Select({
204
+ store,
205
+ getter,
206
+ setter,
207
+ ...props
208
+ }) {
209
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
210
+ return /* @__PURE__ */ React.createElement("select", { ...props, ...storeProps });
211
+ }
212
+ function Textarea({
213
+ store,
214
+ getter,
215
+ setter,
216
+ ...props
217
+ }) {
218
+ const storeProps = useStoreInputWithName(store, { ...props, getter, setter });
219
+ return /* @__PURE__ */ React.createElement("textarea", { ...props, ...storeProps });
220
+ }
221
+ export {
222
+ Input,
223
+ Select,
224
+ Textarea,
225
+ useStoreComponent
226
+ };
@@ -0,0 +1,15 @@
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
+ ref: React.RefObject<TController | null>;
12
+ onChange: () => void;
13
+ };
14
+
15
+ export { type StoreControllerProps, useStoreController };
@@ -0,0 +1,15 @@
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
+ ref: React.RefObject<TController | null>;
12
+ onChange: () => void;
13
+ };
14
+
15
+ export { type StoreControllerProps, useStoreController };
@@ -0,0 +1,81 @@
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
+ ref,
75
+ onChange
76
+ };
77
+ }
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ useStoreController
81
+ });
@@ -0,0 +1,56 @@
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
+ ref,
51
+ onChange
52
+ };
53
+ }
54
+ export {
55
+ useStoreController
56
+ };
@@ -1,19 +1,23 @@
1
- import React__default, { DetailedHTMLProps, InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes } from 'react';
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<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = Omit<TElement extends HTMLInputElement ? DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> : TElement extends HTMLSelectElement ? DetailedHTMLProps<SelectHTMLAttributes<HTMLSelectElement>, HTMLSelectElement> : DetailedHTMLProps<TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "name"> & {
5
- name: keyof TState;
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
- input: React__default.FC<StoreInputProps<HTMLInputElement, TState>>;
9
- select: React__default.FC<StoreInputProps<HTMLSelectElement, TState>>;
10
- textarea: React__default.FC<StoreInputProps<HTMLTextAreaElement, TState>>;
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
+ defaultValue: string | number | readonly string[] | undefined;
19
+ defaultChecked: boolean | undefined;
20
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
11
21
  };
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
22
 
19
- export { Input, Select, type StoreInputPropsWithStore, Textarea, useStoreInput };
23
+ export { type StoreInputProps, useStoreInput };
@@ -1,19 +1,23 @@
1
- import React__default, { DetailedHTMLProps, InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes } from 'react';
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<TElement extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, TState> = Omit<TElement extends HTMLInputElement ? DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> : TElement extends HTMLSelectElement ? DetailedHTMLProps<SelectHTMLAttributes<HTMLSelectElement>, HTMLSelectElement> : DetailedHTMLProps<TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "name"> & {
5
- name: keyof TState;
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
- input: React__default.FC<StoreInputProps<HTMLInputElement, TState>>;
9
- select: React__default.FC<StoreInputProps<HTMLSelectElement, TState>>;
10
- textarea: React__default.FC<StoreInputProps<HTMLTextAreaElement, TState>>;
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
+ defaultValue: string | number | readonly string[] | undefined;
19
+ defaultChecked: boolean | undefined;
20
+ onChange: (event: React.ChangeEvent<TInputElement>) => void;
11
21
  };
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
22
 
19
- export { Input, Select, type StoreInputPropsWithStore, Textarea, useStoreInput };
23
+ export { type StoreInputProps, useStoreInput };