@exabytellc/ui
Version:
236 lines (228 loc) • 11.8 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = Table;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _excluded = ["body", "bordered"],
_excluded2 = ["rows", "head", "bordered", "className", "push"],
_excluded3 = ["cols", "bordered", "className", "push"],
_excluded4 = ["value", "bordered", "className"],
_excluded5 = ["bordered"],
_excluded6 = ["bordered"],
_excluded7 = ["bordered"],
_excluded8 = ["bordered"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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; }
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
/**
* A flexible table component that supports dynamic rows and columns with optional borders.
*
* @param {Object} props - The properties passed to the component.
* @param {Array} [props.body=[]] - Array containing table data.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @returns {JSX.Element} The rendered table component.
*/
function Table(_ref) {
var _ref$body = _ref.body,
body = _ref$body === void 0 ? [] : _ref$body,
_ref$bordered = _ref.bordered,
bordered = _ref$bordered === void 0 ? false : _ref$bordered,
props = _objectWithoutProperties(_ref, _excluded);
return /*#__PURE__*/_react["default"].createElement("table", props, body === null || body === void 0 ? void 0 : body.map(function (_ref2, i) {
var rows = _ref2.rows,
head = _ref2.head,
groupBordered = _ref2.bordered,
groupClassName = _ref2.className,
_ = _ref2.push,
props = _objectWithoutProperties(_ref2, _excluded2);
var Group = head ? "thead" : "tbody";
var Cell = head ? "th" : "td";
var key = "t-g".concat(i);
return /*#__PURE__*/_react["default"].createElement(Group, _extends({}, props, {
key: key,
className: "".concat(groupBordered || bordered ? "border" : "", " ").concat(groupClassName)
}), rows === null || rows === void 0 ? void 0 : rows.map(function (_ref3, i2) {
var cols = _ref3.cols,
rowBordered = _ref3.bordered,
rowClassName = _ref3.className,
_ = _ref3.push,
props = _objectWithoutProperties(_ref3, _excluded3);
var key2 = "".concat(key, "-r").concat(i2);
return /*#__PURE__*/_react["default"].createElement("tr", _extends({
key: key2
}, props, {
className: "".concat(rowBordered || bordered ? "border" : "", " ").concat(rowClassName)
}), cols === null || cols === void 0 ? void 0 : cols.map(function () {
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
value = _ref4.value,
cellBordered = _ref4.bordered,
cellClassName = _ref4.className,
props = _objectWithoutProperties(_ref4, _excluded4);
var i3 = arguments.length > 1 ? arguments[1] : undefined;
var key3 = "".concat(key2, "-c").concat(i3);
return /*#__PURE__*/_react["default"].createElement(Cell, _extends({
key: key3
}, props, {
className: "".concat(cellBordered || bordered ? "border" : "", " ").concat(cellClassName !== null && cellClassName !== void 0 ? cellClassName : "py-1 px-1")
}), value);
}));
}));
}));
}
Table.propTypes = {
body: _propTypes["default"].array,
bordered: _propTypes["default"].bool,
props: _propTypes["default"].object
};
/**
* Helper function to create a table head section.
*
* @param {Array} rows - Array of row objects.
* @param {Object} [props] - Additional properties for the head section.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @returns {Object} An object representing the table head section.
*/
Table.head = function () {
var rows = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var _ref5 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref5$bordered = _ref5.bordered,
bordered = _ref5$bordered === void 0 ? false : _ref5$bordered,
props = _objectWithoutProperties(_ref5, _excluded5);
var push = function push(item) {
return rows.push(item);
};
return _objectSpread(_objectSpread({
head: true,
rows: rows !== null && rows !== void 0 ? rows : [],
bordered: bordered
}, props), {}, {
push: push
});
};
/**
* Helper function to create a table body section.
*
* @param {Array} rows - Array of row objects.
* @param {Object} [props] - Additional properties for the body section.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @returns {Object} An object representing the table body section.
*/
Table.body = function () {
var rows = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref6$bordered = _ref6.bordered,
bordered = _ref6$bordered === void 0 ? false : _ref6$bordered,
props = _objectWithoutProperties(_ref6, _excluded6);
var push = function push(item) {
return rows.push(item);
};
return _objectSpread(_objectSpread({
head: false,
rows: rows !== null && rows !== void 0 ? rows : [],
bordered: bordered
}, props), {}, {
push: push
});
};
/**
* Helper function to create a table row.
*
* @param {Array} cols - Array of column objects.
* @param {Object} [props] - Additional properties for the row.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @returns {Object} An object representing the table row.
*/
Table.row = function () {
var cols = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var _ref7 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref7$bordered = _ref7.bordered,
bordered = _ref7$bordered === void 0 ? false : _ref7$bordered,
props = _objectWithoutProperties(_ref7, _excluded7);
var push = function push(item) {
return cols.push(item);
};
return _objectSpread(_objectSpread({
cols: cols !== null && cols !== void 0 ? cols : [],
bordered: bordered
}, props), {}, {
push: push
});
};
/**
* Helper function to create a table cell.
*
* @param {any} value - The value of the cell.
* @param {Object} [props] - Additional properties for the cell.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @returns {Object} An object representing the table cell.
*/
Table.cell = function (value) {
var _ref8 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref8$bordered = _ref8.bordered,
bordered = _ref8$bordered === void 0 ? false : _ref8$bordered,
props = _objectWithoutProperties(_ref8, _excluded8);
return _objectSpread({
value: value !== null && value !== void 0 ? value : "",
bordered: bordered
}, props);
};
/**
* Helper function to create table head and body sections from given columns and data.
*
* @param {Array} columns - Array of column objects with `name`, `field`, and optional `width`, `render`, `cellProps`.
* @param {Array} data - Array of data objects.
* @param {Object} [props] - Additional properties for the table.
* @param {boolean} [props.bordered=false] - Whether the table cells should have borders.
* @param {Object} [props.headProps] - Additional properties for the table head section.
* @param {Object} [props.bodyProps] - Additional properties for the table body section.
* @returns {Array} An array representing the table head and body sections.
*/
Table.data = function () {
var columns = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var _ref9 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref9$bordered = _ref9.bordered,
bordered = _ref9$bordered === void 0 ? false : _ref9$bordered,
headProps = _ref9.headProps,
bodyProps = _ref9.bodyProps;
// Create table head rows based on columns
var headRows = Table.row(columns.map(function (_ref10) {
var name = _ref10.name,
width = _ref10.width;
return Table.cell(name, {
bordered: bordered,
style: {
width: width !== null && width !== void 0 ? width : "auto"
} // Set width if provided, default to "auto"
});
}));
// Create table body rows based on data and columns
var bodyRows = data.map(function (rowData, rowIndex) {
return Table.row(columns.map(function (_ref11) {
var field = _ref11.field,
render = _ref11.render,
cellProps = _ref11.cellProps;
return Table.cell(render ? render(rowData, rowIndex) : rowData[field], _objectSpread({
bordered: bordered
}, cellProps !== null && cellProps !== void 0 ? cellProps : {}));
}));
});
// Return an array containing the table head and body sections
return [Table.head([headRows], _objectSpread({
bordered: bordered
}, headProps !== null && headProps !== void 0 ? headProps : {})),
// Table head section
Table.body(bodyRows, _objectSpread({
bordered: bordered
}, bodyProps !== null && bodyProps !== void 0 ? bodyProps : {})) // Table body section
];
};