@etsoo/materialui
Version:
TypeScript Material-UI Implementation
74 lines (73 loc) • 3.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TablePage = TablePage;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@etsoo/react");
const react_2 = __importDefault(require("react"));
const MUGlobal_1 = require("../MUGlobal");
const SearchBar_1 = require("../SearchBar");
const TableEx_1 = require("../TableEx");
const CommonPage_1 = require("./CommonPage");
const GridUtils_1 = require("../GridUtils");
const Stack_1 = __importDefault(require("@mui/material/Stack"));
const Box_1 = __importDefault(require("@mui/material/Box"));
/**
* Table page
* @param props Props
* @returns Component
*/
function TablePage(props) {
// Destruct
const { columns, fields, fieldTemplate, loadData, mRef, sizeReadyMiliseconds = 0, pageProps = {}, cacheKey, cacheMinutes = 15, searchBarTop, ...rest } = props;
pageProps.paddings ??= MUGlobal_1.MUGlobal.pagePaddings;
// States
const [states] = react_2.default.useState({});
const refs = (0, react_1.useCombinedRefs)(mRef, (ref) => {
if (ref == null)
return;
const first = states.ref == null;
states.ref = ref;
if (first)
reset();
});
const reset = () => {
if (states.data == null || states.ref == null)
return;
states.ref.reset({ data: states.data });
};
// On submit callback
const onSubmit = (data, _reset) => {
states.data = data;
reset();
};
const localLoadData = (props, lastItem) => {
return loadData(GridUtils_1.GridUtils.createLoader(props, fieldTemplate, cacheKey, false), lastItem);
};
// Search data
const searchData = (0, react_1.useSearchParamsWithCache)(cacheKey);
// Total width
const totalWidth = react_2.default.useMemo(() => columns.reduce((previousValue, { width, minWidth }) => {
return previousValue + (width ?? minWidth ?? TableEx_1.TableExMinWidth);
}, 0), [columns]);
// Watch container
const { dimensions } = (0, react_1.useDimensions)(1, undefined, sizeReadyMiliseconds);
const rect = dimensions[0][2];
const list = react_2.default.useMemo(() => {
if (rect != null && rect.height > 50 && rect.width >= totalWidth) {
let maxHeight = document.documentElement.clientHeight - (rect.top + rect.height + 1);
const style = window.getComputedStyle(dimensions[0][1]);
const paddingBottom = parseFloat(style.paddingBottom);
if (!isNaN(paddingBottom))
maxHeight -= paddingBottom;
return ((0, jsx_runtime_1.jsx)(TableEx_1.TableEx, { autoLoad: false, columns: columns, loadData: localLoadData, maxHeight: maxHeight, mRef: refs, ...rest }));
}
}, [rect]);
const f = typeof fields == "function" ? fields(searchData ?? {}) : fields;
// Layout
return ((0, jsx_runtime_1.jsx)(CommonPage_1.CommonPage, { ...pageProps, scrollContainer: globalThis, children: (0, jsx_runtime_1.jsxs)(Stack_1.default, { children: [(0, jsx_runtime_1.jsx)(Box_1.default, { ref: dimensions[0][0], sx: {
paddingBottom: pageProps.paddings
}, children: rect && rect.width > 100 && ((0, jsx_runtime_1.jsx)(SearchBar_1.SearchBar, { fields: f, onSubmit: onSubmit, top: searchBarTop, width: rect.width })) }), list] }) }));
}