All files / boundless/packages/boundless-pagination index.js

93.75% Statements 75/80
91.23% Branches 52/57
95.83% Functions 23/24
93.59% Lines 73/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521                      518x 98x 1x                                                                                                                                                                             83x       83x   83x                                                                                                                         83x   83x                                                                                                                                                                                                                     894x   25x 99x   103x   71x 71x     28x 20x         12x       12x     12x 3x           9x                     5x 2x     3x             99x 99x 99x 99x 99x   99x 98x                 99x 97x               99x 97x               99x 317x               99x 97x               99x 97x               99x 89x             99x       99x 99x 99x   99x 634x     99x           13x         3x 3x     2x 2x     2x 2x     3x 3x     3x     13x             3x 2x         99x   99x         634x                                         100x 1x     99x                         99x 99x   99x                       99x              
import React, {PropTypes} from 'react';
import {findDOMNode} from 'react-dom';
import cx from 'classnames';
import isInteger from 'lodash.isinteger';
 
import Async from 'boundless-async';
import SegmentedControl from 'boundless-segmented-control';
import ArrowKeyNavigation from 'boundless-arrow-key-navigation';
import omit from 'boundless-utils-omit-keys';
import uuid from 'boundless-utils-uuid';
 
const identity = (x) => x;
const isFunction = (x) => typeof x === 'function';
const noop = () => {};
 
/**
 * __View and navigate heterogenious content one page at a time.__
 *
 * Pagination is implemented as an encapsulated view system, accepting an array of items as input.
 *
 * ## Component Instance Methods
 *
 * When using `Pagination` in your project, you may call the following methods on a rendered
 * instance of the component. Use [`refs`](https:// * facebook.github.io/react/docs/refs-and-the-dom.html)
 * to get the instance.
 *
 * - __`currentPage()`__ returns the ___one___-indexed page number currently in view
 *
 * - __`jumpToIndex(index: number)`__ renders the page that contains the ___zero___-indexed item
 */
export default class Pagination extends React.PureComponent {
    static control = {
        CUSTOM: uuid(),
        FIRST: uuid(),
        LAST: uuid(),
        NEXT: uuid(),
        PREVIOUS: uuid(),
    }
 
    static position = {
        ABOVE: uuid(),
        BELOW: uuid(),
        BOTH: uuid(),
    }
 
    static CONTROL_DATA_ATTRIBUTE = 'data-page-control'
 
