Skip to content

Commit

Permalink
feat(Row): Add row columns support (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang authored and TheSharpieOne committed Dec 3, 2019
1 parent 284a543 commit 1fb3c17
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
19 changes: 18 additions & 1 deletion docs/lib/Components/LayoutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { PrismCode } from 'react-prism';
import PageTitle from '../UI/PageTitle';
import SectionTitle from '../UI/SectionTitle';
import LayoutExample from '../examples/Layout';
import LayoutRowColsExample from '../examples/LayoutRowCols';

const LayoutExampleSource = require('!!raw-loader!../examples/Layout');
const LayoutRowColsExampleSource = require('!!raw-loader!../examples/LayoutRowCols');

export default class LayoutsPage extends React.Component {
render() {
Expand Down Expand Up @@ -34,7 +37,12 @@ export default class LayoutsPage extends React.Component {
{`Row.propTypes = {
noGutters: PropTypes.bool,
// see https://reactstrap.github.io/components/form Form Grid with Form Row
form: PropTypes.bool
form: PropTypes.bool,
xs: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
sm: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
md: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
lg: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
xl: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
}`}
</PrismCode>
</pre>
Expand Down Expand Up @@ -69,6 +77,15 @@ Col.propTypes = {
}`}
</PrismCode>
</pre>
<h4>Row Columns</h4>
<div className="docs-example">
<LayoutRowColsExample />
</div>
<pre>
<PrismCode className="language-jsx">
{LayoutRowColsExampleSource}
</PrismCode>
</pre>
</div>
);
}
Expand Down
41 changes: 41 additions & 0 deletions docs/lib/examples/LayoutRowCols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { Container, Row, Col } from 'reactstrap';

const Example = (props) => {
return (
<Container>
<Row xs="2">
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
</Row>
<Row xs="3">
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
</Row>
<Row xs="4">
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
</Row>
<Row xs="4">
<Col>Column</Col>
<Col>Column</Col>
<Col xs="6">Column</Col>
<Col>Column</Col>
</Row>
<Row xs="1" sm="2" md="4">
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
<Col>Column</Col>
</Row>
</Container>
);
}

export default Example;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"npm-to-cdn-bot (by Forbes Lindesay) (https://github.com/npmcdn-to-unpkg-bot)",
"polmauri (https://github.com/polmauri)",
"Steven Scaffidi (https://github.com/sscaff1)",
"Brady Pascoe (https://github.com/bpas247)"
"Brady Pascoe (https://github.com/bpas247)",
"Kyle Tsang (https://github.com/kyletsang)"
],
"license": "MIT",
"bugs": {
Expand Down
32 changes: 29 additions & 3 deletions src/Row.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, tagPropType } from './utils';

const rowColWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
const rowColsPropType = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);

const propTypes = {
tag: tagPropType,
noGutters: PropTypes.bool,
className: PropTypes.string,
cssModule: PropTypes.object,
form: PropTypes.bool
form: PropTypes.bool,
xs: rowColsPropType,
sm: rowColsPropType,
md: rowColsPropType,
lg: rowColsPropType,
xl: rowColsPropType
};

const defaultProps = {
tag: 'div'
tag: 'div',
widths: rowColWidths
};

const Row = (props) => {
Expand All @@ -22,13 +31,30 @@ const Row = (props) => {
noGutters,
tag: Tag,
form,
widths,
...attributes
} = props;

const colClasses = [];

widths.forEach((colWidth, i) => {
let colSize = props[colWidth];

delete attributes[colWidth];

if (!colSize) {
return;
}

const isXs = !i;
colClasses.push(isXs ? `row-cols-${colSize}` : `row-cols-${colWidth}-${colSize}`);
});

const classes = mapToCssModules(classNames(
className,
noGutters ? 'no-gutters' : null,
form ? 'form-row' : 'row'
form ? 'form-row' : 'row',
colClasses
), cssModule);

return (
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/Row.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ describe('Row', () => {

expect(wrapper.html()).toBe('<div class="form-row"></div>');
});

it('should pass row col size specific classes as strings', () => {
const wrapper = shallow(<Row sm="6" />);

expect(wrapper.hasClass('row-cols-sm-6')).toBe(true);
});

it('should pass row col size specific classes as numbers', () => {
const wrapper = shallow(<Row sm={6} />);

expect(wrapper.hasClass('row-cols-sm-6')).toBe(true);
});
});

0 comments on commit 1fb3c17

Please sign in to comment.