@react-fabric/data 1.0.0-beta.3 → 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.
@@ -0,0 +1,134 @@
1
+ 'use strict';
2
+
3
+ var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
4
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
5
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
6
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
7
+ var core = require('@react-fabric/core');
8
+ var utilities = require('@react-fabric/utilities');
9
+ var react = require('react');
10
+ var reactI18next = require('react-i18next');
11
+ var useSelectableList = require('../hooks/useSelectableList.js');
12
+ var jsxRuntime = require('react/jsx-runtime');
13
+
14
+ var _excluded = ["items", "maxCount", "allowNegative", "emptyMessage", "sortItems"];
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
+ 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
+ var CheckItem = /*#__PURE__*/react.memo(function (_ref) {
18
+ var id = _ref.id,
19
+ allowNegative = _ref.allowNegative,
20
+ selected = _ref.selected,
21
+ icon = _ref.icon,
22
+ label = _ref.label,
23
+ isDisabled = _ref.isDisabled,
24
+ _onClick = _ref.onClick,
25
+ badge = _ref.badge;
26
+ return /*#__PURE__*/jsxRuntime.jsxs("div", {
27
+ role: "none",
28
+ className: "ax-checkList__item",
29
+ "data-disabled": isDisabled,
30
+ onClick: function onClick() {
31
+ return _onClick(id, false);
32
+ },
33
+ onContextMenu: function onContextMenu(e) {
34
+ return [allowNegative && _onClick(id, true), e.preventDefault()];
35
+ },
36
+ children: [!allowNegative && /*#__PURE__*/jsxRuntime.jsx(core.Icon, {
37
+ "data-type": "checkbox",
38
+ className: "ax-checkList__checkbox",
39
+ "data-selected": selected === 1,
40
+ icon: selected ? core.CoreIcons.checkboxInt : core.CoreIcons.checkboxOff
41
+ }), allowNegative && /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
42
+ children: [/*#__PURE__*/jsxRuntime.jsx(core.Icon, {
43
+ "data-type": "multiple",
44
+ className: "ax-checkList__checkbox",
45
+ icon: selected === 1 ? core.CoreIcons.expand : selected === -1 ? core.CoreIcons.collapse : core.CoreIcons.checkboxOff,
46
+ "data-selected": selected
47
+ }), /*#__PURE__*/jsxRuntime.jsxs("div", {
48
+ className: "ax-checkList__checkbox",
49
+ children: [/*#__PURE__*/jsxRuntime.jsx(core.Icon, {
50
+ "data-type": "positive",
51
+ onClick: function onClick(e) {
52
+ return [(_onClick(id, false), e.stopPropagation())];
53
+ },
54
+ icon: selected === 1 ? core.CoreIcons.expandActive : core.CoreIcons.expand,
55
+ "data-selected": selected === 1
56
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Icon, {
57
+ "data-type": "negative",
58
+ onClick: function onClick(e) {
59
+ return [(_onClick(id, true), e.stopPropagation())];
60
+ },
61
+ icon: selected === -1 ? core.CoreIcons.collapseActive : core.CoreIcons.collapse,
62
+ "data-selected": selected === -1
63
+ })]
64
+ })]
65
+ }), /*#__PURE__*/react.isValidElement(icon) && icon, utilities.isString(icon) && /*#__PURE__*/jsxRuntime.jsx(core.Icon, {
66
+ icon: icon
67
+ }), /*#__PURE__*/jsxRuntime.jsx("label", {
68
+ children: label
69
+ }), /*#__PURE__*/jsxRuntime.jsx(core.Badge, _objectSpread({}, badge))]
70
+ });
71
+ });
72
+ CheckItem.displayName = "CheckItem";
73
+ var CheckList = function CheckList(_ref2) {
74
+ var _ref2$items = _ref2.items,
75
+ items = _ref2$items === void 0 ? [] : _ref2$items,
76
+ _ref2$maxCount = _ref2.maxCount,
77
+ maxCount = _ref2$maxCount === void 0 ? 0 : _ref2$maxCount,
78
+ allowNegative = _ref2.allowNegative,
79
+ emptyMessage = _ref2.emptyMessage,
80
+ _ref2$sortItems = _ref2.sortItems,
81
+ sortItems = _ref2$sortItems === void 0 ? true : _ref2$sortItems,
82
+ props = _objectWithoutProperties(_ref2, _excluded);
83
+ var _useTranslation = reactI18next.useTranslation("data"),
84
+ t = _useTranslation.t;
85
+ var _useSelectableList = useSelectableList.useSelectableList(_objectSpread({
86
+ items: items
87
+ }, props)),
88
+ selection = _useSelectableList.selection,
89
+ toggleSelection = _useSelectableList.toggleSelection;
90
+ var _useState = react.useState(false),
91
+ _useState2 = _slicedToArray(_useState, 2),
92
+ showMore = _useState2[0],
93
+ setShowMore = _useState2[1];
94
+ react.useEffect(function () {
95
+ setShowMore(false);
96
+ }, [items]);
97
+ var listItems = react.useMemo(function () {
98
+ return _toConsumableArray(items).sort(function (a, b) {
99
+ if (sortItems && selection[a.id] !== selection[b.id]) {
100
+ if (selection[a.id] === 1) return -1;
101
+ if (selection[b.id] === 1) return 1;
102
+ if (selection[a.id] === -1) return -1;
103
+ if (selection[b.id] === -1) return 1;
104
+ }
105
+ return 0;
106
+ }).slice(0, !showMore && maxCount > 0 ? maxCount : undefined);
107
+ }, [items, selection, maxCount, showMore, sortItems]);
108
+ return /*#__PURE__*/jsxRuntime.jsxs("div", {
109
+ className: "ax-checkList",
110
+ children: [listItems.map(function (item, index) {
111
+ return /*#__PURE__*/react.createElement(CheckItem, _objectSpread(_objectSpread({}, item), {}, {
112
+ key: index,
113
+ onClick: toggleSelection,
114
+ allowNegative: allowNegative,
115
+ selected: selection[item.id]
116
+ }));
117
+ }), maxCount > 0 && items.length > maxCount && /*#__PURE__*/jsxRuntime.jsx("div", {
118
+ className: "ax-moreLink",
119
+ children: /*#__PURE__*/jsxRuntime.jsxs("span", {
120
+ role: "none",
121
+ className: "link",
122
+ onClick: function onClick() {
123
+ return setShowMore(!showMore);
124
+ },
125
+ children: ["...", t("core:action.".concat(showMore ? "less" : "more"))]
126
+ })
127
+ }), listItems.length === 0 && /*#__PURE__*/jsxRuntime.jsx(core.EmptyContent, {
128
+ className: "text-sm",
129
+ message: emptyMessage !== null && emptyMessage !== void 0 ? emptyMessage : t("checkList.empty")
130
+ })]
131
+ });
132
+ };
133
+
134
+ exports.CheckList = CheckList;
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
5
+ var react = require('react');
6
+
7
+ 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; }
8
+ 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; }
9
+ var useSelectableList = function useSelectableList(_ref) {
10
+ var items = _ref.items,
11
+ selected = _ref.selected,
12
+ nonselected = _ref.nonselected,
13
+ onChange = _ref.onChange,
14
+ onClick = _ref.onClick;
15
+ var selectionRef = react.useRef({});
16
+ var _useState = react.useState({}),
17
+ _useState2 = _slicedToArray(_useState, 2),
18
+ selection = _useState2[0],
19
+ setSelection = _useState2[1];
20
+ var _useTransition = react.useTransition(),
21
+ _useTransition2 = _slicedToArray(_useTransition, 2),
22
+ startTransition = _useTransition2[1];
23
+ react.useEffect(function () {
24
+ selectionRef.current = items.reduce(function (ret, _ref2) {
25
+ var _selected$includes, _nonselected$includes;
26
+ var id = _ref2.id;
27
+ return _objectSpread(_objectSpread({}, ret), {}, _defineProperty({}, id, selected !== null && selected !== void 0 && (_selected$includes = selected.includes) !== null && _selected$includes !== void 0 && _selected$includes.call(selected, id) ? 1 : nonselected !== null && nonselected !== void 0 && (_nonselected$includes = nonselected.includes) !== null && _nonselected$includes !== void 0 && _nonselected$includes.call(nonselected, id) ? -1 : 0));
28
+ }, {});
29
+ setSelection(_objectSpread({}, selectionRef.current));
30
+ }, [items, selected, nonselected]);
31
+ var toggleSelection = react.useCallback(function (id) {
32
+ var isNegative = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
33
+ var check = isNegative ? -1 : 1;
34
+ if (selectionRef.current[id] !== check) selectionRef.current[id] = check;else if (selectionRef.current[id] === check) selectionRef.current[id] = 0;
35
+ var change = [[], []];
36
+ Object.entries(selectionRef.current).forEach(function (_ref3) {
37
+ var _ref4 = _slicedToArray(_ref3, 2),
38
+ id = _ref4[0],
39
+ select = _ref4[1];
40
+ return select === 1 ? change[0].push(id) : select === -1 ? change[1].push(id) : undefined;
41
+ });
42
+ setSelection(_objectSpread({}, selectionRef.current));
43
+ startTransition(function () {
44
+ onClick === null || onClick === void 0 || onClick(id, isNegative);
45
+ onChange === null || onChange === void 0 || onChange(change[0], change[1]);
46
+ });
47
+ }, []);
48
+ return {
49
+ selection: selection,
50
+ toggleSelection: toggleSelection
51
+ };
52
+ };
53
+
54
+ exports.useSelectableList = useSelectableList;
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  require('./i18n/index.js');
4
+ var CheckList = require('./checklist/CheckList.js');
4
5
  var Histogram = require('./histogram/Histogram.js');
