Skip to content

Commit

Permalink
fix(card): add missing hasRipple (material-components#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmihelac committed Sep 18, 2018
1 parent 3eb8a4b commit 9f8c79f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
54 changes: 44 additions & 10 deletions packages/card/PrimaryContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,67 @@ import React from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';

import withRipple from '@material/react-ripple';

export default class PrimaryContent extends React.Component {

render() {
const {
className,
children,
hasRipple,
...otherProps
} = this.props;
const classes = classnames('mdc-card__primary-action', className);

if (hasRipple) {
return (<RipplePrimaryContent {...otherProps} />);
}
return (
<div
className={classes}
{...otherProps}
>
{children}
</div>
<PrimaryContentDefault {...otherProps} />
);
}
}


const PrimaryContentDefault = (props) => {
const {
className,
initRipple,
children,
...otherProps
} = props;

const classes = classnames('mdc-card__primary-action', className);
return (
<div
className={classes}
ref={initRipple}
{...otherProps}
>
{children}
</div>
);
};

export const RipplePrimaryContent = withRipple(PrimaryContentDefault);

PrimaryContent.propTypes = {
className: PropTypes.string,
children: PropTypes.node,
hasRipple: PropTypes.bool,
};

PrimaryContent.defaultProps = {
className: '',
children: null,
hasRipple: false,
};

PrimaryContentDefault.propTypes = {
className: PropTypes.string,
children: PropTypes.node,
initRipple: PropTypes.func,
};

PrimaryContentDefault.defaultProps = {
className: '',
initRipple: () => {},
children: null,
};
8 changes: 7 additions & 1 deletion packages/card/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ This component is used as the container for primary tappable content.
import Card, {CardPrimaryContent} from '@material/react-card';

<Card>
<CardPrimaryContent>
<CardPrimaryContent hasRipple>
<p>Content</p>
</CardPrimaryContent>
</Card>
```

#### Props
Prop Name | Type | Description
--- | --- | ---
hasRipple | n/a | If present on element, it will enable the ripple on the primary content.


### CardMedia

This component is a container for an image on the card. Optionally, any children of the `<CardMedia>` component is wrapped with an element with the className `.mdc-card__media-content`.
Expand Down
1 change: 1 addition & 0 deletions packages/card/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"url": "https://github.com/material-components/material-components-web-react.git"
},
"dependencies": {
"@material/react-ripple": "^0.4.2",
"@material/card": "^0.39.0",
"classnames": "^2.2.5",
"prop-types": "^15.6.1",
Expand Down
6 changes: 3 additions & 3 deletions test/screenshot/card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const imageUrl = './../images/1-1.jpg';
const BasicCard = () => {
return (
<Card className='basic-card'>
<CardPrimaryContent>
<CardPrimaryContent hasRipple>
<CardMedia className='basic-card-image' imageUrl={imageUrl} />
</CardPrimaryContent>
<CardActions>
Expand All @@ -37,7 +37,7 @@ const BasicCard = () => {
const HorizontalCard = () => {
return (
<Card className='horizontal-card'>
<CardPrimaryContent>
<CardPrimaryContent hasRipple>
<div className='horizontal-card-content'>
<CardMedia square className='horizontal-card-image' imageUrl={imageUrl} />
<div className='horizontal-card-header'>
Expand Down Expand Up @@ -106,7 +106,7 @@ const NewsRow = ({title, snippet, index}) => {
const ContentOnMediaCard = () => {
return (
<Card className='content-on-media-card'>
<CardPrimaryContent>
<CardPrimaryContent hasRipple>
<CardMedia
square
contentClassName='content-on-media-content'
Expand Down
26 changes: 22 additions & 4 deletions test/unit/card/Content.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import React from 'react';
import {assert} from 'chai';
import {shallow} from 'enzyme';
import {mount} from 'enzyme';
import {CardPrimaryContent} from '../../../packages/card/index';
import {RipplePrimaryContent} from '../../../packages/card/PrimaryContent.js';

suite('CardPrimaryContent');

test('classNames adds classes', () => {
const wrapper = shallow(<CardPrimaryContent className='test-class-name'/>);
assert.isTrue(wrapper.hasClass('test-class-name'));
assert.isTrue(wrapper.hasClass('mdc-card__primary-action'));
const wrapper = mount(<CardPrimaryContent className='test-class-name'/>);
const primaryContentDiv = wrapper.find('.mdc-card__primary-action');
assert.isTrue(primaryContentDiv.hasClass('test-class-name'));
assert.equal(primaryContentDiv.length, 1);
});

const rippledPrimaryContent = <RipplePrimaryContent />;

test('if hasRipple true, then it should contain RipplePrimaryContent', () => {
const wrapper = mount(
<CardPrimaryContent hasRipple />
);
assert.isTrue(wrapper.contains(rippledPrimaryContent));
});

test('if hasRipple false, then it should not contain RipplePrimaryContent', () => {
const wrapper = mount(
<CardPrimaryContent />
);
assert.isFalse(wrapper.contains(rippledPrimaryContent));
});

0 comments on commit 9f8c79f

Please sign in to comment.