Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chips): Add new component #117

Merged
merged 30 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
57d165d
Set up boilerplate for new component
bonniezhou Jun 18, 2018
d9ea92c
one chip no ripple
bonniezhou Jun 19, 2018
8351470
Get chip set working
bonniezhou Jun 21, 2018
9f26358
Remove choice and filter
bonniezhou Jun 21, 2018
698a92d
Remove selected
bonniezhou Jun 21, 2018
46980af
Rename class and set proptypes
bonniezhou Jun 21, 2018
a6e7fb5
resolve classnames
bonniezhou Jun 21, 2018
7c499f9
omg it works
bonniezhou Jun 21, 2018
05569da
add custom colors
bonniezhou Jun 22, 2018
14bc5ef
Don't pass entire chip object to chip
bonniezhou Jun 22, 2018
83388ed
update readme
bonniezhou Jun 22, 2018
f487e4a
Tests WIP
bonniezhou Jun 22, 2018
fa2a8f1
Change labels to children
bonniezhou Jun 22, 2018
3e88629
WIP tests
bonniezhou Jun 22, 2018
8ed862f
tests
bonniezhou Jun 25, 2018
58eab6d
Remove access of private handlers
bonniezhou Jun 25, 2018
7356cb9
Update README
bonniezhou Jun 25, 2018
31fc38c
Merge branch 'master' into feat/chips-simple
bonniezhou Jun 25, 2018
ca569fa
Lint
bonniezhou Jun 26, 2018
28846ce
all the fixes
bonniezhou Jun 27, 2018
b8dcae9
fix test
bonniezhou Jun 27, 2018
e31944c
Add adapter tests
bonniezhou Jun 27, 2018
a5955f0
Update package lock
bonniezhou Jun 27, 2018
01c086e
Remove state from chip set and remove defaultProps
bonniezhou Jun 27, 2018
61ee555
README updates
bonniezhou Jun 27, 2018
5012dd8
fix tests
bonniezhou Jun 28, 2018
62da397
Update index.scss
Jun 29, 2018
208dfcb
Simplify nit
bonniezhou Jul 2, 2018
0bd30a1
merge with master
bonniezhou Jul 2, 2018
15071f3
Add goldens for chips
bonniezhou Jul 2, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@google-cloud/storage": "^1.6.0",
"@material/button": "^0.36.0",
"@material/card": "^0.36.0",
"@material/chips": "^0.36.0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does chips have a version 0.36.0? It is 0.35.x in package-lock

"@material/fab": "^0.36.0",
"@material/floating-label": "^0.36.0",
"@material/line-ripple": "^0.35.0",
Expand Down
46 changes: 46 additions & 0 deletions packages/chips/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# React Chips

