lucid-ui
Version:
A UI component library from AppNexus.
152 lines • 13.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaginatorDumb = void 0;
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var style_helpers_1 = require("../../util/style-helpers");
var reducers = __importStar(require("./Paginator.reducers"));
var Paginator_selectors_1 = __importDefault(require("./Paginator.selectors"));
var SingleSelect_1 = require("../SingleSelect/SingleSelect");
var TextField_1 = __importDefault(require("../TextField/TextField"));
var Button_1 = require("../Button/Button");
var ArrowIcon_1 = __importDefault(require("../Icon/ArrowIcon/ArrowIcon"));
var state_management_1 = require("../../util/state-management");
var cx = style_helpers_1.lucidClassNames.bind('&-Paginator');
var arrayOf = prop_types_1.default.arrayOf, bool = prop_types_1.default.bool, func = prop_types_1.default.func, number = prop_types_1.default.number, object = prop_types_1.default.object, oneOfType = prop_types_1.default.oneOfType, shape = prop_types_1.default.shape, string = prop_types_1.default.string;
var Option = SingleSelect_1.SingleSelectDumb.Option;
var defaultProps = {
hasPageSizeSelector: false,
isDisabled: false,
objectLabel: 'Object',
onPageSelect: lodash_1.default.noop,
selectedPageIndex: 0,
selectedPageSizeIndex: 0,
showTotalObjects: false,
totalCount: null,
totalPages: undefined,
pageSizeOptions: [10, 50, 100],
SingleSelect: __assign(__assign({}, SingleSelect_1.SingleSelectDumb.defaultProps), { selectedIndex: 0 }),
TextField: TextField_1.default.defaultProps,
};
var Paginator = /** @class */ (function (_super) {
__extends(Paginator, _super);
function Paginator() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.handleTextFieldChange = function (pageNum, _a) {
var props = _a.props, event = _a.event;
var _b = _this.props, onPageSelect = _b.onPageSelect, selectedPageIndex = _b.selectedPageIndex, totalPages = _b.totalPages;
var parsedPageNum = lodash_1.default.parseInt(pageNum);
if (lodash_1.default.isNaN(parsedPageNum)) {
return onPageSelect(selectedPageIndex, totalPages, { props: props, event: event });
}
return onPageSelect(parsedPageNum - 1, totalPages, { props: props, event: event });
};
return _this;
}
Paginator.prototype.render = function () {
var _a = this.props, className = _a.className, hasPageSizeSelector = _a.hasPageSizeSelector, isDisabled = _a.isDisabled, objectLabel = _a.objectLabel, objectLabelPlural = _a.objectLabelPlural, onPageSelect = _a.onPageSelect, onPageSizeSelect = _a.onPageSizeSelect, pageSizeOptions = _a.pageSizeOptions, selectedPageIndex = _a.selectedPageIndex, selectedPageSizeIndex = _a.selectedPageSizeIndex, showTotalObjects = _a.showTotalObjects, totalPages = _a.totalPages, totalCount = _a.totalCount, style = _a.style, singleSelectProps = _a.SingleSelect, textFieldProps = _a.TextField;
return (react_1.default.createElement("div", { style: style, className: cx('&', className) },
showTotalObjects && lodash_1.default.isNumber(totalCount) && (react_1.default.createElement("div", { className: cx('&-total-count') },
lodash_1.default.isFunction(showTotalObjects)
? showTotalObjects(totalCount)
: totalCount.toLocaleString(),
' ',
totalCount === 1
? objectLabel
: objectLabelPlural || objectLabel + "s")),
hasPageSizeSelector ? (react_1.default.createElement("div", { className: cx('&-page-size-container') },
react_1.default.createElement("span", { className: cx('&-rows-per-page-label') }, "Rows per page:"),
react_1.default.createElement(SingleSelect_1.SingleSelectDumb, __assign({}, singleSelectProps, { hasReset: false, isInvisible: true, isSelectionHighlighted: false, isDisabled: isDisabled, selectedIndex: selectedPageSizeIndex, onSelect: onPageSizeSelect }), lodash_1.default.map(pageSizeOptions, function (option) { return (react_1.default.createElement(Option, { key: option }, option)); })))) : null,
react_1.default.createElement(Button_1.Button, { onClick: lodash_1.default.partial(onPageSelect, selectedPageIndex - 1, totalPages), isDisabled: isDisabled || selectedPageIndex === 0, kind: 'invisible', hasOnlyIcon: true },
react_1.default.createElement(ArrowIcon_1.default, { direction: 'left' })),
react_1.default.createElement(TextField_1.default, __assign({ lazyLevel: 100 }, textFieldProps, { onBlur: this.handleTextFieldChange, onSubmit: this.handleTextFieldChange, isDisabled: isDisabled, value: selectedPageIndex + 1 })),
!lodash_1.default.isNil(totalPages) && react_1.default.createElement("span", null,
"of ",
totalPages.toLocaleString()),
react_1.default.createElement(Button_1.Button, { kind: 'invisible', onClick: lodash_1.default.partial(onPageSelect, selectedPageIndex + 1, totalPages), isDisabled: isDisabled || selectedPageIndex === totalPages - 1, hasOnlyIcon: true },
react_1.default.createElement(ArrowIcon_1.default, { direction: 'right' }))));
};
Paginator.displayName = 'Paginator';
Paginator.peek = {
description: "\t\n\t\t\tA paginator that has an optional page size selector.\t\n\t\t",
categories: ['navigation'],
madeFrom: ['ArrowIcon', 'TextField', 'Button', 'SingleSelect'],
};
Paginator.reducers = reducers;
Paginator.selectors = Paginator_selectors_1.default;
Paginator.defaultProps = defaultProps;
Paginator.propTypes = {
className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\t\n\t\t\tAppended to the component-specific class names set on the root elements.\t\n\t\t"], ["\t\n\t\t\tAppended to the component-specific class names set on the root elements.\t\n\t\t"]))),
style: object(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\t\n\t\t\tStyles that are passed through to root element.\t\n\t\t"], ["\t\n\t\t\tStyles that are passed through to root element.\t\n\t\t"]))),
isDisabled: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\t\n\t\t\tDisables the Paginator from being clicked or focused.\t\n\t\t"], ["\t\n\t\t\tDisables the Paginator from being clicked or focused.\t\n\t\t"]))),
hasPageSizeSelector: bool(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\t\n\t\t\tWhether to show the page size selector.\t\n\t\t"], ["\t\n\t\t\tWhether to show the page size selector.\t\n\t\t"]))),
selectedPageIndex: number(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\t\n\t\t\t0-indexed currently selected page number.\t\n\t\t"], ["\t\n\t\t\t0-indexed currently selected page number.\t\n\t\t"]))),
selectedPageSizeIndex: number(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\t\n\t\t\tCurrently selected page size option index.\t\n\t\t"], ["\t\n\t\t\tCurrently selected page size option index.\t\n\t\t"]))),
SingleSelect: shape(SingleSelect_1.SingleSelectDumb.propTypes)(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\t\n\t\t\tObject of SingleSelect props which are passed thru to the underlying\t\n\t\t\tSingleSelect component for the page size selector.\t\n\t\t"], ["\t\n\t\t\tObject of SingleSelect props which are passed thru to the underlying\t\n\t\t\tSingleSelect component for the page size selector.\t\n\t\t"]))),
showTotalObjects: oneOfType([bool, func])(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\t\n\t\t\tShow total count of objects.\t\n\t\t"], ["\t\n\t\t\tShow total count of objects.\t\n\t\t"]))),
objectLabel: string(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\t\n\t\t\tLabel when showTotalObjects is true with 1 or fewer objects.\t\n\t\t"], ["\t\n\t\t\tLabel when showTotalObjects is true with 1 or fewer objects.\t\n\t\t"]))),
objectLabelPlural: string(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\t\n\t\t\tLabel when showTotalObjects is true with more than 1 objects.\t\n\t\t"], ["\t\n\t\t\tLabel when showTotalObjects is true with more than 1 objects.\t\n\t\t"]))),
totalPages: number(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\t\n\t\t\tNumber to display in `of ${totalPages}`, calculated from\t\n\t\t\t`totalPages` and selected page size by default.\t\n\t\t"], ["\t\n\t\t\tNumber to display in \\`of \\${totalPages}\\`, calculated from\t\n\t\t\t\\`totalPages\\` and selected page size by default.\t\n\t\t"]))),
totalCount: number(templateObject_12 || (templateObject_12 = __makeTemplateObject(["\t\n\t\t\tTotal number of items across all pages.\t\n\t\t"], ["\t\n\t\t\tTotal number of items across all pages.\t\n\t\t"]))),
pageSizeOptions: arrayOf(number)(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\t\n\t\t\tArray of numbers representing page size options.\t\n\t\t"], ["\t\n\t\t\tArray of numbers representing page size options.\t\n\t\t"]))),
TextField: shape(TextField_1.default.propTypes)(templateObject_14 || (templateObject_14 = __makeTemplateObject(["\t\n\t\t\tObject of TextField props which are passed thru to the underlying\t\n\t\t\tTextField component.\t\n\t\t"], ["\t\n\t\t\tObject of TextField props which are passed thru to the underlying\t\n\t\t\tTextField component.\t\n\t\t"]))),
onPageSelect: func(templateObject_15 || (templateObject_15 = __makeTemplateObject(["\t\n\t\t\tCalled when a page is selected. Has the signature `(pageIndex,\t\n\t\t\ttotalPages, {props, event}) => {}` where pageIndex is a number.\t\n\t\t"], ["\t\n\t\t\tCalled when a page is selected. Has the signature \\`(pageIndex,\t\n\t\t\ttotalPages, {props, event}) => {}\\` where pageIndex is a number.\t\n\t\t"]))),
onPageSizeSelect: func(templateObject_16 || (templateObject_16 = __makeTemplateObject(["\t\n\t\t\tCalled when a page size is selected. Has the signature `(pageSizeIndex,\t\n\t\t\t{props, event}) => {}` where pageSizeIndex is a number.\t\n\t\t"], ["\t\n\t\t\tCalled when a page size is selected. Has the signature \\`(pageSizeIndex,\t\n\t\t\t{props, event}) => {}\\` where pageSizeIndex is a number.\t\n\t\t"]))),
};
return Paginator;
}(react_1.default.Component));
exports.PaginatorDumb = Paginator;
exports.default = state_management_1.buildModernHybridComponent(Paginator, { reducers: reducers, selectors: Paginator_selectors_1.default });
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14, templateObject_15, templateObject_16;
//# sourceMappingURL=Paginator.js.map