@trackunit/react-filter-components 0.0.2

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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ > **⚠️ Beta**
2
+ >
3
+ > This is a beta version and subject to change without notice.
4
+
5
+ # Trackunit React Filter Components
6
+ The `@trackunit/react-filter-components` package is the home of Trackunits public react components used for Filters.
7
+
8
+ This library is exposed publicly for use in the the Trackunit [Iris App SDK](https://www.npmjs.com/package/@trackunit/iris-app).
9
+
10
+ To browse all avaliable components visit our [Public Storybook](https://developers.trackunit.com/page/ui-components).
11
+
12
+ For more info and a full guide on Iris App SDK Development, please visit our [Developer Hub](https://developers.trackunit.com/).
13
+
14
+ ## Development
15
+ At this point this library is only developed by Trackunit Employees.
16
+ For development related information see the [development readme](https://github.com/Trackunit/manager/blob/master/libs/react/components/DEVELOPMENT.md).
17
+
18
+ ## Trackunit
19
+ This package was developed by Trackunit ApS.
20
+ Trackunit is the leading SaaS-based IoT solution for the construction industry, offering an ecosystem of hardware, fleet management software & telematics.
21
+
22
+ ![The Trackunit logo](https://trackunit.com/wp-content/uploads/2022/03/top-logo.svg)
package/index.js ADDED
@@ -0,0 +1,139 @@
1
+ import { jsx, jsxs } from 'react/jsx-runtime';
2
+ import { Button, MenuList, Popover, MenuItem, Checkbox, Spinner } from '@trackunit/react-components';
3
+ import { twx, tws, tw } from '@trackunit/tailwind-styled-components';
4
+ import React, { useRef } from 'react';
5
+
6
+ /******************************************************************************
7
+ Copyright (c) Microsoft Corporation.
8
+
9
+ Permission to use, copy, modify, and/or distribute this software for any
10
+ purpose with or without fee is hereby granted.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
+ PERFORMANCE OF THIS SOFTWARE.
19
+ ***************************************************************************** */
20
+
21
+ function __rest(s, e) {
22
+ var t = {};
23
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
24
+ t[p] = s[p];
25
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
26
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
27
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
28
+ t[p[i]] = s[p[i]];
29
+ }
30
+ return t;
31
+ }
32
+
33
+ /**
34
+ * The Filter component is the base component used in the manager to filter data.
35
+ *
36
+ * - This component in generic and does not have any connection to the data.
37
+ * - The base props extends button props, and any button props such as prefix and suffix can be used.
38
+ * - Color and size has special default values in this component, but can be overridden if needed.
39
+ *
40
+ * To add items to the popover list use the children prop, this is where you build all the filter items and connected logic.
41
+ *
42
+ * @param {FilterProps} props - The props for the Filter component
43
+ * @returns {JSX.Element} Filter component
44
+ */
45
+ const Filter = (_a) => {
46
+ var { title, children, popoverProps, isActive, activeLabel, menuListProps, className, dataTestId, withStickyHeader = false } = _a, rest = __rest(_a, ["title", "children", "popoverProps", "isActive", "activeLabel", "menuListProps", "className", "dataTestId", "withStickyHeader"]);
47
+ const buttonProps = Object.assign({}, rest);
48
+ return (jsx(Popover, Object.assign({ dataTestId: `${dataTestId}-popover`, placement: "bottom-start" }, popoverProps, { render: () => (jsx(CustomMenuList, Object.assign({ withStickyHeader: withStickyHeader, dataTestId: `${dataTestId}-menulist` }, menuListProps, { children: children }))) }, { children: jsxs(CustomButton, Object.assign({ color: "tertiary", size: "small", dataTestId: dataTestId }, buttonProps, { "$isActive": isActive, className: className }, { children: [title, " ", isActive && jsx(ActiveLabel, { children: activeLabel })] })) })));
49
+ };
50
+ const CustomButton = twx(Button) `
51
+ ${({ $isActive }) => ($isActive ? tws `!border-primary-600` : "")}
52
+ `;
53
+ const CustomMenuList = twx(MenuList) `
54
+ max-h-[50vh]
55
+ overflow-x-hidden
56
+ overflow-y-auto
57
+ relative
58
+ `;
59
+ const ActiveLabel = tw.span `
60
+ text-primary-600
61
+ `;
62
+
63
+ /**
64
+ * The FilterItem component is the base component used in the manager to FilterItem data.
65
+ * This component in generic and does not have any connection to the data.
66
+ *
67
+ * @param {FilterItemProps} props - The props for the FilterItem component
68
+ * @returns {JSX.Element} FilterItem component
69
+ */
70
+ const FilterItem = (_a) => {
71
+ var { itemCount, className, prefix, dataTestId, menuItemProps } = _a, rest = __rest(_a, ["itemCount", "className", "prefix", "dataTestId", "menuItemProps"]);
72
+ const checkboxProps = Object.assign({}, rest);
73
+ const checkboxRef = useRef(null);
74
+ return (jsx(MenuItem, Object.assign({ onClick: e => {
75
+ var _a;
76
+ e.preventDefault();
77
+ (_a = checkboxRef.current) === null || _a === void 0 ? void 0 : _a.click();
78
+ }, selected: checkboxProps.checked, disabled: checkboxProps.disabled, className: `${className} overflow-hidden`, dataTestId: dataTestId, prefix: prefix }, menuItemProps, { suffix: jsx("small", { children: itemCount }) }, { children: jsx(Checkbox, Object.assign({ ref: checkboxRef }, checkboxProps, { dataTestId: `${dataTestId}-checkbox`, className: "flex-grow cursor-pointer" })) })));
79
+ };
80
+
81
+ /**
82
+ * The FilterHeader component is used to display the title of the filter and a reset button.
83
+ * IT is intended for use in the Filter component.
84
+ * The reset button will only be visible if the showReset prop is set to true.
85
+ *
86
+ * @param {FilterHeaderProps} props - The props for the FilterHeader component
87
+ * @returns {JSX.Element} FilterHeader component
88
+ */
89
+ const FilterHeader = (_a) => {
90
+ var { title, resetLabel, showReset, dataTestId, onReset, loading, children } = _a, rest = __rest(_a, ["title", "resetLabel", "showReset", "dataTestId", "onReset", "loading", "children"]);
91
+ return (jsxs(Root$1, Object.assign({}, rest, { "data-testid": dataTestId }, { children: [jsxs(HeadingContainer, { children: [jsx("h4", { children: title }), loading ? (jsx(Spinner, { size: "small" })) : (jsx(HiddenButton, Object.assign({ "$isVisible": Boolean(showReset), size: "small", variant: "transparent", color: "danger", onClick: onReset }, { children: resetLabel })))] }), children] })));
92
+ };
93
+ const HeadingContainer = tw.div `
94
+ flex
95
+ items-center
96
+ pl-1
97
+ justify-between
98
+ font-medium
99
+ text-sm
100
+ text-slate-600
101
+ uppercase
102
+ `;
103
+ const HiddenButton = twx(Button) `
104
+ ${props => (props.$isVisible ? tws `visible` : tws `invisible`)}
105
+ `;
106
+ const Root$1 = tw.div `
107
+ grid
108
+ gap-1
109
+ p-1
110
+ `;
111
+
112
+ /**
113
+ * The FilterBody component is used to display the title of the filter and a reset button.
114
+ * IT is intended for use in the Filter component.
115
+ * The reset button will only be visible if the showReset prop is set to true.
116
+ *
117
+ * @param {FilterBodyProps} props - The props for the FilterBody component
118
+ * @returns {JSX.Element} FilterBody component
119
+ */
120
+ const FilterBody = React.forwardRef((_a, ref) => {
121
+ var { children, dataTestId, limitSize = false } = _a, rest = __rest(_a, ["children", "dataTestId", "limitSize"]);
122
+ return (jsx(Root, Object.assign({ "$limitSize": limitSize, ref: ref }, rest, { "data-testid": dataTestId }, { children: children })));
123
+ });
124
+ const Root = tw.div `
125
+ grid
126
+ gap-1
127
+ p-1
128
+ rounded
129
+ ${props => props.$limitSize
130
+ ? tws `
131
+ overflow-hidden
132
+ max-h-80
133
+ max-w-sm
134
+ overflow-y-auto
135
+ `
136
+ : ""}
137
+ `;
138
+
139
+ export { Filter, FilterBody, FilterHeader, FilterItem };
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@trackunit/react-filter-components",
3
+ "version": "0.0.2",
4
+ "repository": "https://github.com/Trackunit/manager",
5
+ "license": "SEE LICENSE IN LICENSE.txt",
6
+ "dependencies": {
7
+ "@trackunit/react-components": "0.1.24",
8
+ "react": "18.2.0",
9
+ "@trackunit/tailwind-styled-components": "0.0.51",
10
+ "jest-canvas-mock": "2.4.0"
11
+ },
12
+ "module": "./index.js",
13
+ "main": "./index.js",
14
+ "type": "module",
15
+ "types": "./src/index.d.ts",
16
+ "peerDependencies": {}
17
+ }
@@ -0,0 +1,50 @@
1
+ /// <reference types="react" />
2
+ import { ButtonProps, MenuListProps, PopoverProps } from "@trackunit/react-components";
3
+ export interface FilterProps extends ButtonProps {
4
+ /**
5
+ * The title/name of the filter.
6
+ */
7
+ title: string;
8
+ /**
9
+ * Used to indicate if the user has any active selection in the filter.
10
+ */
11
+ isActive?: boolean;
12
+ /**
13
+ * The label that will be displayed when the filter is active.
14
+ */
15
+ activeLabel?: string;
16
+ /**
17
+ * When enabled the menu list padding will be removed, and a grid will be applied to keep the first column "sticky".
18
+ * This intended to be used withe the FilterHeader component.
19
+ *
20
+ * @default false
21
+ */
22
+ withStickyHeader?: boolean;
23
+ /**
24
+ * Child elements will be rendered inside the menu list and are only visible when the popover is open.
25
+ */
26
+ children: MenuListProps["children"];
27
+ /**
28
+ * Can be used to override the pops of the popover.
29
+ * The render prop cannot be overridden.
30
+ */
31
+ popoverProps?: Omit<PopoverProps, "render">;
32
+ /**
33
+ * Can be used to override the pops of the MenuList.
34
+ * The render prop cannot be overridden.
35
+ */
36
+ menuListProps?: Omit<MenuListProps, "children">;
37
+ }
38
+ /**
39
+ * The Filter component is the base component used in the manager to filter data.
40
+ *
41
+ * - This component in generic and does not have any connection to the data.
42
+ * - The base props extends button props, and any button props such as prefix and suffix can be used.
43
+ * - Color and size has special default values in this component, but can be overridden if needed.
44
+ *
45
+ * To add items to the popover list use the children prop, this is where you build all the filter items and connected logic.
46
+ *
47
+ * @param {FilterProps} props - The props for the Filter component
48
+ * @returns {JSX.Element} Filter component
49
+ */
50
+ export declare const Filter: ({ title, children, popoverProps, isActive, activeLabel, menuListProps, className, dataTestId, withStickyHeader, ...rest }: FilterProps) => JSX.Element;
@@ -0,0 +1,19 @@
1
+ import { CommonProps } from "@trackunit/react-components";
2
+ import React from "react";
3
+ export interface FilterBodyProps extends CommonProps {
4
+ /**
5
+ * Children to render in the body of the filter.
6
+ * This is intended for FilterItems.
7
+ */
8
+ children?: React.ReactNode;
9
+ limitSize?: boolean;
10
+ }
11
+ /**
12
+ * The FilterBody component is used to display the title of the filter and a reset button.
13
+ * IT is intended for use in the Filter component.
14
+ * The reset button will only be visible if the showReset prop is set to true.
15
+ *
16
+ * @param {FilterBodyProps} props - The props for the FilterBody component
17
+ * @returns {JSX.Element} FilterBody component
18
+ */
19
+ export declare const FilterBody: React.ForwardRefExoticComponent<FilterBodyProps & React.RefAttributes<HTMLDivElement>>;
@@ -0,0 +1,35 @@
1
+ /// <reference types="react" />
2
+ import { CommonProps } from "@trackunit/react-components";
3
+ export interface FilterHeaderProps extends CommonProps {
4
+ /**
5
+ * The title of the filter will be displayed in the header.
6
+ */
7
+ title: string;
8
+ /**
9
+ * Used to indicate if the user has any active selection in the filter.
10
+ */
11
+ showReset?: boolean;
12
+ /**
13
+ * The Label to display on the reset button.
14
+ */
15
+ resetLabel?: string;
16
+ loading?: boolean;
17
+ /**
18
+ * The callback that will be called when the user clicks the reset button.
19
+ */
20
+ onReset?: () => void;
21
+ /**
22
+ * Optional elements to render blow the title and reset button.
23
+ * This is intended for use with the search component.
24
+ */
25
+ children?: React.ReactNode;
26
+ }
27
+ /**
28
+ * The FilterHeader component is used to display the title of the filter and a reset button.
29
+ * IT is intended for use in the Filter component.
30
+ * The reset button will only be visible if the showReset prop is set to true.
31
+ *
32
+ * @param {FilterHeaderProps} props - The props for the FilterHeader component
33
+ * @returns {JSX.Element} FilterHeader component
34
+ */
35
+ export declare const FilterHeader: ({ title, resetLabel, showReset, dataTestId, onReset, loading, children, ...rest }: FilterHeaderProps) => JSX.Element;
@@ -0,0 +1,30 @@
1
+ /// <reference types="react" />
2
+ import { CheckboxProps, MenuItemProps } from "@trackunit/react-components";
3
+ export interface FilterItemProps extends Omit<CheckboxProps, "prefix" | "suffix"> {
4
+ /**
5
+ * The number of results for the filter item.
6
+ * This displays the number next to the label.
7
+ */
8
+ itemCount?: number;
9
+ /**
10
+ * An boolean flag to set checkbox to checked state.
11
+ */
12
+ checked?: boolean;
13
+ /**
14
+ * An boolean flag to set checkbox to disabled state.
15
+ */
16
+ disabled?: boolean;
17
+ /**
18
+ * Properties to be passed to the MenuItem component.
19
+ */
20
+ menuItemProps?: MenuItemProps;
21
+ prefix?: MenuItemProps["prefix"];
22
+ }
23
+ /**
24
+ * The FilterItem component is the base component used in the manager to FilterItem data.
25
+ * This component in generic and does not have any connection to the data.
26
+ *
27
+ * @param {FilterItemProps} props - The props for the FilterItem component
28
+ * @returns {JSX.Element} FilterItem component
29
+ */
30
+ export declare const FilterItem: ({ itemCount, className, prefix, dataTestId, menuItemProps, ...rest }: FilterItemProps) => JSX.Element;
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./Filter/Filter";
2
+ export * from "./FilterItem/FilterItem";
3
+ export * from "./FilterHeader/FilterHeader";
4
+ export * from "./FilterBody/FilterBody";