UNPKG

@onesy/ui-react

Version:
134 lines 6.46 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["tonal", "color", "version", "elevation", "size", "value", "valueDefault", "onChange", "total", "boundary", "middle", "first", "before", "next", "last", "renderItem", "disableSelected", "disabled", "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 { is, clamp, pagination } 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 TypeElement from '../Type'; import LineElement from '../Line'; import IconButtonElement from '../IconButton'; import PaginationItemElement from '../PaginationItem'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: {}, dots: { display: 'inline-block', textAlign: 'center' }, dots_size_small: { width: '30px' }, dots_size_regular: { width: '40px' }, dots_size_large: { width: '50px' } }), { name: 'onesy-Pagination' }); const Pagination = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useOnesyTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyPagination?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]); const IconButton = React.useMemo(() => theme?.elements?.IconButton || IconButtonElement, [theme]); const PaginationItem = React.useMemo(() => theme?.elements?.PaginationItem || PaginationItemElement, [theme]); const { tonal = false, color = 'default', version = 'text', elevation = false, size = 'regular', value: value_ = 1, valueDefault, onChange: onChange_, total = 1, boundary = 1, middle = 1, first = false, before = true, next = true, last = false, renderItem: renderItem_, disableSelected = true, disabled, IconFirst = IconMaterialFirstPage, IconBefore = IconMaterialNavigateBefore, IconNext = IconMaterialNavigateNext, IconLast = IconMaterialLastPage, Component = 'div', className, children } = props, other = _objectWithoutProperties(props, _excluded); const [value, setValue] = React.useState(valueDefault !== undefined ? valueDefault : value_); const { classes } = useStyle(); React.useEffect(() => { if (value_ !== value) setValue(value_); }, [value_]); const onChange = value__ => { // Update inner or controlled if (!props.hasOwnProperty('value')) setValue(value__); if (is('function', onChange_)) onChange_(value__); }; const values = pagination(value, total, boundary, middle); const renderItem = is('function', renderItem_) ? renderItem_ : (value__, item) => { if (is('string', item)) return /*#__PURE__*/React.createElement(Type, { version: props.size === 'large' ? 'b1' : props.size === 'small' ? 'b3' : 'b2', className: classNames([staticClassName('Pagination', theme) && [`onesy-Pagination-dots`], classes.dots, classes[`dots_size_${size}`]]) }, item); return /*#__PURE__*/React.createElement(PaginationItem, { tonal: tonal, color: color, version: version, elevation: elevation, size: size, onClick: () => onChange(item), selected: value__ === item, disabled: disabled || value__ === item && disableSelected }, item); }; const IconButtonProps = { tonal, color, version, elevation, size }; return /*#__PURE__*/React.createElement(Line, _extends({ ref: ref, gap: 0.5, wrap: "wrap", direction: "row", justify: "flex-start", align: "center", Component: Component, className: classNames([staticClassName('Pagination', theme) && [`onesy-Pagination-root`, `onesy-Pagination-version-${version}`, `onesy-Pagination-size-${size}`], className, classes.root]) }, other), first && /*#__PURE__*/React.createElement(IconButton, _extends({ onClick: () => onChange(1), disabled: value === 1 }, IconButtonProps), /*#__PURE__*/React.createElement(IconFirst, null)), before && /*#__PURE__*/React.createElement(IconButton, _extends({ onClick: () => onChange(clamp(value - 1, 1, total)), disabled: value === 1 }, IconButtonProps), /*#__PURE__*/React.createElement(IconBefore, null)), values.map((item, index) => renderItem(value, item, index)).map((item, index) => (/*#__PURE__*/React.cloneElement(item, { key: index }))), next && /*#__PURE__*/React.createElement(IconButton, _extends({ onClick: () => onChange(clamp(value + 1, 1, total)), disabled: value === total }, IconButtonProps), /*#__PURE__*/React.createElement(IconNext, null)), last && /*#__PURE__*/React.createElement(IconButton, _extends({ onClick: () => onChange(total), disabled: value === total }, IconButtonProps), /*#__PURE__*/React.createElement(IconLast, null))); }); Pagination.displayName = 'onesy-Pagination'; export default Pagination;