All files / components/Paginator Paginator.jsx

92.31% Statements 12/13
87.5% Branches 7/8
100% Functions 1/1
92.31% Lines 12/13
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                      120x                   120x   120x                 120x                                                                                                                                     121x                                         2x 2x 2x     2x                                 23x   23x                           20x                                                                    
import React from 'react';
import _ from 'lodash';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass } from '../../util/component-types';
import * as reducers from './Paginator.reducers';
import selectors from './Paginator.selectors';
import SingleSelect from '../SingleSelect/SingleSelect';
import TextField from '../TextField/TextField';
import Button from '../Button/Button';
import ArrowIcon from '../Icon/ArrowIcon/ArrowIcon';
 
const cx = lucidClassNames.bind('&-Paginator');
 
const {
	arrayOf,
	bool,
	func,
	number,
	object,
	shape,
	string,
} = React.PropTypes;
 
const { Option } = SingleSelect;
 
/**
 *
 * {"categories": ["navigation"], "madeFrom": ["ArrowIcon", "TextField", "Button", "SingleSelect"]}
 *
 * A paginator with page size selector.
 */
 
const Paginator = createClass({
	displayName: 'Paginator',
 
	reducers,
 
	selectors,
 
	propTypes: {
		/**
		 * Appended to the component-specific class names set on the root elements.
		 */
		className: string,
		/**
		 * Styles that are passed through to root element.
		 */
		style: object,
		/**
		 * Disables the Paginator from being clicked or focused.
		 */
		isDisabled: bool,
		/**
		 * Whether to show the page size selector. Defaults to false.
		 */
		hasPageSizeSelector: bool,
		/**
		 * 0-indexed currently selected page number
		 */
		selectedPageIndex: number,
		/**
		 * currently selected page size option index
		 */
		selectedPageSizeIndex: number,
		/**
		 * Object of SingleSelect props which are passed thru to the underlying
		 * SingleSelect component for the page size selector.
		 */
		SingleSelect: shape(SingleSelect.propTypes),
		/**
		 * number to display in `of ${totalPages}`, calculated from `totalPages` and
		 * selected page size by default.
		 */
		totalPages: number,
		/**
		 * total number of items across all pages
		 */
		totalCount: number,
		/**
		 * array of numbers representing page size options
		 */
		pageSizeOptions: arrayOf(number),
		/**
		 * Object of TextField props which are passed thru to the underlying TextField component.
		 */
		TextField: shape(TextField.propTypes),
		/**
		 * Called when a page is selected.
		 * Has the signature `(pageIndex, totalPages, {props, event}) => {}` where pageIndex is a number.
		 */
		onPageSelect: func,
		/**
		 * Called when a page size is selected.
		 * Has the signature `(pageSizeIndex, {props, event}) => {}` where pageSizeIndex is a number.
		 */
		onPageSizeSelect: func,
	},
 
	getDefaultProps() {
		return {
			hasPageSizeSelector: false,
			isDisabled: false,
			onPageSelect: _.noop,
			selectedPageIndex: 0,
			selectedPageSizeIndex: 0,
			totalCount: null,
			pageSizeOptions: [10, 50, 100],
			SingleSelect: {
				...SingleSelect.getDefaultProps(),
				selectedIndex: 0,
			},
			TextField: TextField.getDefaultProps(),
		};
	},
 
	handleTextFieldChange(pageNum, {props, event}) {
		const {
			onPageSelect,
			selectedPageIndex,
			totalPages,
		} = this.props;
		const parsedPageNum = _.parseInt(pageNum);
		Iif (_.isNaN(parsedPageNum)) {
			return onPageSelect(selectedPageIndex, totalPages, {props, event});
		}
		return onPageSelect(parsedPageNum - 1, totalPages, {props, event});
	},
 
	render() {
		const {
			className,
			hasPageSizeSelector,
			isDisabled,
			onPageSelect,
			onPageSizeSelect,
			pageSizeOptions,
			selectedPageIndex,
			selectedPageSizeIndex,
			totalPages,
			style,
			SingleSelect: singleSelectProps,
			TextField: textFieldProps,
		} = this.props;
 
		return (
			<div style={style} className={cx('&', className)}>
 
				{ hasPageSizeSelector ? (
					<div className={cx('&-page-size-container')}>
						<span className={cx('&-rows-per-page-label')}>Rows per page:</span>
						<SingleSelect
							{...singleSelectProps}
							hasReset={false}
							isSelectionHighlighted={false}
							isDisabled={isDisabled}
							selectedIndex={selectedPageSizeIndex}
							onSelect={onPageSizeSelect}
						>
							{_.map(pageSizeOptions, option => <Option key={option}>{option}</Option>)}
						</SingleSelect>
					</div>
				) : null }
 
				<Button
					onClick={_.partial(onPageSelect, selectedPageIndex - 1, totalPages)}
					isDisabled={isDisabled || selectedPageIndex === 0}
					hasOnlyIcon
				>
					<ArrowIcon direction='left'/>
				</Button>
				<TextField
					lazyLevel={100}
					{...textFieldProps}
					onBlur={this.handleTextFieldChange}
					onSubmit={this.handleTextFieldChange}
					isDisabled={isDisabled}
					value={selectedPageIndex + 1}
				/>
				<span>of {totalPages}</span>
				<Button
					onClick={_.partial(onPageSelect, selectedPageIndex + 1, totalPages)}
					isDisabled={isDisabled || selectedPageIndex === totalPages - 1}
					hasOnlyIcon
				>
					<ArrowIcon direction='right'/>
				</Button>
			</div>
		);
	},
});
 
export default Paginator;