mui-component
Version:
some custom mui components
176 lines • 12.2 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); }
var _excluded = ["tableContainerBoxProps", "columns", "rows", "rowKey", "total", "current", "pageSize", "onPageChange", "onPageSizeChange", "pageSizeOptions", "initPageSize", "paginationProps", "hideHeader", "showTableFooter", "tableFooter", "tableFooterProps", "hidePagination", "autoHidePagination", "title", "titlePosition", "titleStyle", "bordered", "columnDefaultWidth", "expandable", "showExpandColumn", "expandIcon", "expandRowByClick", "expandColumnWidth", "expandRowRender", "getRowExpandable", "unmountOnExit"];
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { useControllableValue, useCreation } from "ahooks";
import { Box, Table, TableBody, TableCell, TableFooter, TableHead, TableRow } from "@mui/material";
import { Pagination } from "../Pagination";
import { Row } from "./Row";
import { NoData } from "../NoData";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var defaultPageSizes = [10, 25, 50, 100, 200];
export var SimpleTable = function SimpleTable(props) {
var _ref, _ref2;
var tableContainerBoxProps = props.tableContainerBoxProps,
columns = props.columns,
rows = props.rows,
rowKey = props.rowKey,
total = props.total,
current = props.current,
pageSize = props.pageSize,
onPageChange = props.onPageChange,
onPageSizeChange = props.onPageSizeChange,
_props$pageSizeOption = props.pageSizeOptions,
pageSizeOptions = _props$pageSizeOption === void 0 ? defaultPageSizes : _props$pageSizeOption,
_props$initPageSize = props.initPageSize,
initPageSize = _props$initPageSize === void 0 ? 10 : _props$initPageSize,
paginationProps = props.paginationProps,
hideHeader = props.hideHeader,
showTableFooter = props.showTableFooter,
tableFooter = props.tableFooter,
tableFooterProps = props.tableFooterProps,
hidePaginationProp = props.hidePagination,
autoHidePagination = props.autoHidePagination,
title = props.title,
_props$titlePosition = props.titlePosition,
titlePosition = _props$titlePosition === void 0 ? "top" : _props$titlePosition,
titleStyle = props.titleStyle,
bordered = props.bordered,
_props$columnDefaultW = props.columnDefaultWidth,
columnDefaultWidth = _props$columnDefaultW === void 0 ? 100 : _props$columnDefaultW,
expandable = props.expandable,
_props$showExpandColu = props.showExpandColumn,
showExpandColumn = _props$showExpandColu === void 0 ? true : _props$showExpandColu,
expandIcon = props.expandIcon,
expandRowByClick = props.expandRowByClick,
_props$expandColumnWi = props.expandColumnWidth,
expandColumnWidth = _props$expandColumnWi === void 0 ? 45 : _props$expandColumnWi,
expandRowRender = props.expandRowRender,
getRowExpandable = props.getRowExpandable,
unmountOnExit = props.unmountOnExit,
restProps = _objectWithoutProperties(props, _excluded);
var _useControllableValue = useControllableValue(props, {
valuePropName: "current",
trigger: "onPageChange",
defaultValue: 1
}),
_useControllableValue2 = _slicedToArray(_useControllableValue, 2),
pageNumber = _useControllableValue2[0],
setPageNumber = _useControllableValue2[1];
var _useControllableValue3 = useControllableValue(props, {
defaultValuePropName: "initPageSize",
valuePropName: "pageSize",
trigger: "onPageSizeChange",
defaultValue: 25
}),
_useControllableValue4 = _slicedToArray(_useControllableValue3, 2),
rowsPerPage = _useControllableValue4[0],
setRowsPerPage = _useControllableValue4[1];
var dataSource = useCreation(function () {
if (total === undefined || total === (rows === null || rows === void 0 ? void 0 : rows.length)) {
return (rows === null || rows === void 0 ? void 0 : rows.slice(rowsPerPage * (pageNumber - 1), rowsPerPage * pageNumber)) || [];
}
return rows || [];
}, [rows, rowsPerPage, pageNumber, total]);
var counts = (_ref = total !== null && total !== void 0 ? total : rows === null || rows === void 0 ? void 0 : rows.length) !== null && _ref !== void 0 ? _ref : 0;
var hidePagination = useCreation(function () {
if (typeof hidePaginationProp !== "undefined") return !!hidePaginationProp;
if (autoHidePagination) {
return !(counts > initPageSize && counts > rowsPerPage);
}
return false;
}, [hidePaginationProp, autoHidePagination, counts, initPageSize, rowsPerPage]);
return /*#__PURE__*/_jsxs(Box, {
children: [/*#__PURE__*/_jsx(Box, _objectSpread(_objectSpread({}, tableContainerBoxProps || {}), {}, {
sx: _objectSpread({
width: "100%",
px: 2,
overflow: "auto"
}, (tableContainerBoxProps === null || tableContainerBoxProps === void 0 ? void 0 : tableContainerBoxProps.sx) || {}),
children: /*#__PURE__*/_jsxs(Table, _objectSpread(_objectSpread({}, restProps), {}, {
children: [!!title && /*#__PURE__*/_jsx("caption", {
style: _objectSpread({}, _objectSpread({
padding: 0,
captionSide: titlePosition
}, titleStyle || {})),
children: title
}), !hideHeader && /*#__PURE__*/_jsx(TableHead, {
children: /*#__PURE__*/_jsx(Row, {
rowIndex: -1,
columns: columns,
expandable: expandable,
showExpandColumn: showExpandColumn,
expandColumnWidth: expandColumnWidth,
bordered: bordered,
columnDefaultWidth: columnDefaultWidth
})
}), /*#__PURE__*/_jsx(TableBody, {
children: dataSource !== null && dataSource !== void 0 && dataSource.length ? dataSource.map(function (item, index) {
var _item$rowKey;
var isExpandable = true;
if (typeof getRowExpandable === "function") {
var _getRowExpandable;
isExpandable = (_getRowExpandable = getRowExpandable(item, index)) !== null && _getRowExpandable !== void 0 ? _getRowExpandable : true;
}
return /*#__PURE__*/_jsx(Row
// @ts-ignore
, {
row: item,
rowIndex: index,
expandable: expandable,
isExpandable: expandable && isExpandable,
expandColumnWidth: expandColumnWidth,
expandIcon: expandIcon,
expandRowByClick: expandRowByClick,
expandRowRender: expandRowRender,
showExpandColumn: showExpandColumn,
bordered: bordered,
columns: columns,
hideHeader: hideHeader,
unmountOnExit: unmountOnExit,
columnDefaultWidth: columnDefaultWidth
}, (_item$rowKey = item === null || item === void 0 ? void 0 : item[rowKey]) !== null && _item$rowKey !== void 0 ? _item$rowKey : index);
}) : /*#__PURE__*/_jsx(TableRow, {
children: /*#__PURE__*/_jsx(TableCell, {
colSpan: ((columns === null || columns === void 0 ? void 0 : columns.length) || 0) + (expandable && showExpandColumn ? 1 : 0),
children: /*#__PURE__*/_jsx(Box, {
sx: {
width: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center"
},
children: /*#__PURE__*/_jsx(NoData, {})
})
})
})
}), showTableFooter && !!tableFooter && /*#__PURE__*/_jsx(TableFooter, _objectSpread(_objectSpread({}, tableFooterProps || {}), {}, {
children: tableFooter
}))]
}))
})), /*#__PURE__*/_jsx(Box, {
children: !hidePagination && /*#__PURE__*/_jsx(Pagination, _objectSpread({
total: (_ref2 = total !== null && total !== void 0 ? total : rows === null || rows === void 0 ? void 0 : rows.length) !== null && _ref2 !== void 0 ? _ref2 : 0,
current: pageNumber,
onPageChange: setPageNumber,
pageSize: rowsPerPage,
onPageSizeChange: setRowsPerPage,
color: "secondary",
pageSizeOptions: pageSizeOptions,
siblingCount: 1
}, paginationProps || {}))
})]
});
};