Skip to content

Commit c35a8dc

Browse files
nathanstittsilvenon
authored andcommitted
Lookup classnames using with customizable function (#90)
* Lookup classnames using lookup function This will allow implications to not use css modules. * Fallback to returning class name if it's not found in styles * Test styles exist; remove lookup function * remove unused imports
1 parent 4dc4913 commit c35a8dc

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

src/classNames.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import styles from 'flexboxgrid';
2+
3+
export default function getClass(className) {
4+
return (styles && styles[className]) ? styles[className] : className;
5+
}

src/components/Col.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import style from 'flexboxgrid';
21
import React, { PropTypes } from 'react';
32
import createProps from '../createProps';
3+
import getClass from '../classNames';
44
import { ColumnSizeType, ViewportSizeType } from '../types';
55

66
const propTypes = {
@@ -43,20 +43,20 @@ function getClassNames(props) {
4343
}
4444

4545
if (props.first) {
46-
extraClasses.push(style['first-' + props.first]);
46+
extraClasses.push(getClass('first-' + props.first));
4747
}
4848

4949
if (props.last) {
50-
extraClasses.push(style['last-' + props.last]);
50+
extraClasses.push(getClass('last-' + props.last));
5151
}
5252

5353
if (props.reverse) {
54-
extraClasses.push(style.reverse);
54+
extraClasses.push(getClass('reverse'));
5555
}
5656

5757
return Object.keys(props)
5858
.filter(key => classMap[key])
59-
.map(key => style[isInteger(props[key]) ? (classMap[key] + '-' + props[key]) : classMap[key]])
59+
.map(key => getClass(isInteger(props[key]) ? (classMap[key] + '-' + props[key]) : classMap[key]))
6060
.concat(extraClasses);
6161
}
6262

src/components/Grid.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { PropTypes } from 'react';
22
import createProps from '../createProps';
3-
import style from 'flexboxgrid';
3+
import getClass from '../classNames';
44

55
const propTypes = {
66
fluid: PropTypes.bool,
@@ -10,7 +10,7 @@ const propTypes = {
1010
};
1111

1212
export default function Grid(props) {
13-
const containerClass = style[props.fluid ? 'container-fluid' : 'container'];
13+
const containerClass = getClass(props.fluid ? 'container-fluid' : 'container');
1414
const classNames = [props.className, containerClass];
1515

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

src/components/Row.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import style from 'flexboxgrid';
1+
import getClass from '../classNames';
22
import React, { PropTypes } from 'react';
33
import createProps from '../createProps';
44
import { ViewportSizeType } from '../types';
@@ -21,18 +21,18 @@ const propTypes = {
2121
};
2222

2323
function getClassNames(props) {
24-
const modificators = [props.className, style.row];
24+
const modificators = [props.className, getClass('row')];
2525

2626
for (let i = 0; i < rowKeys.length; ++i) {
2727
const key = rowKeys[i];
2828
const value = props[key];
2929
if (value) {
30-
modificators.push(style[`${key}-${value}`]);
30+
modificators.push(getClass(`${key}-${value}`));
3131
}
3232
}
3333

3434
if (props.reverse) {
35-
modificators.push(style.reverse);
35+
modificators.push(getClass('reverse'));
3636
}
3737

3838
return modificators;

test/classNames.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import expect from 'expect';
2+
import style from 'flexboxgrid';
3+
import getClass from '../src/classNames';
4+
5+
describe('classNames lookups', () => {
6+
it('translates known styles', () => {
7+
expect(getClass('col-xs')).toEqual(style['col-xs']);
8+
});
9+
it('falls back to returning unknown classnames', () => {
10+
expect(getClass('blah-blah-blah')).toEqual('blah-blah-blah');
11+
});
12+
});

0 commit comments

Comments
 (0)