@rnaga/wp-next-ui 1.0.8 → 1.0.10

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,17 @@
1
+ import { Button } from "./Button";
2
+ import { RefObject } from "react";
3
+ export type CheckmarksItem = {
4
+ label: string;
5
+ value: string;
6
+ };
7
+ export declare const Checkmarks: (props: {
8
+ ref?: RefObject<HTMLElement | null>;
9
+ items: CheckmarksItem[];
10
+ onChange: (values: string[], items: CheckmarksItem[]) => void;
11
+ label?: string;
12
+ size?: "small" | "medium";
13
+ values?: string[];
14
+ showArrowIcon?: boolean;
15
+ disabled?: boolean;
16
+ } & Omit<Parameters<typeof Button>[0], "onChange" | "ref" | "label" | "value" | "children" | "disabled">) => import("react/jsx-runtime").JSX.Element;
17
+ //# sourceMappingURL=Checkmarks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Checkmarks.d.ts","sourceRoot":"","sources":["../src/Checkmarks.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAKlC,OAAO,EAAY,SAAS,EAAuB,MAAM,OAAO,CAAC;AAKjE,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,eAAO,MAAM,UAAU,GACrB,OAAO;IACL,GAAG,CAAC,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IACpC,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,KAAK,IAAI,CAAC;IAC9D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,GAAG,IAAI,CACN,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC,EAC5B,UAAU,GAAG,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CACjE,4CAoJF,CAAC"}
package/Checkmarks.js ADDED
@@ -0,0 +1,76 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Button } from "./Button";
3
+ import Menu from "@mui/material/Menu";
4
+ import MenuItem from "@mui/material/MenuItem";
5
+ import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown";
6
+ import ArrowDropUpIcon from "@mui/icons-material/ArrowDropUp";
7
+ import { Fragment, useEffect, useState } from "react";
8
+ import { Typography } from "./Typography";
9
+ import { useWPTheme } from "./ThemeRegistry";
10
+ import { Checkbox } from "./Checkbox";
11
+ export const Checkmarks = (props) => {
12
+ const { onChange, label, ref: buttonRef, size, showArrowIcon = true, disabled = false, values = [], ...rest } = props;
13
+ const fontSize = size == "medium" ? 14 : 12;
14
+ const { wpTheme } = useWPTheme();
15
+ const [anchorEl, setAnchorEl] = useState(null);
16
+ const open = Boolean(anchorEl);
17
+ const [selectedValues, setSelectedValues] = useState(values);
18
+ const [buttonWidth, setButtonWidth] = useState("100px");
19
+ const [items, setItems] = useState();
20
+ const handleClick = (event) => {
21
+ setAnchorEl(event.currentTarget);
22
+ };
23
+ const handleClose = () => {
24
+ setAnchorEl(null);
25
+ };
26
+ const handleToggle = (value, item) => {
27
+ const newSelectedValues = selectedValues.includes(value)
28
+ ? selectedValues.filter((v) => v !== value)
29
+ : [...selectedValues, value];
30
+ setSelectedValues(newSelectedValues);
31
+ const selectedItems = props.items.filter((item) => newSelectedValues.includes(item.value));
32
+ onChange(newSelectedValues, selectedItems);
33
+ };
34
+ useEffect(() => {
35
+ setSelectedValues(values);
36
+ }, [values]);
37
+ useEffect(() => {
38
+ setItems(props.items);
39
+ }, [props.items]);
40
+ const buttonLabel = selectedValues.length > 0
41
+ ? `${selectedValues.length} selected`
42
+ : label || "Select items";
43
+ return (_jsxs(Fragment, { children: [_jsxs(Button, { ...rest, id: "checkmarks-button", size: "small", disabled: disabled, "aria-controls": open ? "checkmarks-menu" : undefined, "aria-haspopup": "true", "aria-expanded": open ? "true" : undefined, onClick: handleClick, sx: {
44
+ textTransform: "none",
45
+ border: "1px solid",
46
+ borderColor: wpTheme.border.color,
47
+ justifyContent: "space-between",
48
+ height: size == "medium" ? 32 : 24,
49
+ backgroundColor: wpTheme.background.color,
50
+ color: wpTheme.text.color,
51
+ ...rest.sx,
52
+ }, ref: (ref) => {
53
+ if (ref) {
54
+ setButtonWidth(ref.clientWidth + "px");
55
+ if (buttonRef) {
56
+ buttonRef.current = ref;
57
+ }
58
+ }
59
+ }, children: [_jsx(Typography, { fontSize: fontSize, sx: {
60
+ ...(disabled ? { opacity: 0.5 } : {}),
61
+ }, children: buttonLabel }), disabled === false && (_jsxs(_Fragment, { children: [showArrowIcon && !open && _jsx(ArrowDropDownIcon, { fontSize: "small" }), showArrowIcon && open && _jsx(ArrowDropUpIcon, { fontSize: "small" })] }))] }), _jsx(Menu, { id: "checkmarks-menu", anchorEl: anchorEl, open: disabled === true ? false : open, onClose: handleClose, sx: {
62
+ width: "100%",
63
+ }, children: items?.map((item, index) => {
64
+ const isChecked = selectedValues.includes(item.value);
65
+ return (_jsxs(MenuItem, { onClick: (e) => {
66
+ handleToggle(item.value, item);
67
+ }, sx: {
68
+ width: buttonWidth,
69
+ }, children: [_jsx(Checkbox, { checked: isChecked, size: size, sx: {
70
+ padding: 0,
71
+ marginRight: 1,
72
+ } }), _jsx(Typography, { fontSize: fontSize, sx: {
73
+ minHeight: size == "medium" ? 20 : 16,
74
+ }, component: "div", children: item.label })] }, index));
75
+ }) })] }, label));
76
+ };
package/Input.d.ts CHANGED
@@ -10,5 +10,6 @@ export declare const Input: (props: {
10
10
  clearable?: boolean;
11
11
  readOnly?: boolean;
12
12
  removeBorderOnFocus?: boolean;
13
+ isEmpty?: boolean;
13
14
  } & Omit<Parameters<typeof MuiInput>[0], "onChange" | "value" | "onBlur" | "size" | "readOnly">) => import("react/jsx-runtime").JSX.Element;