    static propTypes = {
        /**
         * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
         */
        '*': PropTypes.any,
 
        /**
         * arbitrary content to be rendered after the items in the DOM
         */
        after: PropTypes.node,
 
        /**
         * arbitrary content to be rendered before the items in the DOM
         */
        before: PropTypes.node,
 
        controlWrapperProps: PropTypes.shape({
            /**
             * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
             */
            '*': PropTypes.any,
        }),
 
        /**
         * allows for arbitrary content to be rendered into the control area
         */
        customControlContent: PropTypes.node,
 
        /**
         * called with a desired item index when that item comes into view;
         * accepts a `Promise` if you need to fetch the row asynchronously
         */
        getItem: PropTypes.func.isRequired,
 
        /**
         * does not render the paging controls if the number of items supplied
         * to the view is less-than-or-equal-to the number of items to show
         * per page via `props.numItemsPerPage`
         */
        hidePagerIfNotNeeded: PropTypes.bool,
 
        /**
         * a unique name for the data source being consumed; pass a
         * different name to cause the view to fully reset and pull fresh data
         */
        identifier: PropTypes.string.isRequired,
 
        /**
         * the (__one-indexed__) number of the page that should be initially
         * displayed; must be a positive integer less than or equal to
         * the total number of pages
         */
        initialPage: function validateInitialPage(props) {
            Iif (isInteger(props.initialPage) === false) {
                return new Error('`initialPage` must be an integer.');
            }
 
            const numberOfPages = Math.ceil(props.totalItems / props.numItemsPerPage);
 
            Iif (props.initialPage < 1 || props.initialPage > numberOfPages) {
                return new Error('`initialPage` must be between 1 and ' + numberOfPages + '.');
            }
        },
 
        /**
         * allows for arbitrary content to be rendered into pagination items
         * as they're loading if the backing data is a `Promise`
         */
        itemLoadingContent: PropTypes.node,
 
        /**
         * an function to specify how an item should be converted
         * to JSX, if it is not already renderable by React
         *
         * ```jsx
         *
         * const getItem = () => ({id: 1234, text: 'foo'});
         * const objToJSX = ({id, text}) => <div data-id={id}>{text}</div>;
         *
         * <Pagination
         *     getItem={getItem}
         *     identifer='foo'
         *     itemToJSXConverter={objToJSX}
         *     totalItems={1} />
         * ```
         */
        itemToJSXConverter: PropTypes.func,
 
        itemWrapperProps: PropTypes.shape({
            /**
             * any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
             */
            '*': PropTypes.any,
        }),
 
        /**
         * content to be displayed inside of the "First page" control button
         */
        jumpToFirstPageControlContent: PropTypes.node,
 
        /**
         * content to be displayed inside of the "Last page" control button
         */
        jumpToLastPageControlContent: PropTypes.node,
 
        /**
         * content to be displayed inside of the "Next page" control button
         */
        jumpToNextPageControlContent: PropTypes.node,
 
        /**
         * content to be displayed inside of the "Previous page" control button
         */
        jumpToPreviousPageControlContent: PropTypes.node,
 
        /**
         * the maximum number of items to be displayed on each page; must be
         * greater than zero
         */
        numItemsPerPage: function validateNumItemsPerPage(props) {
            Iif (isInteger(props.numItemsPerPage) === false) {
                return new Error('`numItemsPerPage` must be an integer.');
            } else Iif (props.numItemsPerPage < 1) {
                return new Error('`numItemsPerPage` must be greater than zero.');
            }
        },
 
        /**
         * the maximum number of pages to be displayed in the control bar at
         * one time
         */
        numPageToggles: PropTypes.number,
 
        /**
         * determines whether the pagination controls are displayed above,
         * below, or both above and below the content
         */
        position: PropTypes.oneOf([
            Pagination.position.ABOVE,
            Pagination.position.BELOW,
            Pagination.position.BOTH,
        ]),
 
        /**
         * whether the "first page" control button should be displayed
         */
        showJumpToFirstPageControl: PropTypes.bool,
 
        /**
         * whether the "last page" control button should be displayed
         */
        showJumpToLastPageControl: PropTypes.bool,
 
        /**
         * whether the "next page" control button should be displayed
         */
        showJumpToNextPageControl: PropTypes.bool,
 
        /**
         * whether the "previous page" control button should be displayed
         */
        showJumpToPreviousPageControl: PropTypes.bool,
 
        /**
         * renders an element called `.b-pagination-control-state` that
         * contains the current state of the pagination like "1 of 10";
         * alternatively, this prop also accepts a function that it will
         * call with the currentPage and totalPages for you to format:
 
         * ```jsx
         * showPaginatedState={
         *     (currentPage, totalPages) => (
         *         <div className='foo'>
         *             You're on page {currentPage} of {totalPages} pages!
         *         </div>
         *     )
         * }
         * ```
         */
        showPaginationState: PropTypes.oneOfType([
            PropTypes.bool,
            PropTypes.func,
        ]),
 
        /**
         * the total number of items to be displayed in the view
         */
        totalItems: PropTypes.number.isRequired,
    }
 
    static defaultProps = {
        after: null,
        before: null,
        controlWrapperProps: {},
        customControlContent: null,
        getItem: noop,
        hidePagerIfNotNeeded: false,
        identifier: uuid(),
        initialPage: 1,
        itemLoadingContent: undefined,
        itemToJSXConverter: identity,
        itemWrapperProps: {},
        jumpToFirstPageControlContent: '⇤',
        jumpToLastPageControlContent: '⇥',
        jumpToNextPageControlContent: '→',
        jumpToPreviousPageControlContent: '←',
        numItemsPerPage: 10,
        numPageToggles: 5,
        position: Pagination.position.ABOVE,
        showJumpToFirstPageControl: true,
        showJumpToLastPageControl: true,
        showJumpToNextPageControl: true,
        showJumpToPreviousPageControl: true,
        showPaginationState: true,
        totalItems: null,
    }
 
