Skip to content

Commit ebdecb8

Browse files
ronnyhaaseeddywashere
authored andcommitted
feat(Col): add flex-support to Col component (#169) (#175)
* feat(Col): add flex-support for Col component (#169) * feat(Col): Removed "flex" as valid option for col size, and bool for object (#169) * feat(Col): add documentation for flexbox support (#169)
1 parent ad890d7 commit ebdecb8

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

docs/lib/Components/LayoutPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export default class LayoutsPage extends React.Component {
4141
const columnProps = PropTypes.oneOfType([
4242
PropTypes.string,
4343
PropTypes.number,
44+
PropTypes.bool,
4445
PropTypes.shape({
4546
size: stringOrNumberProp,
4647
push: stringOrNumberProp,

docs/lib/examples/Layout.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default class Example extends React.Component {
1212
<Col xs="6">col-xs-6</Col>
1313
<Col xs="6">col-xs-6</Col>
1414
</Row>
15+
<Row>
16+
<Col xs>.col-xs (flexbox)</Col>
17+
<Col xs>.col-xs (flexbox)</Col>
18+
</Row>
1519
<Row>
1620
<Col xs="6" sm="4">col-xs-6 col-sm-4</Col>
1721
<Col xs="6" sm="4">col-xs-6 col-sm-4</Col>
@@ -23,6 +27,10 @@ export default class Example extends React.Component {
2327
<Row>
2428
<Col sm="12" md={{ size: 8, offset: 2 }}>.col-xs-12 .col-sm-12 .col-md-6 .col-md-offset-3</Col>
2529
</Row>
30+
<Row>
31+
<Col sm={{ size: 'auto', offset: 1 }}>.col-sm .col-sm-offset-1</Col>
32+
<Col sm={{ size: 'auto', offset: 1 }}>.col-sm .col-sm-offset-1</Col>
33+
</Row>
2634
</Container>
2735
);
2836
}

src/Col.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ const colSizes = ['xs', 'sm', 'md', 'lg', 'xl'];
55
const stringOrNumberProp = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
66

77
const columnProps = PropTypes.oneOfType([
8-
PropTypes.string,
8+
PropTypes.bool,
99
PropTypes.number,
10+
PropTypes.string,
1011
PropTypes.shape({
1112
size: stringOrNumberProp,
1213
push: stringOrNumberProp,
@@ -38,18 +39,31 @@ const Col = (props) => {
3839
colSizes.forEach(colSize => {
3940
const columnProp = props[colSize];
4041
delete attributes[colSize];
42+
let colClass;
4143

4244
if (!columnProp) {
4345
return;
4446
} else if (columnProp.size) {
47+
if (columnProp.size === 'auto') {
48+
colClass = `col-${colSize}`;
49+
} else {
50+
colClass = `col-${colSize}-${columnProp.size}`;
51+
}
52+
4553
colClasses.push(classNames({
46-
[`col-${colSize}-${columnProp.size}`]: columnProp.size,
54+
[colClass]: columnProp.size,
4755
[`push-${colSize}-${columnProp.push}`]: columnProp.push,
4856
[`pull-${colSize}-${columnProp.pull}`]: columnProp.pull,
4957
[`offset-${colSize}-${columnProp.offset}`]: columnProp.offset
5058
}));
5159
} else {
52-
colClasses.push(`col-${colSize}-${columnProp}`);
60+
if (columnProp === 'auto' || columnProp === true) {
61+
colClass = `col-${colSize}`;
62+
} else {
63+
colClass = `col-${colSize}-${columnProp}`;
64+
}
65+
66+
colClasses.push(colClass);
5367
}
5468
});
5569

src/__tests__/Col.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ describe('Col', () => {
3636
expect(wrapper.hasClass('col-xs-12')).toBe(true);
3737
});
3838

39+
it('should pass col size as flex with values "auto" or without value', () => {
40+
const wrapper = shallow(<Col xs="auto" sm />);
41+
42+
expect(wrapper.hasClass('col-xs')).toBe(true);
43+
expect(wrapper.hasClass('col-sm')).toBe(true);
44+
});
45+
3946
it('should pass col size specific classes via Objects', () => {
4047
const wrapper = shallow(<Col sm={{ size: 6, push: 2, pull: 2, offset: 2 }} />);
4148

@@ -45,4 +52,12 @@ describe('Col', () => {
4552
expect(wrapper.hasClass('pull-sm-2')).toBe(true);
4653
expect(wrapper.hasClass('offset-sm-2')).toBe(true);
4754
});
55+
56+
it('should pass col size as flex when passing via object with size "auto"', () => {
57+
const wrapper = shallow(<Col
58+
sm={{ size: 'auto', push: 2, pull: 2, offset: 2 }}
59+
/>);
60+
61+
expect(wrapper.hasClass('col-sm')).toBe(true);
62+
});
4863
});

0 commit comments

Comments
 (0)