Skip to content

Commit 3359ade

Browse files
joeruelloaaronvanston
authored andcommitted
Allows hiding of columns at various breakpoints (#64)
* Allows hiding of columns at various breakpoints * Simplify and add tests for isHidden
1 parent 6736fdd commit 3359ade

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ These are the **available** and **reserved** props that can be used on the `Col`
8383

8484
| Prop | Valid types | Valid values | Default value | Description |
8585
|:--------------|:------------------------------------------------------------|:----------------------------------------------------------------------------------------|:--------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
86-
| `xs` | integer | `1`-`12`<br>(based on default columns) | nill | Sets the width of the col based the column configuration (12 by default) for the XS breakpoint and up. |
87-
| `sm` | integer | `1`-`12`<br>(based on default columns) | nill | Sets the width of the col based the column configuration (12 by default) for the SM breakpoint and up. |
88-
| `md` | integer | `1`-`12`<br>(based on default columns) | nill | Sets the width of the col based the column configuration (12 by default) for the MD breakpoint and up. |
89-
| `lg` | integer | `1`-`12`<br>(based on default columns) | nill | Sets the width of the col based the column configuration (12 by default) for the LG breakpoint and up. |
86+
| `xs` | integer | `0`-`12`<br>(based on default columns)<br>`hidden` | nill | Sets the width of the col based the column configuration (12 by default) for the XS breakpoint and up. |
87+
| `sm` | integer | `0`-`12`<br>(based on default columns)<br>`hidden` | nill | Sets the width of the col based the column configuration (12 by default) for the SM breakpoint and up. |
88+
| `md` | integer | `0`-`12`<br>(based on default columns)<br>`hidden` | nill | Sets the width of the col based the column configuration (12 by default) for the MD breakpoint and up. |
89+
| `lg` | integer | `0`-`12`<br>(based on default columns)<br>`hidden` | nill | Sets the width of the col based the column configuration (12 by default) for the LG breakpoint and up. |
9090
| `gutter` | integer, string,<br>[object](#responsive-api-using-objects) | `'1rem'`<br>`'12px'`<br>`4` | `'1rem'` | Sets the width of the gutter to be used between columns. For correct positioning you must set the same value (if custom) on the parent Row |
9191
| `order` | string,<br>[object](#responsive-api-using-objects) | `-1`<br>`0`<br>`1`<br>(etc) | `0` | By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container. [Read more about order.](https://css-tricks.com/snippets/css/a-guide-to-flexbox/#article-header-id-9) |
9292
| `alignSelf` | string,<br>[object](#responsive-api-using-objects) | `'auto'`<br>`'flex‑start'`<br>`'flex‑end'`<br>`'center'`<br>`'baseline'`<br>`'stretch'` | `'auto'` | This allows the default alignment (or the one specified by align‑items) to be overridden for individual flex items. [Read more about align-self.](https://css-tricks.com/snippets/css/a-guide-to-flexbox/#article-header-id-14) |

src/components/Col/Col.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ Col.defaultProps = {
3535
export const alignSelfOptions = ['auto', 'flex-start', 'flex-end', 'center', 'baseline', 'stretch'];
3636

3737
Col.propTypes = {
38-
xs: PropTypes.number,
39-
sm: PropTypes.number,
40-
md: PropTypes.number,
41-
lg: PropTypes.number,
38+
xs: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['hidden'])]),
39+
sm: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['hidden'])]),
40+
md: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['hidden'])]),
41+
lg: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['hidden'])]),
4242

4343
gutter: PropTypes.oneOfType([
4444
PropTypes.number,

src/helpers/columnWidth/columnWidth.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ const { theme } = themeProvider;
88
export const percentage = (props, breakpoint) =>
99
(Math.abs(props[breakpoint]) / theme(props).columns) * 100;
1010

11+
export const isHidden = (props, breakpoint) =>
12+
(props[breakpoint] === 0 || props[breakpoint] === 'hidden');
13+
1114
const columnWidth = (props, breakpoint) => {
15+
if (isHidden(props, breakpoint)) {
16+
return css`
17+
display:none;
18+
`;
19+
}
20+
1221
const width = percentage(props, breakpoint);
1322

1423
return has(props, `${breakpoint}`) ? css`
1524
flex-basis: ${width}%;
1625
max-width: ${width}%;
26+
display: block;
1727
` : null;
1828
};
1929
export default columnWidth;

src/helpers/columnWidth/columnWidth.spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import columnWidth, { percentage } from './columnWidth';
1+
import columnWidth, { percentage, isHidden } from './columnWidth';
22

33
const mockProps = { xs: -1, sm: 2, md: 0, lg: 6 };
44

@@ -14,6 +14,15 @@ describe('percentage', () => {
1414
});
1515
});
1616

17+
describe('isHidden', () => {
18+
test('should return hidden if a given breakpoint is explicitly "0" or "hidden"', () => {
19+
expect(isHidden({ xs: 0 }, 'xs')).toEqual(true);
20+
expect(isHidden({ xs: 'hidden' }, 'xs')).toEqual(true);
21+
expect(isHidden({ xs: undefined }, 'xs')).toEqual(false);
22+
expect(isHidden({ xs: 12 }, 'xs')).toEqual(false);
23+
});
24+
});
25+
1726
describe('columnWidth', () => {
1827
test('should generate column width css', () => {
1928
const width = columnWidth(mockProps, 'sm').join('');
@@ -25,4 +34,15 @@ describe('columnWidth', () => {
2534
const width = columnWidth(mockProps, 'xl');
2635
expect(width).toEqual(null);
2736
});
37+
38+
test('should return "display:none" if breakpoint is explicity set to 0', () => {
39+
const width = columnWidth(mockProps, 'md').join('');
40+
expect(width).toContain('display:none');
41+
});
42+
43+
test('should return "display:none" if breakpoint is explicity set to "hidden"', () => {
44+
const hiddenMockProps = { xs: -1, sm: 2, md: 'hidden', lg: 6 };
45+
const width = columnWidth(hiddenMockProps, 'md').join('');
46+
expect(width).toContain('display:none');
47+
});
2848
});

0 commit comments

Comments
 (0)