All files / components/Grid Grid.jsx

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7
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          120x           120x                 120x                                                                                                                                                                                                                                                         21x   21x   21x                   97x                                                                      
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, findTypes, omitProps }  from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Grid');
 
const {
	string,
	bool,
	node,
} = React.PropTypes;
 
/**
 * {"categories": ["layout"]}
 *
 * This component is designed to be used in Composits as a layout tool.
 * You can use the Grid components themselves or create your own components using the Grid styles from Grid.less.
 * Please see examples for more information.
 */
const Grid = createClass({
	displayName: 'Grid',
 
	components: {
		/**
		 * Renders an `<article>` as the grid cell
		 */
		Cell: createClass({
			displayName: 'Grid.Cell',
			propTypes: {
				/**
				 * fill all twelve columns of the primary grid axis
				 */
				isFull: bool,
				/**
				 * fill six columns of the primary grid axis
				 */
				isHalf: bool,
				/**
				 * fill four columns of the primary grid axis
				 */
				isThird: bool,
				/**
				 * fill three columns of the primary grid axis
				 */
				isQuarter: bool,
 
				/**
				 * fill 2 columns of 12
				 */
				is2: bool,
				/**
				 *  fill 3 columns of 12
				 */
				is3: bool,
				/**
				 * fill 4 columns of 12
				 */
				is4: bool,
				/**
				 * fill 5 columns of 12
				 */
				is5: bool,
				/**
				 * fill 6 columns of 12
				 */
				is6: bool,
				/**
				 * fill 7 columns of 12
				 */
				is7: bool,
				/**
				 * fill 8 columns of 12
				 */
				is8: bool,
				/**
				 * fill 9 columns of 12
				 */
				is9: bool,
				/**
				 * fill 10 columns of 12
				 */
				is10: bool,
				/**
				 * fill 11 columns of 12
				 */
				is11: bool,
				/**
				 * offset a grid cell by three columns
				 */
				isOffsetQuarter: bool,
				/**
				 * offset a grid cell by four columns
				 */
				isOffsetThird: bool,
				/**
				 * offset a grid cell by six columns
				 */
				isOffsetHalf: bool,
			},
		}),
	},
 
	propTypes: {
		/**
		 * Appended to the component-specific class names set on the root
		 * element.
		 */
		className: string,
 
		/**
		 * explicitly set the primary axis of the grid to Y
		 */
		isVertical: bool,
 
		/**
		 * explicitly set the primary axis of the grid to X
		 */
		isHorizontal: bool,
 
		/**
		 * a grid without padding separating grid cells
		 */
		isGutterless: bool,
 
		/**
		 * Allow Grids to wrap multiple lines
		 */
		isMultiline: bool,
 
		/**
		 * Any valid React component
		 */
		children: node,
	},
 
	render() {
		const {
			className,
			children,
			isVertical,
			isHorizontal,
			isGutterless,
			isMultiline,
			...passThroughs,
		} = this.props;
 
		const cellChildProps = _.map(findTypes(this.props, Grid.Cell), 'props');
 
		return (
			<section {...omitProps(passThroughs, Grid)}
				className={cx('&', {
					'&-is-vertical': isVertical,
					'&-is-horizontal': isHorizontal,
					'&-is-gutterless': isGutterless,
					'&-is-multiline': isMultiline,
				}, className)}
			>
				{_.map(cellChildProps, (cellChildProp, index) => {
					return (
						<article
							{...omitProps(cellChildProp, Grid.Cell)}
							key={index}
							className={cx('&-Cell', {
									'&-Cell-is-full': cellChildProp.isFull,
									'&-Cell-is-half': cellChildProp.isHalf,
									'&-Cell-is-quarter': cellChildProp.isQuarter,
									'&-Cell-is-third': cellChildProp.isThird,
									'&-Cell-is-2': cellChildProp.is2,
									'&-Cell-is-3': cellChildProp.is3,
									'&-Cell-is-4': cellChildProp.is4,
									'&-Cell-is-5': cellChildProp.is5,
									'&-Cell-is-6': cellChildProp.is6,
									'&-Cell-is-7': cellChildProp.is7,
									'&-Cell-is-8': cellChildProp.is8,
									'&-Cell-is-9': cellChildProp.is9,
									'&-Cell-is-10': cellChildProp.is10,
									'&-Cell-is-11': cellChildProp.is11,
									'&-Cell-is-offset-quarter': cellChildProp.isOffsetQuarter,
									'&-Cell-is-offset-third': cellChildProp.isOffsetThird,
									'&-Cell-is-offset-half': cellChildProp.isOffsetHalf,
								}, cellChildProp.className)}
							>
							{cellChildProp.children}
						</article>
					);
				})}
				{children}
			</section>
		);
	},
});
 
export default Grid;