    static internalKeys = Object.keys(Pagination.defaultProps)
 
    mounted = false
 
    state = {
        currentPage: this.props.initialPage,
        targetIndex: (this.props.initialPage - 1) * this.props.numItemsPerPage,
    }
 
    /**
     * @public
     */
    currentPage = () => this.state.currentPage
 
    getPageForIndex = (index, itemsPerPage = this.props.numItemsPerPage) => Math.ceil((index + 1) / itemsPerPage)
    totalPages = () => Math.ceil(this.props.totalItems / this.props.numItemsPerPage)
 
    firstVisibleItemIndex = () => (this.currentPage() - 1) * this.props.numItemsPerPage
 
    componentDidMount()     { this.mounted = true; }
    componentWillUnmount()  { this.mounted = false; }
 
    componentDidUpdate(_, prevState) {
        if (prevState.currentPage !== this.currentPage()) {
            findDOMNode(this.refs.item_0).focus();
        }
    }
 
    componentWillReceiveProps() {
        const oldProps = this.props;
 
        // use transactional `setState()` syntax to ensure that pending state updates are honored,
        // like those from `jumpToIndex()`
        this.setState((state, props) => {
            // NOTE: `props` here is technically the `nextProps` you'd receive from the first cWRP argument
            // so that's why we're caching `oldProps` outside the `setState`
            if (props.identifier !== oldProps.identifier) {
                return {
                    currentPage: 1,
                    targetIndex: 0,
                };
            }
 
            return {
                currentPage: this.getPageForIndex(state.targetIndex, props.numItemsPerPage),
                targetIndex: state.targetIndex,
            };
        });
    }
 
    /**
     * @public
     */
    jumpToIndex = (i) => {
        if (i < 0 || i >= this.props.totalItems) {
            throw Error(`Cannot page to invalid index ${i}.`);
        }
 
        this.setState({
            currentPage: this.getPageForIndex(i),
            targetIndex: i,
        });
    }
 
    generateControls() {
        const options = [];
        const currentPage = this.currentPage();
        const totalPages = this.totalPages();
        const startPage = currentPage - ((currentPage - 1) % this.props.numPageToggles);
        const endPage = Math.min(startPage + this.props.numPageToggles - 1, totalPages);
 
        if (this.props.showPaginationState) {
            options.push({
                children: isFunction(this.props.showPaginationState)
                          ? this.props.showPaginationState(currentPage, totalPages)
                          : `${currentPage} / ${totalPages}`,
                className: 'b-pagination-control b-pagination-control-state',
                disabled: true,
            });
        }
 
        if (this.props.showJumpToFirstPageControl) {
            options.push({
                [Pagination.CONTROL_DATA_ATTRIBUTE]: Pagination.control.FIRST,
                children: this.props.jumpToFirstPageControlContent,
                className: 'b-pagination-control b-pagination-control-first',
                disabled: currentPage === 1,
            });
        }
 
        if (this.props.showJumpToPreviousPageControl) {
            options.push({
                [Pagination.CONTROL_DATA_ATTRIBUTE]: Pagination.control.PREVIOUS,
                children: this.props.jumpToPreviousPageControlContent,
                className: 'b-pagination-control b-pagination-control-previous',
                disabled: currentPage === 1,
            });
        }
 
        for (let i = startPage; i <= endPage; i++) {
            options.push({
                [Pagination.CONTROL_DATA_ATTRIBUTE]: i,
                children: i,
                className: 'b-pagination-control',
                pressed: currentPage === i,
            });
        }
 
        if (this.props.showJumpToNextPageControl) {
            options.push({
                [Pagination.CONTROL_DATA_ATTRIBUTE]: Pagination.control.NEXT,
                children: this.props.jumpToNextPageControlContent,
                className: 'b-pagination-control b-pagination-control-next',
                disabled: currentPage === totalPages,
            });
        }
 
        if (this.props.showJumpToLastPageControl) {
            options.push({
                [Pagination.CONTROL_DATA_ATTRIBUTE]: Pagination.control.LAST,
                children: this.props.jumpToLastPageControlContent,
                className: 'b-pagination-control b-pagination-control-last',
                disabled: currentPage === totalPages,
            });
        }
 
        if (this.props.customControlContent) {
            options.push({
                children: this.props.customControlContent,
                className: 'b-pagination-control b-pagination-control-custom',
                disabled: true,
            });
        }
 
        return options;
    }
 