14
15
  //# sourceMappingURL=Input.d.ts.map
package/Input.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Input.d.ts","sourceRoot":"","sources":["../src/Input.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAKnE,eAAO,MAAM,KAAK,GAChB,OAAO;IACL,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAC;IAC5E,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,OAAO,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mBAAmB,CAAC,EAAE,OAAO,CAAC;CAC/B,GAAG,IAAI,CACN,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EAC9B,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CACtD,4CA+HF,CAAC"}
1
+ {"version":3,"file":"Input.d.ts","sourceRoot":"","sources":["../src/Input.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAKnE,eAAO,MAAM,KAAK,GAChB,OAAO;IACL,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAC;IAC5E,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,OAAO,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;IACpC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,IAAI,CACN,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EAC9B,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,CACtD,4CAsIF,CAAC"}
package/Input.js CHANGED
@@ -3,7 +3,7 @@ import { Box, IconButton, Input as MuiInput } from "@mui/material";
3
3
  import { useEffect, useState } from "react";
4
4
  import ClearIcon from "@mui/icons-material/Clear";
5
5
  export const Input = (props) => {
6
- const { onChange, onBlur, onClear, canClear, size, disableBorder, sx, readOnly = false, clearable = false, removeBorderOnFocus = false, multiline, value: _value, ...rest } = props;
6
+ const { onChange, onBlur, onClear, canClear, size, disableBorder, sx, readOnly = false, clearable = false, removeBorderOnFocus = false, multiline, value: _value, isEmpty, ...rest } = props;
7
7
  const [value, setValue] = useState(_value ?? "");
8
8
  const handleChange = (e) => {
9
9
  const value = e.target.value;
@@ -26,6 +26,11 @@ export const Input = (props) => {
26
26
  return;
27
27
  setValue(_value);
28
28
  }, [_value]);
29
+ useEffect(() => {
30
+ if (isEmpty === true) {
31
+ setValue("");
32
+ }
33
+ }, [isEmpty]);
29
34
  return (_jsx(MuiInput, { disableUnderline: true, readOnly: readOnly, onBlur: handleBlur, value: value, onChange: handleChange, multiline: multiline,
30
35
  // onFocus={(e) => e.target.select?.()}
31
36
  sx: {
package/ListBase.d.ts CHANGED
@@ -40,6 +40,7 @@ export type ListBaseProps<T = string> = {
40
40
  onMouseDown?: (item: ListItemType<T>, event: React.MouseEvent) => void;
41
41
  itemEventHandlers?: ListItemEventHandlers<T>;
42
42
  getItemSx?: (item: ListItemType<T>, index: number) => SxProps;
43
+ getItemDataAttributes?: (item: ListItemType<T>, index: number) => Record<string, string | number>;
43
44
  dataAttributes?: Record<string, string | number>;
44
45
  slotSxProps?: {
45
46
  listItem?: SxProps;
package/ListBase.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ListBase.d.ts","sourceRoot":"","sources":["../src/ListBase.tsx"],"names":[],"mappings":"AAAA,OAAO,EAKL,OAAO,EACR,MAAM,eAAe,CAAC;AAEvB,OAAO,EAEL,GAAG,EACH,SAAS,EAIV,MAAM,OAAO,CAAC;AAKf,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,MAAM,IAAI;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,CAAC,CAAC;IACT,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;CACrC,CAAC;AAEF,KAAK,WAAW,GAAG,UAAU,GAAG,YAAY,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAEzE,KAAK,qBAAqB,CAAC,CAAC,GAAG,MAAM,IAAI;IACvC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,UAAU,KAAK,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AAK,CAAC,UAAU,KAAK,IAAI,CAAC;IACvE,iBAAiB,CAAC,EAAE,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAC7C,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IAC9D,qBAAqB,CAAC,EAAE,CACtB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,MAAM,KACV,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IACrC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IACjD,WAAW,CAAC,EAAE;QACZ,QAAQ,CAAC,EAAE,OAAO,CAAC;KACpB,CAAC;CACH,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,GAAG,GAAG,MAAM,EAAE,OAAO,aAAa,CAAC,CAAC,CAAC,4CA+EvE,CAAC"}
package/ListBase.js CHANGED
@@ -10,7 +10,7 @@ export const ListItem = (props) => {
10
10
  const { item, index, size, dataAttributes, sx } = props;
11
11
  const ref = useRef(null);
12
12
  const [isHovered, setIsHovered] = useState(false);
13
- const { displayType, renderItem, onDelete, onEdit, itemEventHandlers, getItemSx, slotSxProps, editable, } = useContext(ListContext);
13
+ const { displayType, renderItem, onDelete, onEdit, itemEventHandlers, getItemSx, getItemDataAttributes, slotSxProps, editable, } = useContext(ListContext);
14
14
  const { wpTheme } = useWPTheme();
15
15
  const handleEvent = (handler) => {
16
16
  return handler
@@ -30,7 +30,11 @@ export const ListItem = (props) => {
30
30
  ...(slotSxProps?.listItem || {}),
31
31
  ...sx,
32
32
  };
33
- return (_jsxs(MuiListItem, { ref: ref, value: item.value, onClick: handleEvent(itemEventHandlers?.onClick), onMouseDown: handleEvent(itemEventHandlers?.onMouseDown), onMouseUp: handleEvent(itemEventHandlers?.onMouseUp), onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, onDoubleClick: handleEvent(itemEventHandlers?.onDoubleClick), onContextMenu: handleEvent(itemEventHandlers?.onContextMenu), onMouseOver: handleEvent(itemEventHandlers?.onMouseOver), ...(dataAttributes || {}), sx: {
33
+ const itemDataAttributes = {
34
+ ...(dataAttributes || {}),
35
+ ...(getItemDataAttributes ? getItemDataAttributes(item, index) : {}),
36
+ };
37
+ return (_jsxs(MuiListItem, { ref: ref, value: item.value, onClick: handleEvent(itemEventHandlers?.onClick), onMouseDown: handleEvent(itemEventHandlers?.onMouseDown), onMouseUp: handleEvent(itemEventHandlers?.onMouseUp), onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, onDoubleClick: handleEvent(itemEventHandlers?.onDoubleClick), onContextMenu: handleEvent(itemEventHandlers?.onContextMenu), onMouseOver: handleEvent(itemEventHandlers?.onMouseOver), ...itemDataAttributes, sx: {
34
38
  position: "relative",
35
39
  "&:hover": {
36
40
  backgroundColor: wpTheme.background.hoverColor,
@@ -60,7 +64,7 @@ export const ListItem = (props) => {
60
64
  } }) }))] }));
61
65
  };
62
66
  export const ListBase = (props) => {
63
- const { items, size = "small", displayType = "vertical", renderItem, onDelete, onEdit, onClick, onMouseDown, itemEventHandlers, getItemSx, dataAttributes, slotSxProps, editable = false, } = props;
67
+ const { items, size = "small", displayType = "vertical", renderItem, onDelete, onEdit, onClick, onMouseDown, itemEventHandlers, getItemSx, getItemDataAttributes, dataAttributes, slotSxProps, editable = false, } = props;
64
68
  const listItems = items.map((item, index) => ({
65
69
  index,
66
70
  value: item.value,
@@ -100,6 +104,7 @@ export const ListBase = (props) => {
100
104
  onEdit,
101
105
  itemEventHandlers: mergedEventHandlers,
102
106
  getItemSx: getItemSx,
107
+ getItemDataAttributes: getItemDataAttributes,
103
108
  slotSxProps,
104
109
  editable,
105
110
  }, children: _jsx(MuiList, { sx: { ...sx, ...props.sx }, children: listItems.map((item, index) => (_jsx(ListItem, { item: item, index: index, size: size, dataAttributes: dataAttribut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version":3,"file":"SortableList.d.ts","sourceRoot":"","sources":["../src/SortableList.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,OAAO,EAEL,GAAG,EAKJ,MAAM,OAAO,CAAC;AAGf,MAAM,MAAM,oBAAoB,CAAC,CAAC,GAAG,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;AAE/D,KAAK,WAAW,GAAG,UAAU,GAAG,YAAY,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAczE,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,4CAgPA,CAAC"}
package/SortableList.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { Box } from "@mui/material";
3
2
  import { useMouseMove } from "./hooks/use-mouse-move";
4
3
  import { createContext, useEffect, useRef, useState, } from "react";
5
4
  import { ListBase } from "./ListBase";
@@ -9,18 +8,23 @@ export const SortableList = (props) => {
9
8
  const [items, setItems] = useState([]);
10
9
  const targetRef = useRef(null);
11
10
  const refPos = useRef({ x: 0, y: 0 });
11
+ const itemRefs = useRef(new Map());
12
+ const draggedItemRef = useRef(null);
13
+ const itemsRef = useRef(items);
14
+ itemsRef.current = items;
12
15
  const findItem = (index) => {
13
16
  return items.find((item) => item.index === index);
14
17
  };
15
18
  const swapItems = (_e, fromIndex, toIndex) => {
16
19
  console.log("Swapping items", fromIndex, toIndex);
20
+ const currentItems = itemsRef.current;
17
21
  if (fromIndex < 0 ||
18
22
  toIndex < 0 ||
19
- fromIndex >= items.length ||
20
- toIndex >= items.length) {
23
+ fromIndex >= currentItems.length ||
24
+ toIndex >= currentItems.length) {
21
25
  return;
22
26
  }
23
- const newItems = [...items];
27
+ const newItems = [...currentItems];
24
28
  const temp = newItems[fromIndex];
25
29
  newItems[fromIndex] = newItems[toIndex];
26
30
  newItems[toIndex] = temp;
@@ -29,6 +33,90 @@ export const SortableList = (props) => {
29
33
  setItems(newItems);
30
34
  onChange?.(newItems);
31
35
  };
36
+ const handleDeltaChange = (e, delta) => {
37
+ const dragged = draggedItemRef.current;
38
+ if (!dragged)
39
+ return;
40
+ const { element, index } = dragged;
41
+ const rect = element.getBoundingClientRect();
42
+ if (!rect)
43
+ return;
44
+ let newY;
45
+ let newX;
46
+ const transformValues = [];
47
+ if (["horizontal", "horizontal-fit", "grid"].includes(displayType)) {
48
+ newX = refPos.current.x + delta.x;
49
+ transformValues.push(`translateX(${newX}px)`);
50
+ refPos.current.x = newX;
51
+ }
52
+ if (displayType === "vertical" || displayType === "grid") {
53
+ newY = refPos.current.y + delta.y;
54
+ transformValues.push(`translateY(${newY}px)`);
55
+ refPos.current.y = newY;
56
+ }
57
+ element.style.transform = transformValues.join(" ");
58
+ const elementsAtPoint = document.elementsFromPoint(e.clientX, e.clientY);
59
+ let foundTarget = null;
60
+ for (const el of elementsAtPoint) {
61
+ // Find the sortable item container (could be the element itself or an ancestor)
62
+ const sortableItem = el.closest("[data-sortable-item-index]");
63
+ if (!sortableItem)
64
+ continue;
65
+ const itemIndex = sortableItem.getAttribute("data-sortable-item-index");
66
+ if (!itemIndex || itemIndex === String(index))
67
+ continue;
68
+ const itemIndexNumber = parseInt(itemIndex);
69
+ if (itemIndexNumber < 0 || itemIndexNumber >= itemsRef.current.length)
70
+ continue;
71
+ foundTarget = sortableItem;
72
+ break;
73
+ }
74
+ if (targetRef.current && targetRef.current !== foundTarget) {
75
+ targetRef.current.style.removeProperty("border");
76
+ }
77
+ if (foundTarget && foundTarget !== targetRef.current) {
78
+ foundTarget.style.border = "1px solid red";
79
+ }
80
+ targetRef.current = foundTarget;
81
+ };
82
+ const handleMouseDown = (e) => {
83
+ const dragged = draggedItemRef.current;
84
+ if (!dragged)
85
+ return;
86
+ const { element, index } = dragged;
87
+ console.log("Mouse down on item", index);
88
+ const rect = element.getBoundingClientRect();
89
+ if (rect) {
90
+ refPos.current.y = e.clientY - rect.top - rect.height / 2;
91
+ refPos.current.x = 0;
92
+ element.style.zIndex = "100000";
93
+ element.style.transform = `translate(${refPos.current.x}px, ${refPos.current.y}px)`;
94
+ }
95
+ };
96
+ const handleMouseUp = (e) => {
97
+ const dragged = draggedItemRef.current;
98
+ if (!dragged)
99
+ return;
100
+ const { element, index } = dragged;
101
+ refPos.current.y = 0;
102
+ refPos.current.x = 0;
103
+ element.style.removeProperty("transform");
104
+ element.style.removeProperty("z-index");
105
+ if (targetRef.current) {
106
+ const toItemIndex = targetRef.current.getAttribute("data-sortable-item-index");
107
+ swapItems(e, index, toItemIndex ? parseInt(toItemIndex) : -1);
108
+ targetRef.current?.style.removeProperty("border");
109
+ targetRef.current = null;
110
+ }
111
+ draggedItemRef.current = null;
112
+ };
113
+ const { initMouseMove } = useMouseMove({
114
+ onDeltaChange: handleDeltaChange,
115
+ onMouseUp: handleMouseUp,
116
+ onMouseDown: handleMouseDown,
117
+ cursor: "grabbing",
118
+ threshold: 1,
119
+ });
32
120
  useEffect(() => {
33
121
  const initialItems = props.enum.map((item, index) => ({
34
122
  index,
@@ -58,83 +146,17 @@ export const SortableList = (props) => {
58
146
  gap: 1,
59
147
  };
60
148
  }
61
- const renderSortableItem = (item) => {
62
- return (_jsx(Box, { ref: item.ref, onMouseDown: initMouseMoveForItem(item), "data-sortable-item-index": item.index, sx: {
63
- cursor: "move",
64
- }, children: renderItem ? renderItem(item) : item.label }));
149
+ const handleItemMouseDown = (item, e) => {
150
+ // Find the MuiListItem parent element
151
+ const listItemElement = e.target.closest("[data-sortable-item-index]");
152
+ if (listItemElement) {
153
+ itemRefs.current.set(item.index, listItemElement);
154
+ draggedItemRef.current = { index: item.index, element: listItemElement };
155
+ initMouseMove(listItemElement)(e);
156
+ }
65
157
  };
66
- const initMouseMoveForItem = (item) => {
67
- const ref = item.ref;
68
- if (!ref)
69
- return () => { };
70
- const handleDeltaChange = (e, delta) => {
71
- const rect = ref.current?.getBoundingClientRect();
72
- if (!rect)
73
- return;
74
- let newY;
75
- let newX;
76
- const transformValues = [];
77
- if (["horizontal", "horizontal-fit", "grid"].includes(displayType)) {
78
- newX = refPos.current.x + delta.x;
79
- transformValues.push(`translateX(${newX}px)`);
80
- refPos.current.x = newX;
81
- }
82
- if (displayType === "vertical" || displayType === "grid") {
83
- newY = refPos.current.y + delta.y;
84
- transformValues.push(`translateY(${newY}px)`);
85
- refPos.current.y = newY;
86
- }
87
- ref.current.style.transform = transformValues.join(" ");
88
- const elementsAtPoint = document.elementsFromPoint(e.clientX, e.clientY);
89
- for (const element of elementsAtPoint) {
90
- const itemIndex = element.getAttribute("data-sortable-item-index");
91
- if (!itemIndex || itemIndex === String(item.index)) {
92
- if (targetRef.current) {
93
- targetRef.current.style.removeProperty("border");
94
- targetRef.current = null;
95
- }
96
- continue;
97
- }
98
- const itemIndexNumber = parseInt(itemIndex);
99
- if (itemIndexNumber < 0 || itemIndexNumber >= items.length)
100
- continue;
101
- if (targetRef.current) {
102
- targetRef.current.style.removeProperty("border");
103
- }
104
- targetRef.current = element;
105
- targetRef.current.style.border = "1px solid red";
106
- break;
107
- }
108
- };
109
- const handleMouseDown = (e) => {
110
- const rect = ref.current?.getBoundingClientRect();
111
- if (rect) {
112
- refPos.current.y = e.clientY - rect.top - rect.height / 2;
113
- refPos.current.x = 0;
114
- ref.current.style.zIndex = "100000";
115
- ref.current.style.transform = `translate(${refPos.current.x}px, ${refPos.current.y}px)`;
116
- }
117
- };
118
- const handleMouseUp = (e) => {
119
- refPos.current.y = 0;
120
- refPos.current.x = 0;
121
- ref.current?.style.removeProperty("transform");
122
- ref.current?.style.removeProperty("z-index");
123
- if (targetRef.current) {
124
- const toItemIndex = targetRef.current.getAttribute("data-sortable-item-index");
125
- swapItems(e, item.index, toItemIndex ? parseInt(toItemIndex) : -1);
126
- targetRef.current?.style.removeProperty("border");
127
- targetRef.current = null;
128
- }
129
- };
130
- const { initMouseMove } = useMouseMove({
131
- onDeltaChange: handleDeltaChange,
132
- onMouseUp: handleMouseUp,
133
- onMouseDown: handleMouseDown,
134
- cursor: "grabbing",
135
- threshold: 1,
136
- });
137
- return initMouseMove(ref);
158
+ const renderSortableItem = (item) => {
159
+ return renderItem ? renderItem(item) : item.label;
138
160
  };
139
161
  return (_jsx(SortableContext.Provider, { value: {
140
162
  items,
@@ -143,5 +165,11 @@ export const SortableList = (props) => {
143
165
  refPos,
144
166
  targetRef,
145
167
  displayType,
146
- }, children: _jsx(ListBase, { items: items, size: size, sx: { ...sx, ...props.sx }, displayType: displayType, renderItem: renderSortableItem, onDelete: onDelete, onEdit: onEdit }) }));
168
+ }, children: _jsx(ListBase, { items: items, size: size, sx: { ...sx, ...props.sx }, displayType: displayType, renderItem: renderSortableItem, onDelete: onDelete, onEdit: onEdit, onMouseDown: handleItemMouseDown, getItemDataAttributes: (item) => ({
169
+ "data-sortable-item-index": item.index,
170
+ }), slotSxProps: {
171
+ listItem: {
172
+ cursor: "move",
173
+ },
174
+ } }) }));
147
175
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rnaga/wp-next-ui",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "scripts": {
5
5
  "build": "node ../../scripts/build-ui.mjs",
6
6
  "release-old": "node ../../scripts/release.mjs",