UNPKG

@adaptui/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

86 lines (66 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePaginationButton = exports.PaginationButton = void 0; var _utils = require("@chakra-ui/utils"); var _ariakitUtils = require("ariakit-utils"); var _system = require("ariakit-utils/system"); var _utils2 = require("./__utils"); var _excluded = ["state", "goto"]; 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; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var usePaginationButton = (0, _system.createHook)(function (_ref) { var state = _ref.state, goto = _ref.goto, props = _objectWithoutProperties(_ref, _excluded); state = (0, _ariakitUtils.useStore)(state || _utils2.PaginationContextState, ["isAtFirstPage", "isAtLastPage", "currentPage"]); if (!state) return props; var _state = state, isAtLastPage = _state.isAtLastPage, isAtFirstPage = _state.isAtFirstPage, currentPage = _state.currentPage; var isCurrent = currentPage === goto; var disabled = false; if (goto === "nextPage" || goto === "lastPage") { disabled = isAtLastPage; } if (goto === "prevPage" || goto === "firstPage") { disabled = isAtFirstPage; } disabled = props.disabled || disabled; var ariaLabel = isCurrent ? "Page ".concat(goto) : "Go to ".concat(goto === "prevPage" ? "previous" : goto, " Page"); var onClickProp = props.onClick; var onClick = (0, _ariakitUtils.useEvent)(function (event) { onClickProp === null || onClickProp === void 0 ? void 0 : onClickProp(event); if (event.defaultPrevented) return; if (disabled) return; if ((0, _utils.isNumber)(goto)) { var _state2, _state2$movePage; (_state2 = state) === null || _state2 === void 0 ? void 0 : (_state2$movePage = _state2.movePage) === null || _state2$movePage === void 0 ? void 0 : _state2$movePage.call(_state2, goto); return; } if (["nextPage", "prevPage", "lastPage", "firstPage"].includes(goto)) { var _state3, _state3$goto; (_state3 = state) === null || _state3 === void 0 ? void 0 : (_state3$goto = _state3[goto]) === null || _state3$goto === void 0 ? void 0 : _state3$goto.call(_state3); return; } }); props = _objectSpread({ "aria-label": ariaLabel, "aria-current": isCurrent ? true : undefined, disabled: disabled, onClick: onClick }, props); return props; }); exports.usePaginationButton = usePaginationButton; var PaginationButton = (0, _system.createComponent)(function (props) { var htmlProps = usePaginationButton(props); return (0, _system.createElement)("button", htmlProps); }); exports.PaginationButton = PaginationButton; //# sourceMappingURL=pagination-button.js.map