@amaui/ui-react
Version:
UI for React
134 lines • 6.61 kB
JavaScript
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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { is, clamp, pagination } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import IconMaterialLastPage from '@amaui/icons-material-rounded-react/IconMaterialLastPageW100';
import IconMaterialFirstPage from '@amaui/icons-material-rounded-react/IconMaterialFirstPageW100';
import IconMaterialNavigateNext from '@amaui/icons-material-rounded-react/IconMaterialNavigateNextW100';
import IconMaterialNavigateBefore from '@amaui/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: 'amaui-Pagination'
});
const Pagination = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiPagination?.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) && [`amaui-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) && [`amaui-Pagination-root`, `amaui-Pagination-version-${version}`, `amaui-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 = 'amaui-Pagination';
export default Pagination;