Skip to content

Commit

Permalink
Merge pull request #6820 from oliviertassinari/hidden-component
Browse files Browse the repository at this point in the history
[Hidden] Remove one degree of freedom
  • Loading branch information
oliviertassinari authored May 9, 2017
2 parents 11874f8 + a99e027 commit 3a02048
Show file tree
Hide file tree
Showing 97 changed files with 181 additions and 177 deletions.
35 changes: 15 additions & 20 deletions docs/src/pages/layout/hidden/BreakpointDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,21 @@ function BreakpointDown(props) {
<Typography type="subheading" className={classes.typography}>
Current width: {props.width}
</Typography>
<Hidden
xsDown
component={<Paper className={classes.paper}>xsDown</Paper>}
/>
<Hidden
smDown
component={<Paper className={classes.paper}>smDown</Paper>}
/>
<Hidden
mdDown
component={<Paper className={classes.paper}>mdDown</Paper>}
/>
<Hidden
lgDown
component={<Paper className={classes.paper}>lgDown</Paper>}
/>
<Hidden
xlDown
component={<Paper className={classes.paper}>xlDown</Paper>}
/>
<Hidden xsDown>
<Paper className={classes.paper}>xsDown</Paper>
</Hidden>
<Hidden smDown>
<Paper className={classes.paper}>smDown</Paper>
</Hidden>
<Hidden mdDown>
<Paper className={classes.paper}>mdDown</Paper>
</Hidden>
<Hidden lgDown>
<Paper className={classes.paper}>lgDown</Paper>
</Hidden>
<Hidden xlDown>
<Paper className={classes.paper}>xlDown</Paper>
</Hidden>
</div>
);
}
Expand Down
21 changes: 9 additions & 12 deletions docs/src/pages/layout/hidden/BreakpointOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ function BreakpointOnly(props) {
<Typography type="subheading" className={classes.typography}>
Current width: {props.width}
</Typography>
<Hidden
only="lg"
component={<Paper className={classes.paper}>Hidden on lg</Paper>}
/>
<Hidden
only="sm"
component={<Paper className={classes.paper}>Hidden on sm</Paper>}
/>
<Hidden
only={['sm', 'lg']}
component={<Paper className={classes.paper}>Hidden on sm and lg</Paper>}
/>
<Hidden only="lg">
<Paper className={classes.paper}>Hidden on lg</Paper>
</Hidden>
<Hidden only="sm">
<Paper className={classes.paper}>Hidden on sm</Paper>
</Hidden>
<Hidden only={['sm', 'lg']}>
<Paper className={classes.paper}>Hidden on sm and lg</Paper>
</Hidden>
</div>
);
}
Expand Down
35 changes: 15 additions & 20 deletions docs/src/pages/layout/hidden/BreakpointUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,21 @@ function BreakpointUp(props) {
<Typography type="subheading" className={classes.typography}>
Current width: {props.width}
</Typography>
<Hidden
xsUp
component={<Paper className={classes.paper}>xsUp</Paper>}
/>
<Hidden
smUp
component={<Paper className={classes.paper}>smUp</Paper>}
/>
<Hidden
mdUp
component={<Paper className={classes.paper}>mdUp</Paper>}
/>
<Hidden
lgUp
component={<Paper className={classes.paper}>lgUp</Paper>}
/>
<Hidden
xlUp
component={<Paper className={classes.paper}>xlUp</Paper>}
/>
<Hidden xsUp>
<Paper className={classes.paper}>xsUp</Paper>
</Hidden>
<Hidden smUp>
<Paper className={classes.paper}>smUp</Paper>
</Hidden>
<Hidden mdUp>
<Paper className={classes.paper}>mdUp</Paper>
</Hidden>
<Hidden lgUp>
<Paper className={classes.paper}>lgUp</Paper>
</Hidden>
<Hidden xlUp>
<Paper className={classes.paper}>xlUp</Paper>
</Hidden>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build-api-docs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable flowtype/require-valid-file-annotation */
/* eslint-disable no-console */
// Track bug on imported flow type: https://github.com/reactjs/react-docgen/issues/180

import fs from 'fs';
import path from 'path';
import * as reactDocgen from 'react-docgen';
Expand Down
2 changes: 1 addition & 1 deletion src/AppBar/AppBar.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Avatar/Avatar.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
3 changes: 2 additions & 1 deletion src/Badge/Badge.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow weak
// @flow

import React from 'react';
import { createShallow } from 'src/test-utils';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/BottomNavigation/BottomNavigation.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/BottomNavigation/BottomNavigationButton.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Button/Button.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Card/Card.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Card/CardActions.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Card/CardContent.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Card/CardHeader.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Card/CardMedia.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Checkbox/Checkbox.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import { assert } from 'chai';
import { createShallow } from 'src/test-utils';
Expand Down
2 changes: 1 addition & 1 deletion src/Chip/Chip.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import keycode from 'keycode';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog/Dialog.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog/DialogActions.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog/DialogContent.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog/DialogContentText.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Dialog/DialogTitle.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Divider/Divider.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Drawer/Drawer.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Form/FormControl.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Form/FormGroup.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
2 changes: 1 addition & 1 deletion src/Form/FormLabel.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
4 changes: 3 additions & 1 deletion src/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ function Grid(props: Props, context: any) {

if (hidden) {
return (
<Hidden {...hidden} component={<ComponentProp {...gridProps} />} />
<Hidden {...hidden}>
<ComponentProp {...gridProps} />
</Hidden>
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Grid/Grid.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow weak
// @flow

import React from 'react';
import { assert } from 'chai';
Expand Down
18 changes: 4 additions & 14 deletions src/Hidden/Hidden.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
// @flow
import React, { Element } from 'react';

import React from 'react';
import HiddenJs from './HiddenJs';
import type { HiddenProps } from './types';

type Props = HiddenProps & {
/**
* If string or Function, component is used as the root node and all other props are passed
* including children.
* If an Element, it will be rendered as-is and no other props are propagated.
*/
component?: string | Function | Element<*>,
/**
* Specify which implementation to use. 'js' is the default, 'css' works better for server
* side rendering.
*/
implementation?: 'js' | 'css'
}
};

/**
* Responsively hides children based on the selected implementation.
*/
function Hidden(props: Props) {
const {
component: componentProp,
implementation,
...other
} = props;

// workaround: see https://github.com/facebook/flow/issues/1660#issuecomment-297775427
const component = componentProp || Hidden.defaultProps.component;

if (implementation === 'js') {
return <HiddenJs {...other} component={component} />;
return <HiddenJs {...other} />;
}

throw new Error('<Hidden implementation="css" /> is not yet implemented');
}

Hidden.defaultProps = {
component: 'div',
implementation: 'js',
xsUp: false,
smUp: false,
Expand Down
28 changes: 28 additions & 0 deletions src/Hidden/Hidden.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @flow

import React from 'react';
import { assert } from 'chai';
import { createShallow } from 'src/test-utils';
import Hidden from './Hidden';
import HiddenJs from './HiddenJs';

describe('<Hidden />', () => {
let shallow;

before(() => {
shallow = createShallow();
});

describe('props: implementation', () => {
it('should use HiddenJs by default', () => {
const wrapper = shallow(<Hidden>{'Hello'}</Hidden>);
assert.strictEqual(wrapper.find(HiddenJs).length, 1);
});

it('should use change the implementation', () => {
assert.throws(() => {
shallow(<Hidden implementation="css">{'Hello'}</Hidden>);
}, 'is not yet implemented');
});
});
});
Loading

0 comments on commit 3a02048

Please sign in to comment.