All files / src/grid Row.jsx

100% Statements 30/30
86.67% Branches 26/30
100% Functions 15/15
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                        201x                             201x                                         2484x   2484x 2484x                   2484x 2484x                   2283x 2283x                         24x 12x   12x                                             2496x 2496x   2496x 2496x   2496x 12x       2496x                                       2484x   201x           2496x 14976x             2496x       2496x 684x                 2496x       2496x       2496x            
/* eslint-disable react/boolean-prop-naming */
import React, {Component, forwardRef} from 'react';
 
import {autobind} from 'core-decorators';
import PropTypes from 'prop-types';
import styled, {css} from 'styled-components';
 
import {DIMENSIONS} from '../defaultConfig';
import {HALF} from '../utils/constants';
import {getConfig, media} from '../utils/lib';
import {calcAlign, calcDirection, calcJustify} from '../utils/rowHelpers';
 
/**
 * Row
 *
 * @component
 * @augments {Component<Props, State>}
 * @extends {Component}
 */
class Row extends Component {
    static propTypes = {
        children: PropTypes.node.isRequired,
        align: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        as: PropTypes.string,
        className: PropTypes.string,
        direction: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
        justify: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        noWrap: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
        order: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
        reverse: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
        /** TestID for cypress testing  */
        testId: PropTypes.string
    }
 
    static defaultProps = {
        align: null,
        as: null,
        className: null,
        direction: 'row',
        innerRef: null,
        justify: null,
        noWrap: null,
        order: null,
        reverse: false,
        testId: null
    }
 
    /**
     * Creates an instance of Row.
     *
     * @param {Object} props Component props.
     *
     * @memberof Row
     */
    constructor(props) {
        super(props);
 
        Eif (process.env.NODE_ENV !== 'production') {
            this.state = {debug: false};
        }
    }
 
    /**
     * Sets an event listener to toggle debug mode.
     *
     * @memberof Row
     */
    componentDidMount() {
        Eif (process.env.NODE_ENV !== 'production') {
            window.addEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Unsets an event listener to toggle debug mode.
     *
     * @memberof Row
     */
    componentWillUnmount() {
        Eif (process.env.NODE_ENV !== 'production') {
            window.removeEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Sets the debug state.
     *
     * @param {KeyboardEvent} e The keyboard event.
     *
     * @memberof Row
     */
    @autobind
    toggleDebug(e) {
        if (e.ctrlKey && e.code === 'KeyD') {
            e.preventDefault();
            // eslint-disable-next-line no-invalid-this
            this.setState(oldState => ({debug: !oldState.debug}));
        }
    }
 
    /**
     * Renders the Component.
     *
     * @returns {JSX} Component.
     * @memberof Row
     */
    render() {
        const {
            align,
            as,
            children,
            className,
            direction,
            innerRef,
            justify,
            noWrap,
            order,
            reverse,
            testId
        } = this.props;
        const classNames = [className];
 
        Eif (process.env.NODE_ENV !== 'production') {
            const {debug} = this.state;
 
            if (debug) {
                classNames.push('debug');
            }
        }
 
        return (
            <RowElement
                ref={innerRef}
                align={align}
                as={as}
                className={classNames.join(' ')}
                data-cy={testId}
                direction={direction}
                justify={justify}
                noWrap={noWrap}
                order={order}
                reverse={reverse}
            >
                {children}
            </RowElement>
        );
    }
}
 
// eslint-disable-next-line react/jsx-props-no-spreading
export default forwardRef((props, ref) => <Row {...props} innerRef={ref} />);
 
const RowElement = styled.div`
    box-sizing: border-box;
    display: flex;
    flex: 1 1 auto;
    flex-wrap: wrap;
 
    ${({theme}) => css`
        ${DIMENSIONS.map(screenSize => getConfig(theme).container[String(screenSize)] && media(theme, screenSize)`
            margin-left: -${getConfig(theme).gutterWidth[String(screenSize)] / HALF}px;
            margin-right: -${getConfig(theme).gutterWidth[String(screenSize)] / HALF}px;
        `)}
    `}
 
 
    ${({direction, noWrap, reverse, theme}) => css`
        ${calcDirection(direction, noWrap, reverse, theme)}
    `}
 
    ${({order, theme}) => order !== null && css`
        ${DIMENSIONS.map(screenSize => typeof getConfig(theme).breakpoints[String(screenSize)] === 'number' && media(theme, screenSize)`
            ${typeof order === 'object'
        // eslint-disable-next-line indent
                ? typeof order[String(screenSize)] !== 'undefined' && `order: ${order[String(screenSize)]};`
        // eslint-disable-next-line indent
                : `order: ${order};`}
        `)}
    `}
 
    ${({align, theme}) => align && css`
        ${calcAlign(align, theme)}
    `}
 
    ${({justify, theme}) => justify && css`
        ${calcJustify(justify, theme)}
    `}
 
    ${({theme}) => process.env.NODE_ENV !== 'production' && css`
        &.debug {
            background-color: ${getConfig(theme).debug.row.background};
            outline: ${getConfig(theme).debug.row.outline} solid 1px;
        }
    `}
`;