UNPKG

@onesy/ui-react

Version:
167 lines 7.48 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["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"]; 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; } import React from 'react'; import { clamp, is } from '@onesy/utils'; import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react'; import IconMaterialLastPage from '@onesy/icons-material-rounded-react/IconMaterialLastPageW100'; import IconMaterialFirstPage from '@onesy/icons-material-rounded-react/IconMaterialFirstPageW100'; import IconMaterialNavigateNext from '@onesy/icons-material-rounded-react/IconMaterialNavigateNextW100'; import IconMaterialNavigateBefore from '@onesy/icons-material-rounded-react/IconMaterialNavigateBeforeW100'; import LineElement from '../Line'; import TypeElement from '../Type'; import SelectElement from '../Select'; import ListItemElement from '../ListItem'; import IconButtonElement from '../IconButton'; import { staticClassName } from '../utils'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const useStyle = styleMethod(theme => ({ root: {}, select: { minWidth: '140px' }, text: { flex: '0 0 auto' } }), { name: 'onesy-TablePagination' }); const TablePagination = props_ => { const theme = useOnesyTheme(); const l = theme.l; const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyTablePagination?.props?.default), props_); const Line = theme?.elements?.Line || LineElement; const Type = theme?.elements?.Type || TypeElement; const Select = theme?.elements?.Select || SelectElement; const ListItem = theme?.elements?.ListItem || ListItemElement; const IconButton = theme?.elements?.IconButton || IconButtonElement; 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 = IconMaterialFirstPage, IconBefore = IconMaterialNavigateBefore, IconNext = IconMaterialNavigateNext, IconLast = IconMaterialLastPage, Component = 'div', className, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const [page, setPage] = React.useState(pageDefault !== undefined ? pageDefault : page_); const [rowsPerPage, setRowsPerPage] = React.useState(rowsPerPageDefault !== undefined ? rowsPerPageDefault : rowsPerPage_); React.useEffect(() => { if (rowsPerPage_ !== rowsPerPage) setRowsPerPage(rowsPerPage_); }, [rowsPerPage_]); const onUpdateRowsPerPage = value => { // Update inner or controlled if (!props.hasOwnProperty('rowsPerPage')) setRowsPerPage(value); if (is('function', onRowsPerPageChange)) onRowsPerPageChange(value); update(0); }; const update = value_0 => { // Update inner or controlled if (!props.hasOwnProperty('page')) setPage(value_0); if (is('function', onChange)) onChange(value_0); }; const maxPage = Math.ceil(total / rowsPerPage) - 1; const IconButtonProps = { tonal, color, version, elevation, size }; return /*#__PURE__*/_jsxs(Line, _objectSpread(_objectSpread({ gap: 3, direction: "row", justify: "flex-start", align: "center", Component: Component, className: classNames([staticClassName('TablePagination', theme) && [`onesy-TablePagination-root`, `onesy-TablePagination-version-${version}`, `onesy-TablePagination-size-${size}`], className, classes.root]) }, other), {}, { children: [/*#__PURE__*/_jsx(Line, { gap: 1, direction: "row", justify: "flex-start", align: "center", children: /*#__PURE__*/_jsx(Select, { tonal: tonal, color: color, label: rowsPerPageText, size: "small", value: rowsPerPage, onChange: onUpdateRowsPerPage, className: classNames([staticClassName('TablePagination', theme) && [`onesy-TablePagination-select`], classes.select]), children: rowsPerPageOptions.map(item => /*#__PURE__*/_jsx(ListItem, { primary: item, value: item, button: true }, item)) }) }), /*#__PURE__*/_jsxs(Type, { version: "b2", gap: 1, direction: "row", justify: "flex-start", align: "center", Component: Line, className: classNames([staticClassName('TablePagination', theme) && [`onesy-TablePagination-text`], classes.text]), children: [/*#__PURE__*/_jsxs("span", { children: [page * rowsPerPage + 1, " - ", (page + 1) * rowsPerPage || total] }), " ", /*#__PURE__*/_jsx("span", { children: l('of') }), " ", /*#__PURE__*/_jsx("span", { children: total })] }), /*#__PURE__*/_jsxs(Line, { gap: 1, direction: "row", justify: "flex-start", align: "center", children: [first && /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({ onClick: () => update(0), disabled: page === 0 }, IconButtonProps), {}, { children: /*#__PURE__*/_jsx(IconFirst, {}) })), before && /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({ onClick: () => update(clamp(page - 1, 0, maxPage)), disabled: page === 0 }, IconButtonProps), {}, { children: /*#__PURE__*/_jsx(IconBefore, {}) })), next && /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({ onClick: () => update(clamp(page + 1, 0, maxPage)), disabled: page === maxPage }, IconButtonProps), {}, { children: /*#__PURE__*/_jsx(IconNext, {}) })), last && /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({ onClick: () => update(maxPage), disabled: page === maxPage }, IconButtonProps), {}, { children: /*#__PURE__*/_jsx(IconLast, {}) }))] })] })); }; TablePagination.displayName = 'onesy-TablePagination'; export default TablePagination;