-
Notifications
You must be signed in to change notification settings - Fork 227
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
Changes from 26 commits
57d165d
d9ea92c
8351470
9f26358
698a92d
46980af
a6e7fb5
7c499f9
05569da
14bc5ef
83388ed
f487e4a
fa2a8f1
3e88629
8ed862f
58eab6d
7356cb9
31fc38c
ca569fa
28846ce
b8dcae9
e31944c
a5955f0
01c086e
61ee555
5012dd8
62da397
208dfcb
0bd30a1
15071f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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) |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the
Suggest changing the example in the |
||
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))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can reduce to: |
||
</div> | ||
); | ||
} | ||
} | ||
|
||
ChipSet.propTypes = { | ||
className: PropTypes.string, | ||
labels: PropTypes.array.isRequired, | ||
}; |
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); |
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; |
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"; |
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
} |
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> |
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')); |
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); | ||
} |
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; |
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'), | ||
]; |
There was a problem hiding this comment.
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