UNPKG

@amaui/ui-react

Version:
101 lines (100 loc) 9.06 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const IconMaterialLastPageW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialLastPageW100")); const IconMaterialFirstPageW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialFirstPageW100")); const IconMaterialNavigateNextW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialNavigateNextW100")); const IconMaterialNavigateBeforeW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialNavigateBeforeW100")); const Line_1 = __importDefault(require("../Line")); const Type_1 = __importDefault(require("../Type")); const Select_1 = __importDefault(require("../Select")); const ListItem_1 = __importDefault(require("../ListItem")); const IconButton_1 = __importDefault(require("../IconButton")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: {}, select: { minWidth: '140px' }, text: { flex: '0 0 auto' } }), { name: 'amaui-TablePagination' }); const TablePagination = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiTablePagination) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); const Select = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Select) || Select_1.default; }, [theme]); const ListItem = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ListItem) || ListItem_1.default; }, [theme]); const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]); const { tonal = false, color = 'default', version = 'text', elevation = false, size = 'regular', page: page_ = 0, pageDefault, onChange, rowsPerPage: rowsPerPage_ = props.total < 4 ? props.total : props.total * 0.05, rowsPerPageDefault, rowsPerPageOptions = props.total < 4 ? [props.total] : [props.total * 0.05, props.total * 0.1, props.total * 0.25, props.total * 0.5, props.total], rowsPerPageText = 'Rows per page', onRowsPerPageChange, total, first = false, before = true, next = true, last = false, IconFirst = IconMaterialFirstPageW100_1.default, IconBefore = IconMaterialNavigateBeforeW100_1.default, IconNext = IconMaterialNavigateNextW100_1.default, IconLast = IconMaterialLastPageW100_1.default, Component = 'div', className, children } = props, other = __rest(props, ["tonal", "color", "version", "elevation", "size", "page", "pageDefault", "onChange", "rowsPerPage", "rowsPerPageDefault", "rowsPerPageOptions", "rowsPerPageText", "onRowsPerPageChange", "total", "first", "before", "next", "last", "IconFirst", "IconBefore", "IconNext", "IconLast", "Component", "className", "children"]); const { classes } = useStyle(); const [page, setPage] = react_1.default.useState(pageDefault !== undefined ? pageDefault : page_); const [rowsPerPage, setRowsPerPage] = react_1.default.useState(rowsPerPageDefault !== undefined ? rowsPerPageDefault : rowsPerPage_); react_1.default.useEffect(() => { if (rowsPerPage_ !== rowsPerPage) setRowsPerPage(rowsPerPage_); }, [rowsPerPage_]); const onUpdateRowsPerPage = (value) => { // Update inner or controlled if (!props.hasOwnProperty('rowsPerPage')) setRowsPerPage(value); if ((0, utils_1.is)('function', onRowsPerPageChange)) onRowsPerPageChange(value); update(0); }; const update = (value) => { // Update inner or controlled if (!props.hasOwnProperty('page')) setPage(value); if ((0, utils_1.is)('function', onChange)) onChange(value); }; const maxPage = Math.ceil(total / rowsPerPage) - 1; const IconButtonProps = { tonal, color, version, elevation, size }; return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: ref, gap: 3, direction: 'row', justify: 'flex-start', align: 'center', Component: Component, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TablePagination', theme) && [ `amaui-TablePagination-root`, `amaui-TablePagination-version-${version}`, `amaui-TablePagination-size-${size}` ], className, classes.root ]) }, other, { children: [(0, jsx_runtime_1.jsx)(Line, Object.assign({ gap: 1, direction: 'row', justify: 'flex-start', align: 'center' }, { children: (0, jsx_runtime_1.jsx)(Select, Object.assign({ tonal: tonal, color: color, label: rowsPerPageText, size: 'small', value: rowsPerPage, onChange: onUpdateRowsPerPage, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TablePagination', theme) && [ `amaui-TablePagination-select` ], classes.select ]) }, { children: rowsPerPageOptions.map(item => ((0, jsx_runtime_1.jsx)(ListItem, { primary: item, value: item, button: true }, item))) })) })), (0, jsx_runtime_1.jsxs)(Type, Object.assign({ version: 'b2', gap: 1, direction: 'row', justify: 'flex-start', align: 'center', Component: Line, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TablePagination', theme) && [ `amaui-TablePagination-text` ], classes.text ]) }, { children: [(0, jsx_runtime_1.jsxs)("span", { children: [(page * rowsPerPage) + 1, " - ", ((page + 1) * rowsPerPage) || total] }), " ", (0, jsx_runtime_1.jsx)("span", { children: "of" }), " ", (0, jsx_runtime_1.jsx)("span", { children: total })] })), (0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', justify: 'flex-start', align: 'center' }, { children: [first && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => update(0), disabled: page === 0 }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconFirst, {}) }))), before && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => update((0, utils_1.clamp)(page - 1, 0, maxPage)), disabled: page === 0 }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconBefore, {}) }))), next && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => update((0, utils_1.clamp)(page + 1, 0, maxPage)), disabled: page === maxPage }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconNext, {}) }))), last && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => update(maxPage), disabled: page === maxPage }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconLast, {}) })))] }))] }))); }); TablePagination.displayName = 'amaui-TablePagination'; exports.default = TablePagination;