    generateItems() {
        const items = [];
        const firstIndex = this.firstVisibleItemIndex();
        const lastIndex = Math.min(this.props.totalItems, firstIndex + this.props.numItemsPerPage) - 1;
 
        for (let i = firstIndex; i <= lastIndex; i += 1) {
            items.push(this.props.getItem(i));
        }
 
        return items;
    }
 
    handlePageSelected = (option) => {
        let nextTargetIndex;
 
        switch (option[Pagination.CONTROL_DATA_ATTRIBUTE]) {
        case undefined:
            return;
 
        case Pagination.control.FIRST:
            nextTargetIndex = 0;
            break;
 
        case Pagination.control.PREVIOUS:
            nextTargetIndex = this.firstVisibleItemIndex() - this.props.numItemsPerPage;
            break;
 
        case Pagination.control.NEXT:
            nextTargetIndex = this.firstVisibleItemIndex() + this.props.numItemsPerPage;
            break;
 
        case Pagination.control.LAST:
            nextTargetIndex = this.props.totalItems - 1;
            break;
 
        default:
            nextTargetIndex = parseInt(option[Pagination.CONTROL_DATA_ATTRIBUTE], 10) * this.props.numItemsPerPage - 1;
        }
 
        this.setState({
            currentPage: this.getPageForIndex(nextTargetIndex),
            targetIndex: nextTargetIndex,
        });
    }
 
    handleItemPromiseFulfillment = (payload) => {
        if (this.mounted) {
            return this.props.itemToJSXConverter(payload);
        }
    }
 
    renderItems() {
        const props = this.props.itemWrapperProps;
 
        return (
            <ArrowKeyNavigation
                {...props}
                className={cx('b-pagination-items', props.className)}>
                {this.generateItems().map((item, index) => {
                    return (
                        <Async
                            ref={index === 0 ? 'item_0' : null}
                            key={index}
                            className={cx('b-pagination-item', {
                                'b-pagination-item-even': index % 2 === 0,
                                'b-pagination-item-odd': index % 2 !== 0,
                            })}
                            data-pagination-index={this.props.numItemsPerPage * (this.currentPage() - 1) + index}
                            pendingContent={this.props.itemLoadingContent}>
                            {item instanceof Promise
                             ? item.then(this.handleItemPromiseFulfillment, this.handleItemPromiseFulfillment)
                             : this.props.itemToJSXConverter(item)}
                        </Async>
                    );
                })}
            </ArrowKeyNavigation>
        );
    }
 
    renderControls(position) {
        if (this.props.hidePagerIfNotNeeded && this.props.totalItems <= this.props.numItemsPerPage) {
            return;
        }
 
        return (
            <SegmentedControl
                {...this.props.controlWrapperProps}
                className={cx('b-pagination-controls', this.props.controlWrapperProps.className, {
                    ['b-pagination-controls-above']: position === Pagination.position.ABOVE,
                    ['b-pagination-controls-below']: position === Pagination.position.BELOW,
                })}
                options={this.generateControls()}
                onOptionSelected={this.handlePageSelected} />
        );
    }
 
    renderView() {
        const {position} = this.props;
        const p = Pagination.position;
 
        return (
            <div className='b-pagination'>
                {position === p.ABOVE || position === p.BOTH ? this.renderControls(p.ABOVE) : null}
                {this.props.before}
                {this.renderItems()}
                {this.props.after}
                {position === p.BELOW || position === p.BOTH ? this.renderControls(p.BELOW) : null}
            </div>
        );
    }
 
    render() {
        return (
            <div {...omit(this.props, Pagination.internalKeys)} className={cx('b-pagination-wrapper', this.props.className)}>
                {this.renderView()}
            </div>
        );
    }
}