@rnaga/wp-next-ui 1.0.8 → 1.0.9
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/Checkmarks.d.ts +17 -0
- package/Checkmarks.d.ts.map +1 -0
- package/Checkmarks.js +76 -0
- package/package.json +1 -1
package/Checkmarks.d.ts
ADDED
|
@@ -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
|
+
};
|