Skip to content

Commit 8487598

Browse files
TheSharpieOneeddywashere
authored andcommitted
feat(Col): add custom widths ability to Col (#309)
closes #297
1 parent ab9ee8b commit 8487598

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

docs/lib/Components/LayoutPage.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ Col.propTypes = {
6161
sm: columnProps,
6262
md: columnProps,
6363
lg: columnProps,
64-
xl: columnProps
64+
xl: columnProps,
65+
// override the predefined width (the ones above) with your own custom widths.
66+
// see https://github.com/reactstrap/reactstrap/issues/297#issuecomment-273556116
67+
widths: PropTypes.array,
6568
}`}
6669
</PrismCode>
6770
</pre>

src/Col.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ const propTypes = {
2626
xl: columnProps,
2727
className: PropTypes.string,
2828
cssModule: PropTypes.object,
29+
widths: PropTypes.array,
2930
};
3031

3132
const defaultProps = {
32-
xs: true
33+
widths: colWidths,
3334
};
3435

3536
const getColumnSizeClass = (isXs, colWidth, colSize) => {
@@ -46,20 +47,25 @@ const Col = (props) => {
4647
const {
4748
className,
4849
cssModule,
50+
widths,
4951
...attributes
5052
} = props;
5153
const colClasses = [];
5254

53-
colWidths.forEach(colWidth => {
54-
const columnProp = props[colWidth];
55+
widths.forEach((colWidth, i) => {
56+
let columnProp = props[colWidth];
57+
58+
if (!i && columnProp === undefined) {
59+
columnProp = true;
60+
}
5561

5662
delete attributes[colWidth];
5763

5864
if (!columnProp) {
5965
return;
6066
}
6167

62-
const isXs = colWidth === 'xs';
68+
const isXs = !i;
6369
let colClass;
6470

6571
if (isobject(columnProp)) {

src/__tests__/Col.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ describe('Col', () => {
2222
expect(wrapper.hasClass('col')).toBe(true);
2323
});
2424

25+
it('should allow custom columns to be defined', () => {
26+
const wrapper = shallow(<Col widths={['base', 'jumbo']} base="4" jumbo="6" />);
27+
28+
expect(wrapper.hasClass('col-4')).toBe(true);
29+
expect(wrapper.hasClass('col-jumbo-6')).toBe(true);
30+
});
31+
32+
it('should allow custom columns to be defined with objects', () => {
33+
const wrapper = shallow(<Col widths={['base', 'jumbo', 'wtf']} wtf={{ size: 1, push: 2, pull: 3, offset: 4 }} />);
34+
35+
expect(wrapper.hasClass('col-wtf-1')).toBe(true);
36+
expect(wrapper.hasClass('push-wtf-2')).toBe(true);
37+
expect(wrapper.hasClass('pull-wtf-3')).toBe(true);
38+
expect(wrapper.hasClass('offset-wtf-4')).toBe(true);
39+
expect(wrapper.hasClass('col')).toBe(true);
40+
});
41+
2542
it('should pass col size specific classes as Strings', () => {
2643
const wrapper = shallow(<Col sm="6" />);
2744

0 commit comments

Comments
 (0)