5
6
  var JsonViewer = require('./json/JsonViewer.js');
6
7
  var Pagination = require('./pagination/Pagination.js');
@@ -13,6 +14,7 @@ var usePagination = require('./hooks/usePagination.js');
13
14
 
14
15
 
15
16
 
17
+ exports.CheckList = CheckList.CheckList;
16
18
  exports.Histogram = Histogram.Histogram;
17
19
  exports.JsonViewer = JsonViewer.JsonViewer;
18
20
  exports.Pagination = Pagination.Pagination;
@@ -142,13 +142,13 @@ var Table = function Table(_ref) {
142
142
  "data-row": index,
143
143
  className: classNames("flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500", index % 2 ? "bg-even" : "bg-odd", onRowClick && "hover:bg-primary-50 active:bg-primary-100"),
144
144
  onClick: function onClick() {
145
- return checkableRows && !onRowClick ? toggleChecked(data[keyProperty]) : onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(data);
145
+ return checkableRows && keyProperty && !onRowClick ? toggleChecked(data[keyProperty]) : onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(data);
146
146
  },
147
147
  "data-index": index,
148
148
  ref: measureElement,
149
149
  children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
150
150
  className: wrapperStart,
151
- children: [checkableRows && /*#__PURE__*/jsxRuntime.jsx(CheckboxCell.CheckboxCell, {
151
+ children: [checkableRows && keyProperty && /*#__PURE__*/jsxRuntime.jsx(CheckboxCell.CheckboxCell, {
152
152
  onClick: function onClick() {
153
153
  return toggleChecked(data[keyProperty]);
154
154
  },
@@ -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
  });
@@ -0,0 +1,134 @@
1
+ 'use client';
2
+
3
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
5
+ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
6
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
7
+ import { Icon, CoreIcons, Badge, EmptyContent } from '@react-fabric/core';
8
+ import { isString } from '@react-fabric/utilities';
9
+ import { memo, Fragment, isValidElement, useState, useEffect, useMemo, createElement } from 'react';
10
+ import { useTranslation } from 'react-i18next';
11
+ import { useSelectableList } from '../hooks/useSelectableList.js';
12
+ import { jsxs, jsx } from 'react/jsx-runtime';
13
+
14
+ var _excluded = ["items", "maxCount", "allowNegative", "emptyMessage", "sortItems"];
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
+ 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
+ var CheckItem = /*#__PURE__*/memo(function (_ref) {
18
+ var id = _ref.id,
19
+ allowNegative = _ref.allowNegative,
20
+ selected = _ref.selected,
21
+ icon = _ref.icon,
22
+ label = _ref.label,
23
+ isDisabled = _ref.isDisabled,
24
+ _onClick = _ref.onClick,
25
+ badge = _ref.badge;
26
+ return /*#__PURE__*/jsxs("div", {
27
+ role: "none",
28
+ className: "ax-checkList__item",
29
+ "data-disabled": isDisabled,
30
+ onClick: function onClick() {
31
+ return _onClick(id, false);
32
+ },
33
+ onContextMenu: function onContextMenu(e) {
34
+ return [allowNegative && _onClick(id, true), e.preventDefault()];
35
+ },
36
+ children: [!allowNegative && /*#__PURE__*/jsx(Icon, {
37
+ "data-type": "checkbox",
38
+ className: "ax-checkList__checkbox",
39
+ "data-selected": selected === 1,
40
+ icon: selected ? CoreIcons.checkboxInt : CoreIcons.checkboxOff
41
+ }), allowNegative && /*#__PURE__*/jsxs(Fragment, {
42
+ children: [/*#__PURE__*/jsx(Icon, {
43
+ "data-type": "multiple",
44
+ className: "ax-checkList__checkbox",
45
+ icon: selected === 1 ? CoreIcons.expand : selected === -1 ? CoreIcons.collapse : CoreIcons.checkboxOff,
46
+ "data-selected": selected
47
+ }), /*#__PURE__*/jsxs("div", {
48
+ className: "ax-checkList__checkbox",
49
+ children: [/*#__PURE__*/jsx(Icon, {
50
+ "data-type": "positive",
51
+ onClick: function onClick(e) {
52
+ return [(_onClick(id, false), e.stopPropagation())];
53
+ },
54
+ icon: selected === 1 ? CoreIcons.expandActive : CoreIcons.expand,
55
+ "data-selected": selected === 1
56
+ }), /*#__PURE__*/jsx(Icon, {
57
+ "data-type": "negative",
58
+ onClick: function onClick(e) {
59
+ return [(_onClick(id, true), e.stopPropagation())];
60
+ },
61
+ icon: selected === -1 ? CoreIcons.collapseActive : CoreIcons.collapse,
62
+ "data-selected": selected === -1
63
+ })]
64
+ })]
65
+ }), /*#__PURE__*/isValidElement(icon) && icon, isString(icon) && /*#__PURE__*/jsx(Icon, {
66
+ icon: icon
67
+ }), /*#__PURE__*/jsx("label", {
68
+ children: label
69
+ }), /*#__PURE__*/jsx(Badge, _objectSpread({}, badge))]
70
+ });
71
+ });
72
+ CheckItem.displayName = "CheckItem";
73
+ var CheckList = function CheckList(_ref2) {
74
+ var _ref2$items = _ref2.items,
75
+ items = _ref2$items === void 0 ? [] : _ref2$items,
76
+ _ref2$maxCount = _ref2.maxCount,
77
+ maxCount = _ref2$maxCount === void 0 ? 0 : _ref2$maxCount,
78
+ allowNegative = _ref2.allowNegative,
79
+ emptyMessage = _ref2.emptyMessage,
80
+ _ref2$sortItems = _ref2.sortItems,
81
+ sortItems = _ref2$sortItems === void 0 ? true : _ref2$sortItems,
82
+ props = _objectWithoutProperties(_ref2, _excluded);
83
+ var _useTranslation = useTranslation("data"),
84
+ t = _useTranslation.t;
85
+ var _useSelectableList = useSelectableList(_objectSpread({
86
+ items: items
87
+ }, props)),
88
+ selection = _useSelectableList.selection,
89
+ toggleSelection = _useSelectableList.toggleSelection;
90
+ var _useState = useState(false),
91
+ _useState2 = _slicedToArray(_useState, 2),
92
+ showMore = _useState2[0],
93
+ setShowMore = _useState2[1];
94
+ useEffect(function () {
95
+ setShowMore(false);
96
+ }, [items]);
97
+ var listItems = useMemo(function () {
98
+ return _toConsumableArray(items).sort(function (a, b) {
99
+ if (sortItems && selection[a.id] !== selection[b.id]) {
100
+ if (selection[a.id] === 1) return -1;
101
+ if (selection[b.id] === 1) return 1;
102
+ if (selection[a.id] === -1) return -1;
103
+ if (selection[b.id] === -1) return 1;
104
+ }
105
+ return 0;
106
+ }).slice(0, !showMore && maxCount > 0 ? maxCount : undefined);
107
+ }, [items, selection, maxCount, showMore, sortItems]);
108
+ return /*#__PURE__*/jsxs("div", {
109
+ className: "ax-checkList",
110
+ children: [listItems.map(function (item, index) {
111
+ return /*#__PURE__*/createElement(CheckItem, _objectSpread(_objectSpread({}, item), {}, {
112
+ key: index,
113
+ onClick: toggleSelection,
114
+ allowNegative: allowNegative,
115
+ selected: selection[item.id]
116
+ }));
117
+ }), maxCount > 0 && items.length > maxCount && /*#__PURE__*/jsx("div", {
118
+ className: "ax-moreLink",
119
+ children: /*#__PURE__*/jsxs("span", {
120
+ role: "none",
121
+ className: "link",
122
+ onClick: function onClick() {
123
+ return setShowMore(!showMore);
124
+ },
125
+ children: ["...", t("core:action.".concat(showMore ? "less" : "more"))]
126
+ })
127
+ }), listItems.length === 0 && /*#__PURE__*/jsx(EmptyContent, {
128
+ className: "text-sm",
129
+ message: emptyMessage !== null && emptyMessage !== void 0 ? emptyMessage : t("checkList.empty")
130
+ })]
131
+ });
132
+ };
133
+
134
+ export { CheckList };
@@ -0,0 +1,54 @@
1
+ 'use client';
2
+
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
5
+ import { useRef, useState, useTransition, useEffect, useCallback } from 'react';
6
+
7
+ 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; }
8
+ 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; }
9
+ var useSelectableList = function useSelectableList(_ref) {
10
+ var items = _ref.items,
11
+ selected = _ref.selected,
12
+ nonselected = _ref.nonselected,
13
+ onChange = _ref.onChange,
14
+ onClick = _ref.onClick;
15
+ var selectionRef = useRef({});
16
+ var _useState = useState({}),
17
+ _useState2 = _slicedToArray(_useState, 2),
18
+ selection = _useState2[0],
19
+ setSelection = _useState2[1];
20
+ var _useTransition = useTransition(),
21
+ _useTransition2 = _slicedToArray(_useTransition, 2),
22
+ startTransition = _useTransition2[1];
23
+ useEffect(function () {
24
+ selectionRef.current = items.reduce(function (ret, _ref2) {
25
+ var _selected$includes, _nonselected$includes;
26
+ var id = _ref2.id;
27
+ return _objectSpread(_objectSpread({}, ret), {}, _defineProperty({}, id, selected !== null && selected !== void 0 && (_selected$includes = selected.includes) !== null && _selected$includes !== void 0 && _selected$includes.call(selected, id) ? 1 : nonselected !== null && nonselected !== void 0 && (_nonselected$includes = nonselected.includes) !== null && _nonselected$includes !== void 0 && _nonselected$includes.call(nonselected, id) ? -1 : 0));
28
+ }, {});
29
+ setSelection(_objectSpread({}, selectionRef.current));
30
+ }, [items, selected, nonselected]);
31
+ var toggleSelection = useCallback(function (id) {
32
+ var isNegative = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
33
+ var check = isNegative ? -1 : 1;
34
+ if (selectionRef.current[id] !== check) selectionRef.current[id] = check;else if (selectionRef.current[id] === check) selectionRef.current[id] = 0;
35
+ var change = [[], []];
36
+ Object.entries(selectionRef.current).forEach(function (_ref3) {
37
+ var _ref4 = _slicedToArray(_ref3, 2),
38
+ id = _ref4[0],
39
+ select = _ref4[1];
40
+ return select === 1 ? change[0].push(id) : select === -1 ? change[1].push(id) : undefined;
41
+ });
42
+ setSelection(_objectSpread({}, selectionRef.current));
43
+ startTransition(function () {
44
+ onClick === null || onClick === void 0 || onClick(id, isNegative);
45
+ onChange === null || onChange === void 0 || onChange(change[0], change[1]);
46
+ });
47
+ }, []);
48
+ return {
49
+ selection: selection,
50
+ toggleSelection: toggleSelection
51
+ };
52
+ };
53
+
54
+ export { useSelectableList };
package/dist/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  import './i18n/index.js';
4
+ export { CheckList } from './checklist/CheckList.js';
4
5
  export { Histogram } from './histogram/Histogram.js';
5
6
  export { JsonViewer } from './json/JsonViewer.js';
6
7
  export { Pagination } from './pagination/Pagination.js';
@@ -142,13 +142,13 @@ var Table = function Table(_ref) {
142
142
  "data-row": index,
143
143
  className: classNames("flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500", index % 2 ? "bg-even" : "bg-odd", onRowClick && "hover:bg-primary-50 active:bg-primary-100"),
144
144
  onClick: function onClick() {
145
- return checkableRows && !onRowClick ? toggleChecked(data[keyProperty]) : onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(data);
145
+ return checkableRows && keyProperty && !onRowClick ? toggleChecked(data[keyProperty]) : onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(data);
146
146
  },
147
147
  "data-index": index,
148
148
  ref: measureElement,
149
149
  children: [/*#__PURE__*/jsxs("div", {
150
150
  className: wrapperStart,
151
- children: [checkableRows && /*#__PURE__*/jsx(CheckboxCell, {
151
+ children: [checkableRows && keyProperty && /*#__PURE__*/jsx(CheckboxCell, {
152
152
  onClick: function onClick() {
153
153
  return toggleChecked(data[keyProperty]);
154
154
  },
@@ -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
  });
@@ -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
+ };
@@ -1,4 +1,5 @@
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";
@@ -4,7 +4,7 @@ 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";
@@ -30,7 +30,7 @@ export interface TableRef {
30
30
  export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
31
31
  columns: Array<ColumnType<T>>;
32
32
  data: T[];
33
- keyProperty: keyof T;
33
+ keyProperty?: keyof T;
34
34
  /**
35
35
  * checkable rows
36
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: Key, onCheckChanged: (checked: Key[]) => void) => {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/data",
3
- "version": "1.0.0-beta.3",
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.3",
37
- "@react-fabric/form": "^1.0.0-beta.3",
38
- "@react-fabric/utilities": "^1.0.0-beta.1",
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": "2ce2a968f48d6ffc338f5da3a0cf67232d0c6864"
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,6 +23,7 @@
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";
@@ -160,7 +160,7 @@ export const Table = <T extends KeyValue = KeyValue>({
160
160
  onRowClick && "hover:bg-primary-50 active:bg-primary-100",
161
161
  )}
162
162
  onClick={() =>
163
- checkableRows && !onRowClick
163
+ checkableRows && keyProperty && !onRowClick
164
164
  ? toggleChecked(data[keyProperty])
165
165
  : onRowClick?.(data)
166
166
  }
@@ -168,7 +168,7 @@ export const Table = <T extends KeyValue = KeyValue>({
168
168
  ref={measureElement}
169
169
  >
170
170
  <div className={wrapperStart}>
171
- {checkableRows && (
171
+ {checkableRows && keyProperty && (
172
172
  <CheckboxCell
173
173
  onClick={() => toggleChecked(data[keyProperty])}
174
174
  state={
@@ -37,7 +37,7 @@ export interface ColumnType<T = KeyValue>
37
37
  extends CssProp,
38
38
  IconProps,
39
39
  TestProps {
40
- id: keyof T;
40
+ id: keyof T | string;
41
41
  label?: string;
42
42
  actions?: ReactElement;
43
43
  align?: "start" | "center" | "end";
@@ -72,7 +72,7 @@ export interface TableRef {
72
72
  export interface TableProps<T = KeyValue> extends RefProp<TableRef> {
73
73
  columns: Array<ColumnType<T>>;
74
74
  data: T[];
75
- keyProperty: keyof T;
75
+ keyProperty?: keyof T;
76
76
  /**
77
77
  * checkable rows
78
78
  */
@@ -33,8 +33,8 @@ type DataAction = { type: "check"; key: Key } | { type: "checkall" };
33
33
 
34
34
  export const useVirtualTable = (
35
35
  data: KeyValue[],
36
- keyProperty: Key,
37
- onCheckChanged: (checked: Key[]) => void,
36
+ keyProperty?: Key,
37
+ onCheckChanged?: (checked: Key[]) => void,
38
38
  ) => {
39
39
  // TODO: refactor data into item list, apply grouping if groupColumn provided
40
40
  // TODO: when grouping maintain map of group with children, {___GROUP___:true, label, items, open}
@@ -56,11 +56,11 @@ export const useVirtualTable = (
56
56
  if (action.type === "checkall") {
57
57
  if (state.allChecked !== 0) {
58
58
  state.checked = [];
59
- } else {
59
+ } else if (keyProperty) {
60
60
  state.checked = data.map((item) => item[keyProperty]);
61
61
  }
62
62
  }
63
- onCheckChanged(state.checked);
63
+ onCheckChanged?.(state.checked);
64
64
  return {
65
65
  ...state,
66
66
  allChecked: