baseui
Version:
A React Component library implementing the Base design language
80 lines (77 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _constants = require("./constants");
var _utils = require("./utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/ // Files
// Types
const initialState = {
currentPage: 1
};
class PaginationStatefulContainer extends React.Component {
constructor(...args) {
super(...args);
_defineProperty(this, "state", this.props.initialState || initialState);
_defineProperty(this, "onPageChange", ({
nextPage
}) => {
const {
numPages,
onPageChange
} = this.props;
const {
currentPage
} = this.state;
const clamped = (0, _utils.clamp)(nextPage, 1, numPages);
if (clamped !== currentPage) {
onPageChange && onPageChange({
nextPage: clamped,
prevPage: currentPage
});
this.internalSetState(_constants.STATE_CHANGE_TYPE.changePage, {
currentPage: clamped
});
}
});
}
// Internal set state function that will also invoke stateReducer
internalSetState(changeType, changes) {
const {
stateReducer
} = this.props;
if (stateReducer) {
this.setState(stateReducer(changeType, changes, this.state));
} else {
this.setState(changes);
}
}
render() {
const {
currentPage
} = this.state;
const {
children
} = this.props;
return children({
currentPage,
onPageChange: this.onPageChange
});
}
}
exports.default = PaginationStatefulContainer;
_defineProperty(PaginationStatefulContainer, "defaultProps", {
initialState,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
stateReducer: (changeType, changes) => changes
});