UNPKG

@amaui/ui-react

Version:
102 lines (101 loc) 7.89 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 Type_1 = __importDefault(require("../Type")); const Line_1 = __importDefault(require("../Line")); const IconButton_1 = __importDefault(require("../IconButton")); const PaginationItem_1 = __importDefault(require("../PaginationItem")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(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 = 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.amauiPagination) === 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 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 PaginationItem = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.PaginationItem) || PaginationItem_1.default; }, [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 = 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", "value", "valueDefault", "onChange", "total", "boundary", "middle", "first", "before", "next", "last", "renderItem", "disableSelected", "disabled", "IconFirst", "IconBefore", "IconNext", "IconLast", "Component", "className", "children"]); const [value, setValue] = react_1.default.useState(valueDefault !== undefined ? valueDefault : value_); const { classes } = useStyle(); react_1.default.useEffect(() => { if (value_ !== value) setValue(value_); }, [value_]); const onChange = (value__) => { // Update inner or controlled if (!props.hasOwnProperty('value')) setValue(value__); if ((0, utils_1.is)('function', onChange_)) onChange_(value__); }; const values = (0, utils_1.pagination)(value, total, boundary, middle); const renderItem = (0, utils_1.is)('function', renderItem_) ? renderItem_ : (value__, item) => { if ((0, utils_1.is)('string', item)) return ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: props.size === 'large' ? 'b1' : props.size === 'small' ? 'b3' : 'b2', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Pagination', theme) && [ `amaui-Pagination-dots` ], classes.dots, classes[`dots_size_${size}`] ]) }, { children: item }))); return ((0, jsx_runtime_1.jsx)(PaginationItem, Object.assign({ tonal: tonal, color: color, version: version, elevation: elevation, size: size, onClick: () => onChange(item), selected: value__ === item, disabled: disabled || value__ === item && disableSelected }, { children: item }))); }; const IconButtonProps = { tonal, color, version, elevation, size }; return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ ref: ref, gap: 0.5, wrap: 'wrap', direction: 'row', justify: 'flex-start', align: 'center', Component: Component, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Pagination', theme) && [ `amaui-Pagination-root`, `amaui-Pagination-version-${version}`, `amaui-Pagination-size-${size}` ], className, classes.root ]) }, other, { children: [first && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => onChange(1), disabled: value === 1 }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconFirst, {}) }))), before && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => onChange((0, utils_1.clamp)(value - 1, 1, total)), disabled: value === 1 }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconBefore, {}) }))), values .map((item, index) => renderItem(value, item, index)) .map((item, index) => (react_1.default.cloneElement(item, { key: index }))), next && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => onChange((0, utils_1.clamp)(value + 1, 1, total)), disabled: value === total }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconNext, {}) }))), last && ((0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: () => onChange(total), disabled: value === total }, IconButtonProps, { children: (0, jsx_runtime_1.jsx)(IconLast, {}) })))] }))); }); Pagination.displayName = 'amaui-Pagination'; exports.default = Pagination;