-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chips): Add new component (#117)
Add basic working component for Chips, including Chip and ChipSet. Does not include any variants.
- Loading branch information
1 parent
a53fc7d
commit 410da30
Showing
20 changed files
with
370 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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(this.renderChip)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ChipSet.propTypes = { | ||
className: PropTypes.string, | ||
labels: PropTypes.array.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
"prop-types": "^15.6.1", | ||
"react": "^16.3.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.