@react-fabric/data 1.0.0-beta.1 → 1.0.0-beta.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.
- package/dist/cjs/checklist/CheckList.js +134 -0
- package/dist/cjs/hooks/useSelectableList.js +54 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/table/Table.js +37 -4
- package/dist/cjs/table/useVirtualTable.js +2 -2
- package/dist/cjs/tree/TreePanel.js +14 -2
- package/dist/cjs/tree/useTree.js +7 -0
- package/dist/esm/checklist/CheckList.js +134 -0
- package/dist/esm/hooks/useSelectableList.js +54 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/table/Table.js +38 -5
- package/dist/esm/table/useVirtualTable.js +2 -2
- package/dist/esm/tree/TreePanel.js +15 -3
- package/dist/esm/tree/useTree.js +7 -0
- package/dist/types/checklist/CheckList.d.ts +30 -0
- package/dist/types/hooks/useSelectableList.d.ts +28 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/table/Table.d.ts +1 -1
- package/dist/types/table/types.d.ts +9 -4
- package/dist/types/table/useVirtualTable.d.ts +1 -1
- package/dist/types/tree/TreePanel.d.ts +1 -1
- package/dist/types/tree/types.d.ts +6 -2
- package/dist/types/tree/useTree.d.ts +1 -0
- package/package.json +5 -5
- package/src/checklist/CheckList.tsx +218 -0
- package/src/hooks/useSelectableList.ts +94 -0
- package/src/index.ts +3 -0
- package/src/table/Table.tsx +34 -4
- package/src/table/types.ts +10 -3
- package/src/table/useVirtualTable.ts +4 -4
- package/src/tree/TreePanel.tsx +12 -1
- package/src/tree/types.ts +8 -1
- package/src/tree/useTree.ts +5 -0
|
@@ -28,13 +28,13 @@ var useVirtualTable = function useVirtualTable(data, keyProperty, onCheckChanged
|
|
|
28
28
|
if (action.type === "checkall") {
|
|
29
29
|
if (state.allChecked !== 0) {
|
|
30
30
|
state.checked = [];
|
|
31
|
-
} else {
|
|
31
|
+
} else if (keyProperty) {
|
|
32
32
|
state.checked = data.map(function (item) {
|
|
33
33
|
return item[keyProperty];
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
onCheckChanged(state.checked);
|
|
37
|
+
onCheckChanged === null || onCheckChanged === void 0 || onCheckChanged(state.checked);
|
|
38
38
|
return _objectSpread(_objectSpread({}, state), {}, {
|
|
39
39
|
allChecked: state.checked.length === data.length ? 1 : state.checked.length ? 2 : 0
|
|
40
40
|
});
|
|
@@ -5,18 +5,19 @@ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProper
|
|
|
5
5
|
import { CoreIcons, Section, Header, Icon, ButtonGroup, Button } from '@react-fabric/core';
|
|
6
6
|
import { Search } from '@react-fabric/form';
|
|
7
7
|
import { useVirtualizer } from '@tanstack/react-virtual';
|
|
8
|
-
import { useRef, useMemo } from 'react';
|
|
8
|
+
import { useRef, useMemo, useImperativeHandle } from 'react';
|
|
9
9
|
import { TreeNode } from './TreeNode.js';
|
|
10
10
|
import { iconExpandAll, iconCollapseAll } from './types.js';
|
|
11
11
|
import { useTree } from './useTree.js';
|
|
12
12
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
13
13
|
|
|
14
|
-
var _excluded = ["items", "selected", "checked", "checkable", "noLines", "renderer", "searchable", "selectable", "matcher", "filterPlaceholder", "defaultLeafIcon", "defaultNodeIcon", "expander", "onChecked", "onLoad", "onSelect"];
|
|
14
|
+
var _excluded = ["ref", "items", "selected", "checked", "checkable", "noLines", "renderer", "searchable", "selectable", "matcher", "filterPlaceholder", "defaultLeafIcon", "defaultNodeIcon", "expander", "onChecked", "onLoad", "onSelect"];
|
|
15
15
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
16
16
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
17
17
|
var TreePanel = function TreePanel(_ref) {
|
|
18
18
|
var _virtualItems$;
|
|
19
|
-
var
|
|
19
|
+
var ref = _ref.ref,
|
|
20
|
+
items = _ref.items,
|
|
20
21
|
selected = _ref.selected,
|
|
21
22
|
checked = _ref.checked,
|
|
22
23
|
checkable = _ref.checkable,
|
|
@@ -48,6 +49,7 @@ var TreePanel = function TreePanel(_ref) {
|
|
|
48
49
|
expandAll = _useTree.expandAll,
|
|
49
50
|
collapseAll = _useTree.collapseAll,
|
|
50
51
|
select = _useTree.select,
|
|
52
|
+
expand = _useTree.expand,
|
|
51
53
|
onFilter = _useTree.onFilter;
|
|
52
54
|
var scrollerRef = useRef(null);
|
|
53
55
|
var virtualizer = useVirtualizer({
|
|
@@ -66,6 +68,16 @@ var TreePanel = function TreePanel(_ref) {
|
|
|
66
68
|
if (expander === "folder") return [CoreIcons.folderClosed, CoreIcons.folderOpen];
|
|
67
69
|
return [CoreIcons.expand, CoreIcons.collapse];
|
|
68
70
|
}, [expander]);
|
|
71
|
+
useImperativeHandle(ref, function () {
|
|
72
|
+
return {
|
|
73
|
+
open: function open(id) {
|
|
74
|
+
return expand(id);
|
|
75
|
+
},
|
|
76
|
+
select: function select(id) {
|
|
77
|
+
return expand(id);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}, []);
|
|
69
81
|
return /*#__PURE__*/jsxs(Section, {
|
|
70
82
|
children: [/*#__PURE__*/jsxs(Header, {
|
|
71
83
|
flex: true,
|
package/dist/esm/tree/useTree.js
CHANGED
|
@@ -151,6 +151,12 @@ var useTree = function useTree(_ref) {
|
|
|
151
151
|
id: id
|
|
152
152
|
});
|
|
153
153
|
}, []);
|
|
154
|
+
var expand = useCallback(function (id) {
|
|
155
|
+
dispatch({
|
|
156
|
+
type: "expand",
|
|
157
|
+
id: id
|
|
158
|
+
});
|
|
159
|
+
}, []);
|
|
154
160
|
var expandAll = useCallback(function () {
|
|
155
161
|
dispatch({
|
|
156
162
|
type: "expandAll"
|
|
@@ -174,6 +180,7 @@ var useTree = function useTree(_ref) {
|
|
|
174
180
|
collapseAll: collapseAll,
|
|
175
181
|
toggleCheck: toggleCheck,
|
|
176
182
|
select: select,
|
|
183
|
+
expand: expand,
|
|
177
184
|
onFilter: onFilter
|
|
178
185
|
};
|
|
179
186
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type BadgeType } from "@react-fabric/core/dist/types/types";
|
|
2
|
+
import { type FC } from "react";
|
|
3
|
+
import { type SelectableProps } from "../hooks/useSelectableList";
|
|
4
|
+
export interface CheckListItem extends KeyValue {
|
|
5
|
+
id: string;
|
|
6
|
+
label: string;
|
|
7
|
+
icon?: string;
|
|
8
|
+
badge?: BadgeType;
|
|
9
|
+
isDisabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface CheckListProps extends SelectableProps {
|
|
12
|
+
/**
|
|
13
|
+
* list items
|
|
14
|
+
*/
|
|
15
|
+
items: CheckListItem[];
|
|
16
|
+
/**
|
|
17
|
+
* allow negative selections
|
|
18
|
+
*/
|
|
19
|
+
allowNegative?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* max list items
|
|
22
|
+
*/
|
|
23
|
+
maxCount?: number;
|
|
24
|
+
/**
|
|
25
|
+
* message for empty list
|
|
26
|
+
*/
|
|
27
|
+
emptyMessage?: string;
|
|
28
|
+
sortItems?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare const CheckList: FC<CheckListProps>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface SelectableProps {
|
|
2
|
+
/**
|
|
3
|
+
* list items
|
|
4
|
+
*/
|
|
5
|
+
items: Array<{
|
|
6
|
+
label: string;
|
|
7
|
+
} & KeyValue>;
|
|
8
|
+
/**
|
|
9
|
+
* selected item ids
|
|
10
|
+
*/
|
|
11
|
+
selected?: string[];
|
|
12
|
+
/**
|
|
13
|
+
* selected negative item ids
|
|
14
|
+
*/
|
|
15
|
+
nonselected?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* change callback
|
|
18
|
+
*/
|
|
19
|
+
onChange?: (ids: string[], nonIds?: string[]) => void;
|
|
20
|
+
/**
|
|
21
|
+
* item click callback
|
|
22
|
+
*/
|
|
23
|
+
onClick?: (id: string, isNegative: boolean) => void;
|
|
24
|
+
}
|
|
25
|
+
export declare const useSelectableList: ({ items, selected, nonselected, onChange, onClick, }: SelectableProps) => {
|
|
26
|
+
selection: KeyValue<number>;
|
|
27
|
+
toggleSelection: (id: string, isNegative?: boolean) => void;
|
|
28
|
+
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import "./i18n";
|
|
2
|
+
export { CheckList } from "./checklist/CheckList";
|
|
2
3
|
export { Histogram } from "./histogram/Histogram";
|
|
3
4
|
export { JsonViewer } from "./json/JsonViewer";
|
|
4
5
|
export { Pagination } from "./pagination/Pagination";
|
|
5
6
|
export { Table } from "./table/Table";
|
|
7
|
+
export { type TableRef } from "./table/types";
|
|
6
8
|
export { TreePanel } from "./tree/TreePanel";
|
|
9
|
+
export { type TreeNodeType as TreeNode } from "./tree/types";
|
|
7
10
|
export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
|
|
8
11
|
export { VirtualList, type VirtualListRef } from "./virtual/List";
|
|
9
12
|
export * from "./hooks/useFilteredList";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type TableProps } from "./types";
|
|
2
|
-
export declare const Table: <T extends KeyValue<any> = KeyValue<any>>({ data, columns, keyProperty, checkableRows, hideableColumns, sort, loading, onSort, onRowClick, onCheckedChanged, }: TableProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare const Table: <T extends KeyValue<any> = KeyValue<any>>({ ref, data, columns, keyProperty, checkableRows, hideableColumns, sort, loading, onSort, onRowClick, onCheckedChanged, }: TableProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type CssProp, type IconProps, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
1
|
+
import { type CssProp, type IconProps, type RefProp, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
2
2
|
import { type ReactElement } from "react";
|
|
3
3
|
export declare const COL_MIN_WIDTH = "3rem";
|
|
4
4
|
export declare const COL_MAX_WIDTH = "64rem";
|
|
5
5
|
export declare const COL_DEFAULT_WIDTH = "12rem";
|
|
6
6
|
export interface ColumnType<T = KeyValue> extends CssProp, IconProps, TestProps {
|
|
7
|
-
id: keyof T;
|
|
7
|
+
id: keyof T | string;
|
|
8
8
|
label?: string;
|
|
9
9
|
actions?: ReactElement;
|
|
10
10
|
align?: "start" | "center" | "end";
|
|
@@ -22,10 +22,15 @@ export interface ColumnType<T = KeyValue> extends CssProp, IconProps, TestProps
|
|
|
22
22
|
format?: string;
|
|
23
23
|
renderer?: (value: AnyObject, data: T) => void;
|
|
24
24
|
}
|
|
25
|
-
export interface
|
|
25
|
+
export interface TableRef {
|
|
26
|
+
hilight: (row: number) => void;
|
|
27
|
+
unhilight: () => void;
|
|
28
|
+
scrollTo: (row: number) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
|
|
26
31
|
columns: Array<ColumnType<T>>;
|
|
27
32
|
data: T[];
|
|
28
|
-
keyProperty
|
|
33
|
+
keyProperty?: keyof T;
|
|
29
34
|
/**
|
|
30
35
|
* checkable rows
|
|
31
36
|
*/
|
|
@@ -3,7 +3,7 @@ interface DataState {
|
|
|
3
3
|
checked: Key[];
|
|
4
4
|
allChecked: 0 | 1 | 2;
|
|
5
5
|
}
|
|
6
|
-
export declare const useVirtualTable: (data: KeyValue[], keyProperty
|
|
6
|
+
export declare const useVirtualTable: (data: KeyValue[], keyProperty?: Key, onCheckChanged?: (checked: Key[]) => void) => {
|
|
7
7
|
scrollerRef: import("react").RefObject<HTMLDivElement>;
|
|
8
8
|
items: import("@tanstack/virtual-core").VirtualItem<Element>[];
|
|
9
9
|
checkState: DataState;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type TreePanelProps } from "./types";
|
|
2
|
-
export declare const TreePanel: <T extends KeyValue<any>>({ items, selected, checked, checkable, noLines, renderer, searchable, selectable, matcher, filterPlaceholder, defaultLeafIcon, defaultNodeIcon, expander, onChecked, onLoad, onSelect, ...aria }: TreePanelProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare const TreePanel: <T extends KeyValue<any>>({ ref, items, selected, checked, checkable, noLines, renderer, searchable, selectable, matcher, filterPlaceholder, defaultLeafIcon, defaultNodeIcon, expander, onChecked, onLoad, onSelect, ...aria }: TreePanelProps<T>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { type BadgeType, type ChildProp, type CssProp, type Elements, type IconProps, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
2
|
+
import { type BadgeType, type ChildProp, type CssProp, type Elements, type IconProps, type RefProp, type TestProps } from "@react-fabric/core/dist/types/types";
|
|
3
3
|
interface TreeBaseNode<T> extends IconProps, CssProp, TestProps {
|
|
4
4
|
id: string;
|
|
5
5
|
/**
|
|
@@ -70,7 +70,11 @@ export interface TreeNodeProps extends ChildProp {
|
|
|
70
70
|
onSelect: (id: string) => void;
|
|
71
71
|
onChecked: (id: string) => void;
|
|
72
72
|
}
|
|
73
|
-
export interface
|
|
73
|
+
export interface TreeRef {
|
|
74
|
+
select: (id: string) => void;
|
|
75
|
+
open: (id: string) => void;
|
|
76
|
+
}
|
|
77
|
+
export interface TreePanelProps<T extends KeyValue = KeyValue> extends TestProps, RefProp<TreeRef> {
|
|
74
78
|
items?: Array<TreeNodeType<T>>;
|
|
75
79
|
/**
|
|
76
80
|
* make tree searchable
|
|
@@ -13,6 +13,7 @@ export declare const useTree: <T extends KeyValue<any>>({ items, checked, select
|
|
|
13
13
|
collapseAll: () => void;
|
|
14
14
|
toggleCheck: (id: string) => void;
|
|
15
15
|
select: (id: string) => void;
|
|
16
|
+
expand: (id: string) => void;
|
|
16
17
|
onFilter: {
|
|
17
18
|
(...args: any): void;
|
|
18
19
|
cancel(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-fabric/data",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"description": "RUF: Data package",
|
|
5
5
|
"author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"watch": "npm-run-all -p watch:rollup watch:dts"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@react-fabric/core": "^1.0.0-beta.
|
|
37
|
-
"@react-fabric/form": "^1.0.0-beta.
|
|
38
|
-
"@react-fabric/utilities": "^1.0.0-beta.
|
|
36
|
+
"@react-fabric/core": "^1.0.0-beta.4",
|
|
37
|
+
"@react-fabric/form": "^1.0.0-beta.4",
|
|
38
|
+
"@react-fabric/utilities": "^1.0.0-beta.4",
|
|
39
39
|
"@tanstack/react-virtual": "^3.8.6",
|
|
40
40
|
"classnames": "^2.5.1",
|
|
41
41
|
"color": "^4.2.3",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"node": ">=18.12.0",
|
|
52
52
|
"npm": ">=8.19.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "47b0aede19252a3a3ef8df75c08a21caabde8100"
|
|
55
55
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Badge, CoreIcons, EmptyContent, Icon } from "@react-fabric/core";
|
|
25
|
+
import { type BadgeType } from "@react-fabric/core/dist/types/types";
|
|
26
|
+
import { isString } from "@react-fabric/utilities";
|
|
27
|
+
import {
|
|
28
|
+
Fragment,
|
|
29
|
+
isValidElement,
|
|
30
|
+
memo,
|
|
31
|
+
useEffect,
|
|
32
|
+
useMemo,
|
|
33
|
+
useState,
|
|
34
|
+
type FC,
|
|
35
|
+
} from "react";
|
|
36
|
+
import { useTranslation } from "react-i18next";
|
|
37
|
+
import {
|
|
38
|
+
useSelectableList,
|
|
39
|
+
type SelectableProps,
|
|
40
|
+
} from "../hooks/useSelectableList";
|
|
41
|
+
|
|
42
|
+
export interface CheckListItem extends KeyValue {
|
|
43
|
+
id: string;
|
|
44
|
+
label: string;
|
|
45
|
+
icon?: string;
|
|
46
|
+
badge?: BadgeType;
|
|
47
|
+
isDisabled?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface CheckListProps extends SelectableProps {
|
|
51
|
+
/**
|
|
52
|
+
* list items
|
|
53
|
+
*/
|
|
54
|
+
items: CheckListItem[];
|
|
55
|
+
/**
|
|
56
|
+
* allow negative selections
|
|
57
|
+
*/
|
|
58
|
+
allowNegative?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* max list items
|
|
61
|
+
*/
|
|
62
|
+
maxCount?: number;
|
|
63
|
+
/**
|
|
64
|
+
* message for empty list
|
|
65
|
+
*/
|
|
66
|
+
emptyMessage?: string;
|
|
67
|
+
|
|
68
|
+
sortItems?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const CheckItem: FC<
|
|
72
|
+
Omit<CheckListItem, "selected"> & {
|
|
73
|
+
selected?: number;
|
|
74
|
+
allowNegative?: boolean;
|
|
75
|
+
}
|
|
76
|
+
> = memo(
|
|
77
|
+
({
|
|
78
|
+
id,
|
|
79
|
+
allowNegative,
|
|
80
|
+
selected,
|
|
81
|
+
icon,
|
|
82
|
+
label,
|
|
83
|
+
isDisabled,
|
|
84
|
+
onClick,
|
|
85
|
+
badge,
|
|
86
|
+
}: Omit<CheckListItem, "selected"> & {
|
|
87
|
+
selected?: number;
|
|
88
|
+
allowNegative?: boolean;
|
|
89
|
+
}) => {
|
|
90
|
+
return (
|
|
91
|
+
<div
|
|
92
|
+
role="none"
|
|
93
|
+
className="ax-checkList__item"
|
|
94
|
+
data-disabled={isDisabled}
|
|
95
|
+
onClick={() => onClick(id, false)}
|
|
96
|
+
onContextMenu={(e) => [
|
|
97
|
+
allowNegative && onClick(id, true),
|
|
98
|
+
e.preventDefault(),
|
|
99
|
+
]}
|
|
100
|
+
>
|
|
101
|
+
{!allowNegative && (
|
|
102
|
+
<Icon
|
|
103
|
+
data-type="checkbox"
|
|
104
|
+
className="ax-checkList__checkbox"
|
|
105
|
+
data-selected={selected === 1}
|
|
106
|
+
icon={selected ? CoreIcons.checkboxInt : CoreIcons.checkboxOff}
|
|
107
|
+
/>
|
|
108
|
+
)}
|
|
109
|
+
{allowNegative && (
|
|
110
|
+
<Fragment>
|
|
111
|
+
<Icon
|
|
112
|
+
data-type="multiple"
|
|
113
|
+
className="ax-checkList__checkbox"
|
|
114
|
+
icon={
|
|
115
|
+
selected === 1
|
|
116
|
+
? CoreIcons.expand
|
|
117
|
+
: selected === -1
|
|
118
|
+
? CoreIcons.collapse
|
|
119
|
+
: CoreIcons.checkboxOff
|
|
120
|
+
}
|
|
121
|
+
data-selected={selected}
|
|
122
|
+
/>
|
|
123
|
+
<div className="ax-checkList__checkbox">
|
|
124
|
+
<Icon
|
|
125
|
+
data-type="positive"
|
|
126
|
+
onClick={(e) => [(onClick(id, false), e.stopPropagation())]}
|
|
127
|
+
icon={
|
|
128
|
+
selected === 1 ? CoreIcons.expandActive : CoreIcons.expand
|
|
129
|
+
}
|
|
130
|
+
data-selected={selected === 1}
|
|
131
|
+
/>
|
|
132
|
+
<Icon
|
|
133
|
+
data-type="negative"
|
|
134
|
+
onClick={(e) => [(onClick(id, true), e.stopPropagation())]}
|
|
135
|
+
icon={
|
|
136
|
+
selected === -1
|
|
137
|
+
? CoreIcons.collapseActive
|
|
138
|
+
: CoreIcons.collapse
|
|
139
|
+
}
|
|
140
|
+
data-selected={selected === -1}
|
|
141
|
+
/>
|
|
142
|
+
</div>
|
|
143
|
+
</Fragment>
|
|
144
|
+
)}
|
|
145
|
+
{isValidElement(icon) && icon}
|
|
146
|
+
{isString(icon) && <Icon icon={icon} />}
|
|
147
|
+
<label>{label}</label>
|
|
148
|
+
<Badge {...badge} />
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
CheckItem.displayName = "CheckItem";
|
|
154
|
+
|
|
155
|
+
export const CheckList: FC<CheckListProps> = ({
|
|
156
|
+
items = [],
|
|
157
|
+
maxCount = 0,
|
|
158
|
+
allowNegative,
|
|
159
|
+
emptyMessage,
|
|
160
|
+
sortItems = true,
|
|
161
|
+
...props
|
|
162
|
+
}: CheckListProps) => {
|
|
163
|
+
const { t } = useTranslation("data");
|
|
164
|
+
const { selection, toggleSelection } = useSelectableList({
|
|
165
|
+
items,
|
|
166
|
+
...props,
|
|
167
|
+
});
|
|
168
|
+
const [showMore, setShowMore] = useState(false);
|
|
169
|
+
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
setShowMore(false);
|
|
172
|
+
}, [items]);
|
|
173
|
+
|
|
174
|
+
const listItems = useMemo(() => {
|
|
175
|
+
return [...items]
|
|
176
|
+
.sort((a, b) => {
|
|
177
|
+
if (sortItems && selection[a.id] !== selection[b.id]) {
|
|
178
|
+
if (selection[a.id] === 1) return -1;
|
|
179
|
+
if (selection[b.id] === 1) return 1;
|
|
180
|
+
if (selection[a.id] === -1) return -1;
|
|
181
|
+
if (selection[b.id] === -1) return 1;
|
|
182
|
+
}
|
|
183
|
+
return 0;
|
|
184
|
+
})
|
|
185
|
+
.slice(0, !showMore && maxCount > 0 ? maxCount : undefined);
|
|
186
|
+
}, [items, selection, maxCount, showMore, sortItems]);
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<div className="ax-checkList">
|
|
190
|
+
{listItems.map((item, index) => (
|
|
191
|
+
<CheckItem
|
|
192
|
+
{...item}
|
|
193
|
+
key={index}
|
|
194
|
+
onClick={toggleSelection}
|
|
195
|
+
allowNegative={allowNegative}
|
|
196
|
+
selected={selection[item.id]}
|
|
197
|
+
/>
|
|
198
|
+
))}
|
|
199
|
+
{maxCount > 0 && items.length > maxCount && (
|
|
200
|
+
<div className="ax-moreLink">
|
|
201
|
+
<span
|
|
202
|
+
role="none"
|
|
203
|
+
className="link"
|
|
204
|
+
onClick={() => setShowMore(!showMore)}
|
|
205
|
+
>
|
|
206
|
+
...{t(`core:action.${showMore ? "less" : "more"}`)}
|
|
207
|
+
</span>
|
|
208
|
+
</div>
|
|
209
|
+
)}
|
|
210
|
+
{listItems.length === 0 && (
|
|
211
|
+
<EmptyContent
|
|
212
|
+
className="text-sm"
|
|
213
|
+
message={emptyMessage ?? t("checkList.empty")}
|
|
214
|
+
/>
|
|
215
|
+
)}
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { useCallback, useEffect, useRef, useState, useTransition } from "react";
|
|
25
|
+
|
|
26
|
+
export interface SelectableProps {
|
|
27
|
+
/**
|
|
28
|
+
* list items
|
|
29
|
+
*/
|
|
30
|
+
items: Array<{ label: string } & KeyValue>;
|
|
31
|
+
/**
|
|
32
|
+
* selected item ids
|
|
33
|
+
*/
|
|
34
|
+
selected?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* selected negative item ids
|
|
37
|
+
*/
|
|
38
|
+
nonselected?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* change callback
|
|
41
|
+
*/
|
|
42
|
+
onChange?: (ids: string[], nonIds?: string[]) => void;
|
|
43
|
+
/**
|
|
44
|
+
* item click callback
|
|
45
|
+
*/
|
|
46
|
+
onClick?: (id: string, isNegative: boolean) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const useSelectableList = ({
|
|
50
|
+
items,
|
|
51
|
+
selected,
|
|
52
|
+
nonselected,
|
|
53
|
+
onChange,
|
|
54
|
+
onClick,
|
|
55
|
+
}: SelectableProps) => {
|
|
56
|
+
const selectionRef = useRef<KeyValue<number>>({});
|
|
57
|
+
const [selection, setSelection] = useState<KeyValue<number>>({});
|
|
58
|
+
const [, startTransition] = useTransition();
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
selectionRef.current = items.reduce(
|
|
61
|
+
(ret, { id }) => ({
|
|
62
|
+
...ret,
|
|
63
|
+
[id]: selected?.includes?.(id)
|
|
64
|
+
? 1
|
|
65
|
+
: nonselected?.includes?.(id)
|
|
66
|
+
? -1
|
|
67
|
+
: 0,
|
|
68
|
+
}),
|
|
69
|
+
{},
|
|
70
|
+
);
|
|
71
|
+
setSelection({ ...selectionRef.current });
|
|
72
|
+
}, [items, selected, nonselected]);
|
|
73
|
+
|
|
74
|
+
const toggleSelection = useCallback((id: string, isNegative = false) => {
|
|
75
|
+
const check = isNegative ? -1 : 1;
|
|
76
|
+
if (selectionRef.current[id] !== check) selectionRef.current[id] = check;
|
|
77
|
+
else if (selectionRef.current[id] === check) selectionRef.current[id] = 0;
|
|
78
|
+
const change: string[][] = [[], []];
|
|
79
|
+
Object.entries(selectionRef.current).forEach(([id, select]) =>
|
|
80
|
+
select === 1
|
|
81
|
+
? change[0].push(id)
|
|
82
|
+
: select === -1
|
|
83
|
+
? change[1].push(id)
|
|
84
|
+
: undefined,
|
|
85
|
+
);
|
|
86
|
+
setSelection({ ...selectionRef.current });
|
|
87
|
+
startTransition(() => {
|
|
88
|
+
onClick?.(id, isNegative);
|
|
89
|
+
onChange?.(change[0], change[1]);
|
|
90
|
+
});
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
return { selection, toggleSelection };
|
|
94
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -23,11 +23,14 @@
|
|
|
23
23
|
|
|
24
24
|
import "./i18n";
|
|
25
25
|
|
|
26
|
+
export { CheckList } from "./checklist/CheckList";
|
|
26
27
|
export { Histogram } from "./histogram/Histogram";
|
|
27
28
|
export { JsonViewer } from "./json/JsonViewer";
|
|
28
29
|
export { Pagination } from "./pagination/Pagination";
|
|
29
30
|
export { Table } from "./table/Table";
|
|
31
|
+
export { type TableRef } from "./table/types";
|
|
30
32
|
export { TreePanel } from "./tree/TreePanel";
|
|
33
|
+
export { type TreeNodeType as TreeNode } from "./tree/types";
|
|
31
34
|
export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
|
|
32
35
|
export { VirtualList, type VirtualListRef } from "./virtual/List";
|
|
33
36
|
|