Skip to content

Commit 3ccfa3c

Browse files
nathanstittsilvenon
authored andcommitted
Export functions used to build className for Row & Column (#91)
* export functions used to build className list This will allow other components to act as a Row or Column by sharing the prop building validation * export className computation functions * Document get classname functions * test all symbols are exported as they should be
1 parent c35a8dc commit 3ccfa3c

File tree

7 files changed

+46
-8
lines changed

7 files changed

+46
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ Example
5757
Looking for example to use `react-flexbox-grid`? Head over to [react-flexbox-grid-example](https://github.com/roylee0704/react-flexbox-grid-example).
5858

5959

60+
Advanced composition
61+
-------
62+
Functions for generating Row and Column classNames are exported for use in other components. For example, suppose you have a `MyFormInput` component that should also act as both a `Row` and a `Col`.
63+
64+
```js
65+
import {getRowClassNames, getColClassNames} from 'react-flexbox-grid'
66+
67+
export default function MyFormInput(props) {
68+
return (
69+
<form className={getRowclassNames(props)}>
70+
<input className={getColClassNames(props)} />
71+
</form>
72+
);
73+
}
74+
75+
// Can now be rendered as: <MyFormInput end="sm" sm={8} />
76+
```
77+
78+
79+
6080
Installation
6181
------------
6282

src/components/Col.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function isInteger(value) {
3535
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
3636
}
3737

38-
function getClassNames(props) {
38+
export function getColClassNames(props) {
3939
const extraClasses = [];
4040

4141
if (props.className) {
@@ -61,7 +61,7 @@ function getClassNames(props) {
6161
}
6262

6363
export default function Col(props) {
64-
const classNames = getClassNames(props);
64+
const classNames = getColClassNames(props);
6565

6666
return React.createElement(props.tagName || 'div', createProps(propTypes, props, classNames));
6767
}

src/components/Row.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const propTypes = {
2020
children: PropTypes.node
2121
};
2222

23-
function getClassNames(props) {
23+
export function getRowClassNames(props) {
2424
const modificators = [props.className, getClass('row')];
2525

2626
for (let i = 0; i < rowKeys.length; ++i) {
@@ -39,7 +39,7 @@ function getClassNames(props) {
3939
}
4040

4141
export default function Row(props) {
42-
return React.createElement(props.tagName || 'div', createProps(propTypes, props, getClassNames(props)));
42+
return React.createElement(props.tagName || 'div', createProps(propTypes, props, getRowClassNames(props)));
4343
}
4444

4545
Row.propTypes = propTypes;

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export Grid from './components/Grid';
2-
export Row from './components/Row';
3-
export Col from './components/Col';
2+
export Row, { getRowClassNames } from './components/Row';
3+
export Col, { getColClassNames } from './components/Col';

test/components/Col.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import expect from 'expect';
22
import React from 'react';
33
import TestUtils from 'react-addons-test-utils';
4-
import Col from '../../src/components/Col';
4+
import Col, { getColClassNames } from '../../src/components/Col';
55
import style from 'flexboxgrid';
66

77
const renderer = TestUtils.createRenderer();
@@ -17,6 +17,10 @@ describe('Col', () => {
1717
expect(className).toContain(style['col-lg-4']);
1818
});
1919

20+
it('Computes the classNames', () => {
21+
expect(getColClassNames({ className: 'test', xs: 12 })).toEqual([style['col-xs-12'], 'test']);
22+
});
23+
2024
it('Should add "first-*" class if "first" property is set', () => {
2125
renderer.render(<Col first="md"/>);
2226
expect(renderer.getRenderOutput().props.className).toContain(style['first-md']);

test/components/Row.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import expect from 'expect';
22
import React from 'react';
33
import TestUtils from 'react-addons-test-utils';
4-
import Row from '../../src/components/Row';
4+
import Row, { getRowClassNames } from '../../src/components/Row';
55
import style from 'flexboxgrid';
66

77
const renderer = TestUtils.createRenderer();
@@ -12,6 +12,10 @@ describe('Row', () => {
1212
expect(renderer.getRenderOutput().props.className).toEqual(style.row);
1313
});
1414

15+
it('Computes the classNames', () => {
16+
expect(getRowClassNames({ className: 'test', reverse: true })).toEqual(['test', style.row, style.reverse]);
17+
});
18+
1519
it('Should add "reverse" class if "reverse" property is true', () => {
1620
renderer.render(<Row reverse />);
1721
expect(renderer.getRenderOutput().props.className).toContain(style.reverse);

test/index.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import expect from 'expect';
2+
import * as Exports from '../src/index';
3+
4+
describe('react-flexbox-grid exports', () => {
5+
it('exports all the symbols it should', () => {
6+
['Grid', 'Row', 'Col', 'getColClassNames', 'getRowClassNames'].forEach((prop) => {
7+
expect(Exports.hasOwnProperty(prop)).toBe(true);
8+
});
9+
});
10+
});

0 commit comments

Comments
 (0)