@rnaga/wp-next-ui 1.0.1
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/Accordion.d.ts +4 -0
- package/Accordion.d.ts.map +1 -0
- package/Accordion.js +13 -0
- package/Accordions.d.ts +19 -0
- package/Accordions.d.ts.map +1 -0
- package/Accordions.js +39 -0
- package/Background.d.ts +5 -0
- package/Background.d.ts.map +1 -0
- package/Background.js +18 -0
- package/BadgeOnMouseOver.d.ts +12 -0
- package/BadgeOnMouseOver.d.ts.map +1 -0
- package/BadgeOnMouseOver.js +40 -0
- package/BasicMenuButton.d.ts +17 -0
- package/BasicMenuButton.d.ts.map +1 -0
- package/BasicMenuButton.js +61 -0
- package/Button.d.ts +10 -0
- package/Button.d.ts.map +1 -0
- package/Button.js +15 -0
- package/Card.d.ts +6 -0
- package/Card.d.ts.map +1 -0
- package/Card.js +13 -0
- package/CardImage.d.ts +10 -0
- package/CardImage.d.ts.map +1 -0
- package/CardImage.js +34 -0
- package/Checkbox.d.ts +6 -0
- package/Checkbox.d.ts.map +1 -0
- package/Checkbox.js +13 -0
- package/Chip.d.ts +10 -0
- package/Chip.d.ts.map +1 -0
- package/Chip.js +41 -0
- package/DateTimePicker.d.ts +6 -0
- package/DateTimePicker.d.ts.map +1 -0
- package/DateTimePicker.js +45 -0
- package/DraggableBox.d.ts +39 -0
- package/DraggableBox.d.ts.map +1 -0
- package/DraggableBox.js +171 -0
- package/Form.d.ts +4 -0
- package/Form.d.ts.map +1 -0
- package/Form.js +8 -0
- package/FormDataProvider.d.ts +5 -0
- package/FormDataProvider.d.ts.map +1 -0
- package/FormDataProvider.js +6 -0
- package/IconButtonDelete.d.ts +6 -0
- package/IconButtonDelete.d.ts.map +1 -0
- package/IconButtonDelete.js +14 -0
- package/Input.d.ts +14 -0
- package/Input.d.ts.map +1 -0
- package/Input.js +84 -0
- package/InputClickField.d.ts +13 -0
- package/InputClickField.d.ts.map +1 -0
- package/InputClickField.js +21 -0
- package/InputColor.d.ts +12 -0
- package/InputColor.d.ts.map +1 -0
- package/InputColor.js +141 -0
- package/InputMultiple.d.ts +15 -0
- package/InputMultiple.d.ts.map +1 -0
- package/InputMultiple.js +55 -0
- package/InputSearch.d.ts +7 -0
- package/InputSearch.d.ts.map +1 -0
- package/InputSearch.js +28 -0
- package/Link.d.ts +6 -0
- package/Link.d.ts.map +1 -0
- package/Link.js +15 -0
- package/LinkCopy.d.ts +6 -0
- package/LinkCopy.d.ts.map +1 -0
- package/LinkCopy.js +17 -0
- package/Loading.d.ts +7 -0
- package/Loading.d.ts.map +1 -0
- package/Loading.js +29 -0
- package/LoadingBox.d.ts +12 -0
- package/LoadingBox.d.ts.map +1 -0
- package/LoadingBox.js +30 -0
- package/MediaTag.d.ts +9 -0
- package/MediaTag.d.ts.map +1 -0
- package/MediaTag.js +19 -0
- package/Modal.d.ts +11 -0
- package/Modal.d.ts.map +1 -0
- package/Modal.js +65 -0
- package/ModalConfirm.d.ts +8 -0
- package/ModalConfirm.d.ts.map +1 -0
- package/ModalConfirm.js +58 -0
- package/PopperMenu.d.ts +7 -0
- package/PopperMenu.d.ts.map +1 -0
- package/PopperMenu.js +6 -0
- package/README.md +292 -0
- package/Select.d.ts +21 -0
- package/Select.d.ts.map +1 -0
- package/Select.js +23 -0
- package/SelectAutocomplete.d.ts +1208 -0
- package/SelectAutocomplete.d.ts.map +1 -0
- package/SelectAutocomplete.js +113 -0
- package/SelectMultiple.d.ts +16 -0
- package/SelectMultiple.d.ts.map +1 -0
- package/SelectMultiple.js +28 -0
- package/SelectWPPost.d.ts +17 -0
- package/SelectWPPost.d.ts.map +1 -0
- package/SelectWPPost.js +98 -0
- package/SelectWPTaxonomy.d.ts +9 -0
- package/SelectWPTaxonomy.d.ts.map +1 -0
- package/SelectWPTaxonomy.js +35 -0
- package/SelectWPTerm.d.ts +13 -0
- package/SelectWPTerm.d.ts.map +1 -0
- package/SelectWPTerm.js +92 -0
- package/SelectWPTerms.d.ts +27 -0
- package/SelectWPTerms.d.ts.map +1 -0
- package/SelectWPTerms.js +105 -0
- package/SelectWPUser.d.ts +16 -0
- package/SelectWPUser.d.ts.map +1 -0
- package/SelectWPUser.js +101 -0
- package/SortableList.d.ts +28 -0
- package/SortableList.d.ts.map +1 -0
- package/SortableList.js +195 -0
- package/Tabs.d.ts +17 -0
- package/Tabs.d.ts.map +1 -0
- package/Tabs.js +35 -0
- package/ThemeRegistry.d.ts +13 -0
- package/ThemeRegistry.d.ts.map +1 -0
- package/ThemeRegistry.js +36 -0
- package/Typography.d.ts +9 -0
- package/Typography.d.ts.map +1 -0
- package/Typography.js +12 -0
- package/Viewport.d.ts +6 -0
- package/Viewport.d.ts.map +1 -0
- package/Viewport.js +10 -0
- package/hooks/use-form-data.d.ts +39 -0
- package/hooks/use-form-data.d.ts.map +1 -0
- package/hooks/use-form-data.js +91 -0
- package/hooks/use-media-selector.d.ts +10 -0
- package/hooks/use-media-selector.d.ts.map +1 -0
- package/hooks/use-media-selector.js +33 -0
- package/hooks/use-mouse-move.d.ts +18 -0
- package/hooks/use-mouse-move.d.ts.map +1 -0
- package/hooks/use-mouse-move.js +112 -0
- package/hooks/use-scheme-toggle.d.ts +6 -0
- package/hooks/use-scheme-toggle.d.ts.map +1 -0
- package/hooks/use-scheme-toggle.js +16 -0
- package/hooks/use-viewport.d.ts +5 -0
- package/hooks/use-viewport.d.ts.map +1 -0
- package/hooks/use-viewport.js +9 -0
- package/list/ListGrid.d.ts +12 -0
- package/list/ListGrid.d.ts.map +1 -0
- package/list/ListGrid.js +15 -0
- package/list/ListTable.d.ts +43 -0
- package/list/ListTable.d.ts.map +1 -0
- package/list/ListTable.js +143 -0
- package/list/Pagination.d.ts +5 -0
- package/list/Pagination.d.ts.map +1 -0
- package/list/Pagination.js +24 -0
- package/list/index.d.ts +4 -0
- package/list/index.d.ts.map +1 -0
- package/list/index.js +3 -0
- package/media/MediaGridForm.d.ts +5 -0
- package/media/MediaGridForm.d.ts.map +1 -0
- package/media/MediaGridForm.js +128 -0
- package/media/MediaThumbnail.d.ts +4 -0
- package/media/MediaThumbnail.d.ts.map +1 -0
- package/media/MediaThumbnail.js +17 -0
- package/media/MediaUpload.d.ts +7 -0
- package/media/MediaUpload.d.ts.map +1 -0
- package/media/MediaUpload.js +76 -0
- package/media/index.d.ts +4 -0
- package/media/index.d.ts.map +1 -0
- package/media/index.js +3 -0
- package/media-selector/MediaSelectorList.d.ts +2 -0
- package/media-selector/MediaSelectorList.d.ts.map +1 -0
- package/media-selector/MediaSelectorList.js +43 -0
- package/media-selector/MediaSelectorPreview.d.ts +2 -0
- package/media-selector/MediaSelectorPreview.d.ts.map +1 -0
- package/media-selector/MediaSelectorPreview.js +80 -0
- package/media-selector/index.d.ts +2 -0
- package/media-selector/index.d.ts.map +1 -0
- package/media-selector/index.js +49 -0
- package/package.json +33 -0
- package/portal/Portal.d.ts +6 -0
- package/portal/Portal.d.ts.map +1 -0
- package/portal/Portal.js +7 -0
- package/portal/index.d.ts +3 -0
- package/portal/index.d.ts.map +1 -0
- package/portal/index.js +2 -0
- package/portal/use-portal.d.ts +4 -0
- package/portal/use-portal.d.ts.map +1 -0
- package/portal/use-portal.js +11 -0
- package/types/global-state.d.ts +27 -0
- package/types/hooks/filters.d.ts +20 -0
- package/types/hooks/index.d.ts +1 -0
- package/types/index.d.ts +3 -0
- package/types/wp-theme.d.ts +49 -0
- package/wp-theme/index.d.ts +3 -0
- package/wp-theme/index.d.ts.map +1 -0
- package/wp-theme/index.js +86 -0
package/SelectWPUser.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { Autocomplete } from "@mui/material";
|
|
4
|
+
import { useServerActions } from "@rnaga/wp-next-core/client/hooks/use-server-actions";
|
|
5
|
+
import { Typography } from "./Typography";
|
|
6
|
+
import { SelectAutocomplete } from "./SelectAutocomplete";
|
|
7
|
+
const sxSelectAutocomplete = SelectAutocomplete.sx;
|
|
8
|
+
const SelectAutocompleteTextField = SelectAutocomplete.TextField;
|
|
9
|
+
const RenderOption = SelectAutocomplete.RenderOption;
|
|
10
|
+
const getArgs = (args) => {
|
|
11
|
+
args = {
|
|
12
|
+
...args,
|
|
13
|
+
exclude_anonymous: true,
|
|
14
|
+
per_page: 20,
|
|
15
|
+
};
|
|
16
|
+
return [args, { context: "view" }];
|
|
17
|
+
};
|
|
18
|
+
export const SelectWPUser = (props) => {
|
|
19
|
+
const { onChange, defaultValue, blogId, userOptions, userArgs, slotSxProps, size, } = props;
|
|
20
|
+
const { actions, parse, safeParse } = useServerActions();
|
|
21
|
+
const [users, setUsers] = useState();
|
|
22
|
+
const [currentUser, setCurrentUser] = useState();
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
(async () => {
|
|
25
|
+
let currentUser;
|
|
26
|
+
if (defaultValue) {
|
|
27
|
+
[currentUser] = ((await actions.user
|
|
28
|
+
.list(...getArgs({
|
|
29
|
+
blog_id: blogId,
|
|
30
|
+
include: [defaultValue],
|
|
31
|
+
per_page: 1,
|
|
32
|
+
}))
|
|
33
|
+
.then(parse)) ?? [])[0];
|
|
34
|
+
setCurrentUser(currentUser);
|
|
35
|
+
}
|
|
36
|
+
const [users] = await actions.user
|
|
37
|
+
.list(...getArgs({
|
|
38
|
+
blog_id: blogId,
|
|
39
|
+
exclude: defaultValue ? [defaultValue] : undefined,
|
|
40
|
+
}))
|
|
41
|
+
.then(parse);
|
|
42
|
+
setUsers(currentUser ? [...users, currentUser] : users);
|
|
43
|
+
})();
|
|
44
|
+
}, []);
|
|
45
|
+
const handleSearch = async (value) => {
|
|
46
|
+
const response = await actions.user
|
|
47
|
+
.list(...getArgs({
|
|
48
|
+
blog_id: blogId,
|
|
49
|
+
search: value,
|
|
50
|
+
}))
|
|
51
|
+
.then(safeParse);
|
|
52
|
+
if (!response.success) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
setUsers(response.data);
|
|
56
|
+
};
|
|
57
|
+
const arePostsEqual = (a, b) => {
|
|
58
|
+
return a.ID === b.ID;
|
|
59
|
+
};
|
|
60
|
+
const getUserKey = (user) => `${Math.random()}-${user.ID}`;
|
|
61
|
+
if (!users || (defaultValue && !currentUser)) {
|
|
62
|
+
return _jsx(Typography, { children: "Loading.." });
|
|
63
|
+
}
|
|
64
|
+
let value = currentUser;
|
|
65
|
+
return (_jsx(SelectAutocomplete.Wrapper, { size: size, slotSxProps: slotSxProps, children: _jsx(Autocomplete, { size: "small",
|
|
66
|
+
//freeSolo={freeSolo}
|
|
67
|
+
disableClearable: true, value: value, onChange: (e, v) => {
|
|
68
|
+
const user = v;
|
|
69
|
+
console.log("onChange", v);
|
|
70
|
+
setCurrentUser(user);
|
|
71
|
+
onChange(user);
|
|
72
|
+
}, onInputChange: (event, value, reason) => {
|
|
73
|
+
if (typeof value === "string") {
|
|
74
|
+
const newValue = {
|
|
75
|
+
ID: 0,
|
|
76
|
+
user_nicename: value,
|
|
77
|
+
};
|
|
78
|
+
onChange(newValue);
|
|
79
|
+
setCurrentUser(newValue);
|
|
80
|
+
}
|
|
81
|
+
if (reason === "input") {
|
|
82
|
+
handleSearch(value);
|
|
83
|
+
}
|
|
84
|
+
}, getOptionKey: getUserKey, getOptionLabel: (option) => {
|
|
85
|
+
if (typeof option === "string") {
|
|
86
|
+
return option;
|
|
87
|
+
}
|
|
88
|
+
return typeof option !== "string" ? option.user_nicename : "";
|
|
89
|
+
},
|
|
90
|
+
// getOptionLabel={(option) => option.post_title}
|
|
91
|
+
isOptionEqualToValue: arePostsEqual, options: [...users], renderInput: (params) => (_jsx(SelectAutocompleteTextField, { params: params, size: size, sx: slotSxProps?.textField })), sx: sxSelectAutocomplete({
|
|
92
|
+
size: size ?? "small",
|
|
93
|
+
sx: slotSxProps?.input,
|
|
94
|
+
}), renderOption: (props, option, state) => {
|
|
95
|
+
const postId = option.ID;
|
|
96
|
+
return (_jsx(RenderOption, { props: props, option: {
|
|
97
|
+
label: option.user_nicename,
|
|
98
|
+
id: `${postId}`,
|
|
99
|
+
}, state: state }, postId));
|
|
100
|
+
} }, `user-${defaultValue ?? 0}`) }));
|
|
101
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { SxProps } from "@mui/material";
|
|
2
|
+
import { JSX, RefObject } from "react";
|
|
3
|
+
export type SortableListItemType<T = string> = {
|
|
4
|
+
index: number;
|
|
5
|
+
value: T;
|
|
6
|
+
label: string;
|
|
7
|
+
ref: RefObject<HTMLElement | null>;
|
|
8
|
+
};
|
|
9
|
+
type DisplayType = "vertical" | "horizontal" | "grid" | "horizontal-fit";
|
|
10
|
+
export type SortableListProps = Parameters<typeof SortableList>[0];
|
|
11
|
+
export declare const SortableList: <T extends any = string>(props: {
|
|
12
|
+
enum: {
|
|
13
|
+
value: T;
|
|
14
|
+
label: string;
|
|
15
|
+
}[];
|
|
16
|
+
size?: "small" | "medium";
|
|
17
|
+
sx?: SxProps;
|
|
18
|
+
slotSxProps?: {
|
|
19
|
+
label?: SxProps;
|
|
20
|
+
};
|
|
21
|
+
displayType: DisplayType;
|
|
22
|
+
renderItem?: (item: SortableListItemType<T>) => JSX.Element;
|
|
23
|
+
onDelete?: (index: number) => void;
|
|
24
|
+
onEdit?: (index: number) => void;
|
|
25
|
+
onChange?: (items: SortableListItemType<T>[]) => void;
|
|
26
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=SortableList.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SortableList.d.ts","sourceRoot":"","sources":["../src/SortableList.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAmC,OAAO,EAAE,MAAM,eAAe,CAAC;AAGzE,OAAO,EAEL,GAAG,EACH,SAAS,EAKV,MAAM,OAAO,CAAC;AAQf,MAAM,MAAM,oBAAoB,CAAC,CAAC,GAAG,MAAM,IAAI;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;CACpC,CAAC;AAEF,KAAK,WAAW,GAAG,UAAU,GAAG,YAAY,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAgOzE,MAAM,MAAM,iBAAiB,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AAEnE,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,GAAG,GAAG,MAAM,EAAE,OAAO;IAC1D,IAAI,EAAE;QAAE,KAAK,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IACpC,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC1B,EAAE,CAAC,EAAE,OAAO,CAAC;IACb,WAAW,CAAC,EAAE;QACZ,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,CAAC;IACF,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,oBAAoB,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAC5D,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC;CACvD,4CAmGA,CAAC"}
|
package/SortableList.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Box, IconButton, List, ListItem } from "@mui/material";
|
|
3
|
+
import { Typography } from "./Typography";
|
|
4
|
+
import { useMouseMove } from "./hooks/use-mouse-move";
|
|
5
|
+
import { createContext, useContext, useEffect, useRef, useState, } from "react";
|
|
6
|
+
import { Close } from "@mui/icons-material";
|
|
7
|
+
import EditIcon from "@mui/icons-material/Edit";
|
|
8
|
+
import { useWPTheme } from "./ThemeRegistry";
|
|
9
|
+
const Context = createContext({});
|
|
10
|
+
const SortableListItem = (props) => {
|
|
11
|
+
const { index, size } = props;
|
|
12
|
+
const ref = useRef(null);
|
|
13
|
+
const { findItem, swapItems, refPos, targetRef, items, displayType, renderItem, onDelete, onEdit, } = useContext(Context);
|
|
14
|
+
const item = findItem(index);
|
|
15
|
+
const { wpTheme } = useWPTheme();
|
|
16
|
+
const handleDeltaChange = (e, delta) => {
|
|
17
|
+
// Get the rect of the item being dragged
|
|
18
|
+
const rect = ref.current?.getBoundingClientRect();
|
|
19
|
+
if (!rect) {
|
|
20
|
+
console.warn("SortableListItem ref is not set or does not have a bounding rect.");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
let newY, newX, transformValues = [];
|
|
24
|
+
//if (displayType === "horizontal" || displayType === "grid") {
|
|
25
|
+
if (["horizontal", "horizontal-fit", "grid"].includes(displayType)) {
|
|
26
|
+
newX = refPos.current.x + delta.x; // Update the X position relative to the element
|
|
27
|
+
transformValues.push(`translateX(${newX}px)`);
|
|
28
|
+
refPos.current.x = newX; // Update the reference position
|
|
29
|
+
}
|
|
30
|
+
if (displayType === "vertical" || displayType === "grid") {
|
|
31
|
+
newY = refPos.current.y + delta.y; // Update the Y position relative to the element
|
|
32
|
+
transformValues.push(`translateY(${newY}px)`);
|
|
33
|
+
refPos.current.y = newY; // Update the reference position
|
|
34
|
+
}
|
|
35
|
+
ref.current.style.transform = transformValues.join(" ");
|
|
36
|
+
// Get elements on the currrent moust position
|
|
37
|
+
const elementsAtPoint = document.elementsFromPoint(e.clientX, e.clientY);
|
|
38
|
+
// timeoutId.current = setTimeout(() => {
|
|
39
|
+
for (const element of elementsAtPoint) {
|
|
40
|
+
// get the data attribute
|
|
41
|
+
const itemIndex = element.getAttribute("data-sortable-item-index");
|
|
42
|
+
if (!itemIndex) {
|
|
43
|
+
// Skip if the element does not have the data attribute
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Skip if itemValue is null or the same as the current value
|
|
47
|
+
if (itemIndex === String(index)) {
|
|
48
|
+
if (targetRef.current) {
|
|
49
|
+
targetRef.current.style.removeProperty("border");
|
|
50
|
+
targetRef.current = null;
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const itemIndexNumber = parseInt(itemIndex);
|
|
55
|
+
if (itemIndexNumber < 0 || itemIndexNumber >= items.length) {
|
|
56
|
+
continue; // Skip invalid indices
|
|
57
|
+
}
|
|
58
|
+
if (targetRef.current) {
|
|
59
|
+
targetRef.current.style.removeProperty("border");
|
|
60
|
+
}
|
|
61
|
+
targetRef.current = element;
|
|
62
|
+
targetRef.current.style.border = "1px solid red";
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const handleMouseDown = (e) => {
|
|
67
|
+
console.log("mouse down on item", index, e);
|
|
68
|
+
const rect = ref.current?.getBoundingClientRect();
|
|
69
|
+
if (rect) {
|
|
70
|
+
refPos.current.y = e.clientY - rect.top - rect.height / 2; // Calculate the initial Y offset
|
|
71
|
+
refPos.current.x = 0;
|
|
72
|
+
ref.current.style.zIndex = "100000"; // Bring the item to the front
|
|
73
|
+
ref.current.style.transform = `translate(${refPos.current.x}px, ${refPos.current.y}px)`;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const handleMouseUp = (e) => {
|
|
77
|
+
refPos.current.y = 0; // Reset Y position
|
|
78
|
+
refPos.current.x = 0; // Reset X position
|
|
79
|
+
ref.current?.style.removeProperty("transform");
|
|
80
|
+
ref.current?.style.removeProperty("z-index");
|
|
81
|
+
if (targetRef.current) {
|
|
82
|
+
const toItemIndex = targetRef.current.getAttribute("data-sortable-item-index");
|
|
83
|
+
swapItems(e, index, toItemIndex ? parseInt(toItemIndex) : -1);
|
|
84
|
+
targetRef.current?.style.removeProperty("border");
|
|
85
|
+
targetRef.current = null;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const { initMouseMove } = useMouseMove({
|
|
89
|
+
onDeltaChange: handleDeltaChange,
|
|
90
|
+
onMouseUp: handleMouseUp,
|
|
91
|
+
onMouseDown: handleMouseDown,
|
|
92
|
+
cursor: "grabbing",
|
|
93
|
+
threshold: 1,
|
|
94
|
+
});
|
|
95
|
+
// Assign the ref to the item
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
item.ref = ref;
|
|
98
|
+
}, [item]);
|
|
99
|
+
return (_jsxs(ListItem, { ref: ref, value: item.value, onMouseDown: initMouseMove(ref), "data-sortable-item-index": index, sx: {
|
|
100
|
+
position: "relative",
|
|
101
|
+
cursor: "move",
|
|
102
|
+
"&:hover": {
|
|
103
|
+
backgroundColor: wpTheme.background.hoverColor, // Use theme hover color
|
|
104
|
+
},
|
|
105
|
+
border: (theme) => `1px solid ${theme.palette.divider}`,
|
|
106
|
+
p: 0,
|
|
107
|
+
width: displayType === "horizontal" ? "auto" : "100%",
|
|
108
|
+
display: "flex",
|
|
109
|
+
}, children: [_jsx(Box, { sx: { flexGrow: 1, display: "flex", alignItems: "center" }, children: renderItem ? (renderItem(item)) : (_jsx(Typography, { size: size, sx: {
|
|
110
|
+
p: 0.5,
|
|
111
|
+
}, children: item.label })) }), onEdit && (_jsx(IconButton, { size: "small", sx: {
|
|
112
|
+
height: "100%",
|
|
113
|
+
}, onMouseDown: (e) => {
|
|
114
|
+
e.stopPropagation();
|
|
115
|
+
onEdit?.(index);
|
|
116
|
+
}, children: _jsx(EditIcon, { sx: {
|
|
117
|
+
fontSize: size === "small" ? "small" : "medium",
|
|
118
|
+
} }) })), onDelete && (_jsx(IconButton, { size: "small", sx: {
|
|
119
|
+
height: "100%",
|
|
120
|
+
}, onMouseDown: (e) => {
|
|
121
|
+
e.stopPropagation();
|
|
122
|
+
console.log("Removing item at index", index);
|
|
123
|
+
onDelete?.(index);
|
|
124
|
+
}, children: _jsx(Close, { sx: {
|
|
125
|
+
fontSize: size === "small" ? "small" : "medium",
|
|
126
|
+
} }) }))] }));
|
|
127
|
+
};
|
|
128
|
+
export const SortableList = (props) => {
|
|
129
|
+
const { size = "small", displayType = "vertical", renderItem, onDelete, onChange, onEdit, } = props;
|
|
130
|
+
const [items, setItems] = useState([]);
|
|
131
|
+
const targetRef = useRef(null);
|
|
132
|
+
const refPos = useRef({ x: 0, y: 0 });
|
|
133
|
+
const findItem = (index) => {
|
|
134
|
+
return items.find((item) => item.index === index);
|
|
135
|
+
};
|
|
136
|
+
const swapItems = (e, fromIndex, toIndex) => {
|
|
137
|
+
console.log("Swapping items", fromIndex, toIndex);
|
|
138
|
+
if (fromIndex < 0 ||
|
|
139
|
+
toIndex < 0 ||
|
|
140
|
+
fromIndex >= items.length ||
|
|
141
|
+
toIndex >= items.length) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// Swap items in the state
|
|
145
|
+
const newItems = [...items];
|
|
146
|
+
const temp = newItems[fromIndex];
|
|
147
|
+
newItems[fromIndex] = newItems[toIndex];
|
|
148
|
+
newItems[toIndex] = temp;
|
|
149
|
+
newItems[fromIndex].index = fromIndex; // Update index after swap
|
|
150
|
+
newItems[toIndex].index = toIndex; // Update index after swap
|
|
151
|
+
setItems(newItems);
|
|
152
|
+
onChange?.(newItems);
|
|
153
|
+
};
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
// Initialize items from the enum prop
|
|
156
|
+
const initialItems = props.enum.map((item, index) => ({
|
|
157
|
+
index,
|
|
158
|
+
value: item.value,
|
|
159
|
+
label: item.label,
|
|
160
|
+
ref: null,
|
|
161
|
+
}));
|
|
162
|
+
setItems(initialItems);
|
|
163
|
+
}, [props.enum]);
|
|
164
|
+
let sx = {};
|
|
165
|
+
if (props.displayType === "horizontal") {
|
|
166
|
+
sx = {
|
|
167
|
+
display: "flex",
|
|
168
|
+
flexDirection: "row",
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
else if (props.displayType === "horizontal-fit") {
|
|
172
|
+
sx = {
|
|
173
|
+
display: "flex",
|
|
174
|
+
flexDirection: "row",
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
else if (props.displayType === "grid") {
|
|
178
|
+
sx = {
|
|
179
|
+
display: "grid",
|
|
180
|
+
gridTemplateColumns: "repeat(auto-fill, minmax(100px, 1fr))",
|
|
181
|
+
gap: 1,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return (_jsx(Context, { value: {
|
|
185
|
+
items,
|
|
186
|
+
findItem: findItem,
|
|
187
|
+
swapItems,
|
|
188
|
+
refPos,
|
|
189
|
+
targetRef,
|
|
190
|
+
displayType,
|
|
191
|
+
renderItem: renderItem,
|
|
192
|
+
onDelete,
|
|
193
|
+
onEdit,
|
|
194
|
+
}, children: _jsx(List, { sx: { ...sx, ...props.sx }, children: items.map((item, index) => (_jsx(SortableListItem, { index: index, size: size }, item.index))) }) }));
|
|
195
|
+
};
|
package/Tabs.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SxProps } from "@mui/material";
|
|
2
|
+
import React from "react";
|
|
3
|
+
export declare const Tabs: (props: {
|
|
4
|
+
items: {
|
|
5
|
+
label: string;
|
|
6
|
+
content: React.ReactNode;
|
|
7
|
+
}[];
|
|
8
|
+
size?: "small" | "medium" | "large";
|
|
9
|
+
slotSxProps?: {
|
|
10
|
+
tab: SxProps;
|
|
11
|
+
typography?: SxProps;
|
|
12
|
+
};
|
|
13
|
+
sx?: SxProps;
|
|
14
|
+
tabIndex?: number;
|
|
15
|
+
onChange?: (index: number) => void;
|
|
16
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
//# sourceMappingURL=Tabs.d.ts.map
|
package/Tabs.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tabs.d.ts","sourceRoot":"","sources":["../src/Tabs.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAuC,OAAO,EAAE,MAAM,eAAe,CAAC;AAG7E,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,eAAO,MAAM,IAAI,GAAI,OAAO;IAC1B,KAAK,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;KAC1B,EAAE,CAAC;IACJ,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpC,WAAW,CAAC,EAAE;QACZ,GAAG,EAAE,OAAO,CAAC;QACb,UAAU,CAAC,EAAE,OAAO,CAAC;KACtB,CAAC;IACF,EAAE,CAAC,EAAE,OAAO,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC,4CAgEA,CAAC"}
|
package/Tabs.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Tab, Tabs as MuiTabs } from "@mui/material";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { Typography } from "./Typography";
|
|
5
|
+
export const Tabs = (props) => {
|
|
6
|
+
const { items, size, tabIndex, onChange } = props;
|
|
7
|
+
const [elementTabIndex, setElementTabIndex] = useState(tabIndex ?? 0);
|
|
8
|
+
const handleElementTabClick = (event, newValue) => {
|
|
9
|
+
// Handle element click logic here
|
|
10
|
+
setElementTabIndex(newValue);
|
|
11
|
+
onChange?.(newValue);
|
|
12
|
+
};
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (typeof tabIndex === "number" && elementTabIndex !== tabIndex) {
|
|
15
|
+
setElementTabIndex(tabIndex);
|
|
16
|
+
}
|
|
17
|
+
}, [tabIndex]);
|
|
18
|
+
const selectedContent = items[elementTabIndex]?.content;
|
|
19
|
+
if (!selectedContent) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return (_jsxs(_Fragment, { children: [_jsx(MuiTabs, { value: elementTabIndex, onChange: handleElementTabClick, sx: {
|
|
23
|
+
...props.sx,
|
|
24
|
+
}, children: items.map((item, index) => (_jsx(Tab, { label: _jsx(Typography, { size: size, bold: true, fontSize: !size || size === "small" ? 9 : "inherit", sx: {
|
|
25
|
+
...props.slotSxProps?.typography,
|
|
26
|
+
}, children: item.label }), value: index, sx: {
|
|
27
|
+
textTransform: "none",
|
|
28
|
+
p: 0,
|
|
29
|
+
"&.MuiTab-root": {
|
|
30
|
+
width: `calc(100% / ${items.length})`,
|
|
31
|
+
minWidth: "unset",
|
|
32
|
+
},
|
|
33
|
+
...props.slotSxProps?.tab,
|
|
34
|
+
} }, index))) }), selectedContent] }));
|
|
35
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type * as types from "./types";
|
|
3
|
+
export declare const useWPTheme: () => {
|
|
4
|
+
mode: "dark" | "light" | "system";
|
|
5
|
+
wpTheme: types.WPThemeProps;
|
|
6
|
+
muiTheme: import("@mui/material").Theme;
|
|
7
|
+
wpRawTheme: types.WPTheme;
|
|
8
|
+
};
|
|
9
|
+
export declare const baseMaterialTheme: any[];
|
|
10
|
+
export default function ThemeRegistry({ children, }: {
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
//# sourceMappingURL=ThemeRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeRegistry.d.ts","sourceRoot":"","sources":["../src/ThemeRegistry.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAc/B,OAAO,KAAK,KAAK,KAAK,MAAM,SAAS,CAAC;AAItC,eAAO,MAAM,UAAU;;;;;CAatB,CAAC;AAEF,eAAO,MAAM,iBAAiB,OAAK,CAAC;AAEpC,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,EACpC,QAAQ,GACT,EAAE;IACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,2CA4BA"}
|
package/ThemeRegistry.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { ThemeProvider as MuiThemeProvider, createTheme as createMuiTheme, Experimental_CssVarsProvider as MaterialCssVarsProvider, THEME_ID as MATERIAL_THEME_ID, useColorScheme as useMaterialColorScheme, useTheme, } from "@mui/material/styles";
|
|
5
|
+
import { useWP } from "@rnaga/wp-next-core/client/wp";
|
|
6
|
+
import { wpTheme } from "./wp-theme";
|
|
7
|
+
import { CssBaseline } from "@mui/material";
|
|
8
|
+
const WPThemeContext = React.createContext(wpTheme);
|
|
9
|
+
export const useWPTheme = () => {
|
|
10
|
+
const theme = React.useContext(WPThemeContext);
|
|
11
|
+
const { mode } = useMaterialColorScheme();
|
|
12
|
+
const muiTheme = useTheme();
|
|
13
|
+
const wpTheme = mode === "dark" ? theme.dark : theme.global;
|
|
14
|
+
return {
|
|
15
|
+
mode,
|
|
16
|
+
wpTheme,
|
|
17
|
+
muiTheme,
|
|
18
|
+
wpRawTheme: theme,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export const baseMaterialTheme = [];
|
|
22
|
+
export default function ThemeRegistry({ children, }) {
|
|
23
|
+
const { wpHooks } = useWP();
|
|
24
|
+
const initWPTheme = React.useContext(WPThemeContext);
|
|
25
|
+
const themes = wpHooks.filter.apply("next_ui_theme", {
|
|
26
|
+
mui: baseMaterialTheme,
|
|
27
|
+
wp: initWPTheme,
|
|
28
|
+
});
|
|
29
|
+
const muiThemeWithOverrides = createMuiTheme({
|
|
30
|
+
colorSchemes: {
|
|
31
|
+
dark: true,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const wpTheme = themes.wp;
|
|
35
|
+
return (_jsx(MaterialCssVarsProvider, { theme: { [MATERIAL_THEME_ID]: muiThemeWithOverrides }, children: _jsx(MuiThemeProvider, { theme: muiThemeWithOverrides, children: _jsxs(WPThemeContext, { value: wpTheme, children: [_jsx(CssBaseline, {}), children] }) }) }));
|
|
36
|
+
}
|
package/Typography.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Typography as MuiTypography } from "@mui/material";
|
|
2
|
+
export declare const Typography: (props: {
|
|
3
|
+
size?: "small" | "medium" | "large" | "xlarge";
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
bold?: boolean;
|
|
6
|
+
fontWeight?: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
7
|
+
component?: React.ElementType;
|
|
8
|
+
} & Omit<Parameters<typeof MuiTypography>[0], "children">) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
//# sourceMappingURL=Typography.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typography.d.ts","sourceRoot":"","sources":["../src/Typography.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,eAAe,CAAC;AAE5D,eAAO,MAAM,UAAU,GACrB,OAAO;IACL,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;IAC/C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,UAAU,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;IACjE,SAAS,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC;CAC/B,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,4CAsB1D,CAAC"}
|
package/Typography.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Typography as MuiTypography } from "@mui/material";
|
|
3
|
+
export const Typography = (props) => {
|
|
4
|
+
const { size, children, bold, fontWeight, component, ...rest } = props;
|
|
5
|
+
return (_jsx(MuiTypography, { component: component, variant: size === "large" ? "body1" : undefined, fontWeight: fontWeight ? fontWeight : bold ? "bold" : "normal", fontSize: size === "large"
|
|
6
|
+
? undefined
|
|
7
|
+
: size === "xlarge"
|
|
8
|
+
? 20
|
|
9
|
+
: size === "medium"
|
|
10
|
+
? 14
|
|
11
|
+
: 12, ...rest, children: children }));
|
|
12
|
+
};
|
package/Viewport.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Viewport.d.ts","sourceRoot":"","sources":["../src/Viewport.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,eAAO,MAAM,QAAQ,GAAI,OAAO;IAC9B,MAAM,EAAE,SAAS,GAAG,QAAQ,CAAC;IAC7B,QAAQ,EAAE,SAAS,CAAC;CACrB,cAYA,CAAC"}
|
package/Viewport.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useViewport } from "./hooks/use-viewport";
|
|
2
|
+
export const Viewport = (props) => {
|
|
3
|
+
const { device, children } = props;
|
|
4
|
+
const viewport = useViewport();
|
|
5
|
+
if ((device == "desktop" && viewport.isDesktop) ||
|
|
6
|
+
(device == "mobile" && viewport.isMobile)) {
|
|
7
|
+
return children;
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { FormEvent } from "react";
|
|
2
|
+
type FormRecord = Record<string, any>;
|
|
3
|
+
export declare const useFormDataContext: <T extends FormRecord>(key: string) => {
|
|
4
|
+
formData: T extends Partial<any> ? T : Partial<T>;
|
|
5
|
+
setFormData: <Key extends keyof T>(input: T | Record<Key, T[Key]>, options?: Partial<{
|
|
6
|
+
ready: boolean;
|
|
7
|
+
}>) => void;
|
|
8
|
+
resetFormData: () => void;
|
|
9
|
+
validation: {
|
|
10
|
+
valid: boolean;
|
|
11
|
+
error?: string;
|
|
12
|
+
errors?: Record<string, any>;
|
|
13
|
+
};
|
|
14
|
+
setFormReady: (ready: boolean) => void;
|
|
15
|
+
formReady: boolean;
|
|
16
|
+
};
|
|
17
|
+
type Validator<T> = (data: T) => boolean | [false, string] | [false, string, Record<string, any>];
|
|
18
|
+
export declare function useFormData<T extends FormRecord>(key: string, options?: {
|
|
19
|
+
initialValue?: T | undefined;
|
|
20
|
+
initialReady?: boolean;
|
|
21
|
+
}): {
|
|
22
|
+
formData: T extends Partial<any> ? T : Partial<T>;
|
|
23
|
+
setFormData: <Key extends keyof T>(input: Record<Key, T[Key]> | T, options?: Partial<{
|
|
24
|
+
ready: boolean;
|
|
25
|
+
}>) => void;
|
|
26
|
+
resetFormData: () => void;
|
|
27
|
+
umount: () => void;
|
|
28
|
+
submit: (callback: (data: T extends Partial<any> ? T : Partial<T>) => any | Promise<any>, validator?: Validator<T extends Partial<any> ? T : Partial<T>>) => (e: FormEvent<HTMLFormElement>) => void;
|
|
29
|
+
validation: {
|
|
30
|
+
valid: boolean;
|
|
31
|
+
error?: string;
|
|
32
|
+
errors?: Record<string, any>;
|
|
33
|
+
};
|
|
34
|
+
value: () => /*elided*/ any;
|
|
35
|
+
formReady: boolean;
|
|
36
|
+
setFormReady: (ready: boolean) => void;
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=use-form-data.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-form-data.d.ts","sourceRoot":"","sources":["../../src/hooks/use-form-data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmC,MAAM,OAAO,CAAC;AAInE,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAgBtC,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,UAAU,EAAE,KAAK,MAAM;;kBAyC7C,GAAG;eAGb,OAAO;;;;eAhBT,OAAO;gBACN,MAAM;iBACL,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;;0BAwBD,OAAO;;CA3CrC,CAAC;AAEF,KAAK,SAAS,CAAC,CAAC,IAAI,CAClB,IAAI,EAAE,CAAC,KACJ,OAAO,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;AAEtE,wBAAgB,WAAW,CAAC,CAAC,SAAS,UAAU,EAC9C,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE;IACR,YAAY,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;IAC7B,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;;kBAmBoB,GAAG,SAAS,MAAM,CAAC,SAC/B,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,YACpB,OAAO,CAAC;QAChB,KAAK,EAAE,OAAO,CAAC;KAChB,CAAC;;;uBAwBU,CAAC,IAAI,yCAAiB,KAAK,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,cAC3C,SAAS,yCAAiB,MAEvC,GAAG,SAAS,CAAC,eAAe,CAAC;;eA5CvB,OAAO;gBACN,MAAM;iBACL,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;;;eAwFL,OAAO;0BAhEH,OAAO;EAuErC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from "react";
|
|
2
|
+
import { FormDataContext } from "../FormDataProvider";
|
|
3
|
+
const useFormDataMap = (key) => {
|
|
4
|
+
const formDataMap = useContext(FormDataContext);
|
|
5
|
+
const formData = formDataMap.get(key);
|
|
6
|
+
if (!formData) {
|
|
7
|
+
console.log("formDataMap.keys()", formDataMap.keys());
|
|
8
|
+
throw new Error(`Form Data not found - ${key}`);
|
|
9
|
+
}
|
|
10
|
+
return formData ?? {};
|
|
11
|
+
};
|
|
12
|
+
export const useFormDataContext = (key) => {
|
|
13
|
+
const context = useFormDataMap(key);
|
|
14
|
+
return {
|
|
15
|
+
formData: context.formData,
|
|
16
|
+
setFormData: context.setFormData,
|
|
17
|
+
resetFormData: context.resetFormData,
|
|
18
|
+
validation: context.validation,
|
|
19
|
+
setFormReady: context.setFormReady,
|
|
20
|
+
formReady: context.formReady,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export function useFormData(key, options) {
|
|
24
|
+
const { initialValue, initialReady = false } = options ?? {};
|
|
25
|
+
const formDataMap = useContext(FormDataContext);
|
|
26
|
+
const [validation, setValidation] = useState();
|
|
27
|
+
const [formData, setFormDataState] = useState({ ...(initialValue ?? {}), ___ready: initialReady });
|
|
28
|
+
const resetFormData = () => {
|
|
29
|
+
setFormDataState({});
|
|
30
|
+
};
|
|
31
|
+
const setFormData = (input, options) => {
|
|
32
|
+
setFormDataState({
|
|
33
|
+
...formData,
|
|
34
|
+
...input,
|
|
35
|
+
___ready: formData.___ready === true ? true : options?.ready ?? false,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const setFormReady = (ready) => {
|
|
39
|
+
setFormDataState({ ...formData, ___ready: ready });
|
|
40
|
+
};
|
|
41
|
+
const umount = () => {
|
|
42
|
+
//console.log("umount", key);
|
|
43
|
+
//formDataMap.delete(key);
|
|
44
|
+
};
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
return umount();
|
|
47
|
+
}, []);
|
|
48
|
+
const submit = (callback, validator) => (e) => {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
const data = {
|
|
51
|
+
...formData,
|
|
52
|
+
...Object.fromEntries(new FormData(e.currentTarget)),
|
|
53
|
+
};
|
|
54
|
+
let isValid = true;
|
|
55
|
+
if (validator) {
|
|
56
|
+
const result = validator(data);
|
|
57
|
+
const newState = result === true
|
|
58
|
+
? {
|
|
59
|
+
valid: true,
|
|
60
|
+
error: undefined,
|
|
61
|
+
errors: undefined,
|
|
62
|
+
}
|
|
63
|
+
: {
|
|
64
|
+
valid: false,
|
|
65
|
+
error: Array.isArray(result) ? result[1] : undefined,
|
|
66
|
+
errors: Array.isArray(result) ? result[2] : undefined,
|
|
67
|
+
};
|
|
68
|
+
setValidation(newState);
|
|
69
|
+
isValid = result === true;
|
|
70
|
+
}
|
|
71
|
+
if (!isValid) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
setValidation({ valid: true, error: undefined, errors: undefined });
|
|
75
|
+
callback(data);
|
|
76
|
+
};
|
|
77
|
+
const { ___ready, ...formDataState } = formData;
|
|
78
|
+
const value = {
|
|
79
|
+
formData: formDataState,
|
|
80
|
+
setFormData,
|
|
81
|
+
resetFormData,
|
|
82
|
+
umount,
|
|
83
|
+
submit,
|
|
84
|
+
validation,
|
|
85
|
+
value: () => value,
|
|
86
|
+
formReady: ___ready,
|
|
87
|
+
setFormReady,
|
|
88
|
+
};
|
|
89
|
+
formDataMap.set(key, value);
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type * as wpCoreTypes from "@rnaga/wp-next-core/types";
|
|
2
|
+
type OnSelected = NonNullable<NonNullable<wpCoreTypes.client.GlobalState["media-selector-modal"]>["onSelected"]>;
|
|
3
|
+
export declare const useMediaSelector: () => {
|
|
4
|
+
open: (...args: [OnSelected] | [string[], OnSelected]) => void;
|
|
5
|
+
close: () => void;
|
|
6
|
+
select: (post: wpCoreTypes.actions.Posts[number]) => void;
|
|
7
|
+
isOpen: boolean;
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=use-media-selector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-media-selector.d.ts","sourceRoot":"","sources":["../../src/hooks/use-media-selector.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,WAAW,MAAM,2BAA2B,CAAC;AAE9D,KAAK,UAAU,GAAG,WAAW,CAC3B,WAAW,CACT,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,sBAAsB,CAAC,CACvD,CAAC,YAAY,CAAC,CAChB,CAAC;AAEF,eAAO,MAAM,gBAAgB;oBAIJ,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,UAAU,CAAC;;mBAqBtC,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC;;CAUxD,CAAC"}
|