A React version of an [MDC Chips](https://github.com/material-components/material-components-web/tree/master/packages/mdc-chips).

## Installation

```
npm install --save @material/react-chips
```

## Usage

```js
import React, {Component} from 'react';
import ChipSet from '@material/react-chips';

class MyApp extends Component {
render() {
return (
<ChipSet labels={['Chip One', 'Chip Two']} />
);
}
}
```

## Props

### Chip Set

Prop Name | Type | Description
--- | --- | ---
className | String | Classes to be applied to the chip set element.
labels | Array | An array of strings. Each string has a corresponding chip whose label will be set to the value of that string.

### Chip

Prop Name | Type | Description
--- | --- | ---
className | String | Classes to be applied to the chip element.

## Sass Mixins

Sass mixins may be available to customize various aspects of the Components. Please refer to the
MDC Web repository for more information on what mixins are available, and how to use them.

[Advanced Sass Mixins](https://github.com/material-components/material-components-web/blob/v0.35.0/packages/mdc-chips/README.md#sass-mixins)
51 changes: 51 additions & 0 deletions packages/chips/chip-set/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, {Component} from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import {MDCChipSetFoundation} from '@material/chips';

import Chip from '../chip';

export default class ChipSet extends Component {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the README's example, you use a different import style for Component, e.g.:

import React from 'react';

class Foo extends React.Component {...}

Suggest changing the example in the README to use this code style instead.

foundation_ = null;

componentDidMount() {
this.foundation_ = new MDCChipSetFoundation(this.adapter);
this.foundation_.init();
}

componentWillUnmount() {
this.foundation_.destroy();
}

get classes() {
const {className} = this.props;
return classnames('mdc-chip-set', className);
}

get adapter() {
return {
hasClass: (className) => this.classes.split(' ').includes(className),
};
}

renderChip(label, index) {
return (
<Chip key={index}>
{label}
</Chip>
);
}

render() {
return (
<div className={this.classes}>
{this.props.labels.map((label, index) => this.renderChip(label, index))}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can reduce to: this.props.labels.map(this.renderChip)

</div>
);
}
}

ChipSet.propTypes = {
className: PropTypes.string,
labels: PropTypes.array.isRequired,
};
68 changes: 68 additions & 0 deletions packages/chips/chip/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, {Component} from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import withRipple from '@material/react-ripple';
import {MDCChipFoundation} from '@material/chips';

export class Chip extends Component {
foundation_ = null;
state = {
classList: new Set(),
};

componentDidMount() {
this.foundation_ = new MDCChipFoundation(this.adapter);
this.foundation_.init();
}

componentWillUnmount() {
this.foundation_.destroy();
}

get classes() {
const {classList} = this.state;
const {className} = this.props;
return classnames('mdc-chip', Array.from(classList), className);
}

get adapter() {
return {
addClass: (className) =>
this.setState({classList: this.state.classList.add(className)}),
removeClass: (className) => {
const {classList} = this.state;
classList.delete(className);
this.setState({classList});
},
};
}

render() {
const {
className, // eslint-disable-line no-unused-vars
children,
initRipple,
unbounded, // eslint-disable-line no-unused-vars
...otherProps
} = this.props;

return (
<div
className={this.classes}
ref={initRipple}
{...otherProps}
>
<div className='mdc-chip__text'>{children}</div>
</div>
);
}
}

Chip.propTypes = {
className: PropTypes.string,
initRipple: PropTypes.func,
unbounded: PropTypes.bool,
children: PropTypes.string,
};

export default withRipple(Chip);
5 changes: 5 additions & 0 deletions packages/chips/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Chip from './chip';
import ChipSet from './chip-set';

export {Chip};
export default ChipSet;
2 changes: 2 additions & 0 deletions packages/chips/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "@material/chips/chip/mdc-chip";
@import "@material/chips/chip-set/mdc-chip-set";
29 changes: 29 additions & 0 deletions packages/chips/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@material/react-chips",
"version": "0.0.0",
"description": "Material Components React Chips",
"license": "Apache-2.0",
"keywords": [
"mdc web react",
"material components react",
"material design",
"material chip",
"materialchip",
"material chips",
"materialchips"
],
"repository": {
"type": "git",
"url": "https://github.com/material-components/material-components-web-react.git"
},
"dependencies": {
"@material/chips": "^0.36.0",
"@material/react-ripple": "^0.2.0",
"classnames": "^2.2.5",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a way of making sure that minor version changes to these will not cause problems for the components?

"prop-types": "^15.6.1",
"react": "^16.3.2"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function getMaterialExternals() {
'base',
'button',
'card',
'chips',
'fab',
'floating-label',
'line-ripple',
Expand Down
18 changes: 18 additions & 0 deletions test/screenshot/chips/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="index.css">
<style>
body {
font-family: 'Roboto', Arial, sans-serif;
}
</style>
<script src="index.js" async></script>
</head>
<body>
<div id="app">
</div>
</body>
</html>
30 changes: 30 additions & 0 deletions test/screenshot/chips/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss';

import ChipSet from '../../../packages/chips';

class TestChipSet extends React.Component {
render() {
const {
className, labels, // eslint-disable-line react/prop-types
} = this.props;

return <ChipSet
className={className}
labels={labels}
/>;
}
}

ReactDOM.render((
<div>
<TestChipSet
labels={['Jane Smith', 'John Doe']}
/>
<TestChipSet
className='demo-custom-color'
labels={['Custom color', 'Custom color']}
/>
</div>
), document.getElementById('app'));
7 changes: 7 additions & 0 deletions test/screenshot/chips/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import "../../../packages/chips/index.scss";

.demo-custom-color .mdc-chip {
@include mdc-chip-ink-color(primary);
@include mdc-chip-fill-color(white);
@include mdc-chip-outline(2px, solid, primary);
}
10 changes: 10 additions & 0 deletions test/screenshot/chips/screenshot-suite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ScreenshotSuite from '../screenshot-suite';
import Screenshot from '../screenshot';

const screenshots = [
new Screenshot('chips/index.html'),
];

const screenshotSuite = new ScreenshotSuite('Chips', screenshots);

export default screenshotSuite;
5 changes: 5 additions & 0 deletions test/screenshot/chips/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const {bundle} = require('../webpack-bundles');

module.exports = [
bundle('chips/index.js', 'chips/index'),
];
1 change: 1 addition & 0 deletions test/screenshot/full-suite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default [
require('./button/screenshot-suite').default,
require('./card/screenshot-suite').default,
require('./chips/screenshot-suite').default,
require('./line-ripple/screenshot-suite').default,
require('./fab/screenshot-suite').default,
require('./floating-label/screenshot-suite').default,
Expand Down
1 change: 1 addition & 0 deletions test/screenshot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<a href="button">Button</a>
<a href='card'>Card</a>
<a href="chips">Chips</a>
<a href="fab">Fab</a>
<a href="floating-label">Floating Label</a>
<a href="line-ripple">Line Ripple</a>
Expand Down
1 change: 1 addition & 0 deletions test/screenshot/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = [
...require('./button/webpack.config.js'),
...require('./card/webpack.config.js'),
...require('./chips/webpack.config.js'),
...require('./fab/webpack.config.js'),
...require('./floating-label/webpack.config.js'),
...require('./line-ripple/webpack.config.js'),
Expand Down
Loading