-
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 selection to chips (#121)
BREAKING CHANGE: Users should render `Chip` components directly instead of passing a `labels` prop to `ChipSet`.
- Loading branch information
1 parent
3797a06
commit 3ef1123
Showing
12 changed files
with
389 additions
and
150 deletions.
There are no files selected for viewing
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,102 @@ | ||
import React, {Component} from 'react'; | ||
import classnames from 'classnames'; | ||
import PropTypes from 'prop-types'; | ||
import withRipple from '../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, selected} = this.props; | ||
return classnames('mdc-chip', Array.from(classList), className, { | ||
'mdc-chip--selected': selected, | ||
}); | ||
} | ||
|
||
get adapter() { | ||
return { | ||
addClass: (className) => { | ||
const classList = new Set(this.state.classList); | ||
classList.add(className); | ||
this.setState({classList}); | ||
}, | ||
removeClass: (className) => { | ||
const classList = new Set(this.state.classList); | ||
classList.delete(className); | ||
this.setState({classList}); | ||
}, | ||
hasClass: (className) => this.classes.split(' ').includes(className), | ||
}; | ||
} | ||
|
||
handleClick = (e) => { | ||
if (typeof this.props.onClick === 'function') { | ||
this.props.onClick(e); | ||
} | ||
this.props.handleSelect(this.props.id); | ||
} | ||
|
||
render() { | ||
const { | ||
className, // eslint-disable-line no-unused-vars | ||
label, | ||
handleSelect, // eslint-disable-line no-unused-vars | ||
onClick, // eslint-disable-line no-unused-vars | ||
chipCheckmark, | ||
computeBoundingRect, // eslint-disable-line no-unused-vars | ||
initRipple, | ||
unbounded, // eslint-disable-line no-unused-vars | ||
...otherProps | ||
} = this.props; | ||
|
||
return ( | ||
<div | ||
className={this.classes} | ||
onClick={this.handleClick} | ||
ref={initRipple} | ||
{...otherProps} | ||
> | ||
{chipCheckmark} | ||
<div className='mdc-chip__text'>{label}</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Chip.propTypes = { | ||
id: PropTypes.number, | ||
label: PropTypes.string, | ||
className: PropTypes.string, | ||
selected: PropTypes.bool, | ||
handleSelect: PropTypes.func, | ||
onClick: PropTypes.func, | ||
// The following props are handled by withRipple and do not require defaults. | ||
initRipple: PropTypes.func, | ||
unbounded: PropTypes.bool, | ||
chipCheckmark: PropTypes.node, | ||
computeBoundingRect: PropTypes.func, | ||
}; | ||
|
||
Chip.defaultProps = { | ||
id: -1, | ||
label: '', | ||
className: '', | ||
selected: false, | ||
handleSelect: () => {}, | ||
}; | ||
|
||
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,29 @@ | ||
import React, {Component} from 'react'; | ||
|
||
export default class ChipCheckmark extends Component { | ||
width = null; | ||
|
||
init = (element) => { | ||
if (!element) { | ||
return; | ||
} | ||
|
||
// The checkmark's width may initially be set to 0, so use the checkmark's height as a proxy since the | ||
// checkmark should always be square. | ||
this.width = element.getBoundingClientRect().height; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className='mdc-chip__checkmark' ref={this.init}> | ||
<svg className='mdc-chip__checkmark-svg' viewBox='-2 -3 30 30'> | ||
<path | ||
className='mdc-chip__checkmark-path' | ||
fill='none' | ||
stroke='black' | ||
d='M1.73,12.91 8.1,19.28 22.79,4.59'/> | ||
</svg> | ||
</div> | ||
); | ||
} | ||
} |
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,63 @@ | ||
import React, {Component} from 'react'; | ||
import classnames from 'classnames'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import ChipCheckmark from './ChipCheckmark'; | ||
import Chip from './Chip'; | ||
|
||
export default class ChipSet extends Component { | ||
checkmarkWidth_ = 0; | ||
|
||
get classes() { | ||
const {className} = this.props; | ||
return classnames('mdc-chip-set', className); | ||
} | ||
|
||
get adapter() { | ||
return { | ||
hasClass: (className) => this.classes.split(' ').includes(className), | ||
}; | ||
} | ||
|
||
setCheckmarkWidth = (checkmark) => { | ||
if (!!this.checkmarkWidth_) { | ||
return; | ||
} | ||
this.checkmarkWidth_ = checkmark.width; | ||
} | ||
|
||
computeBoundingRect = (chipElement) => { | ||
const height = chipElement.getBoundingClientRect().height; | ||
const width = chipElement.getBoundingClientRect().width + this.checkmarkWidth_; | ||
return {height, width}; | ||
} | ||
|
||
renderChip = (chip) => { | ||
return ( | ||
<Chip | ||
chipCheckmark={this.props.filter ? <ChipCheckmark ref={this.setCheckmarkWidth}/> : null} | ||
computeBoundingRect={this.props.filter ? this.computeBoundingRect : null} | ||
{...chip.props} /> | ||
); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className={this.classes}> | ||
{React.Children.map(this.props.children, this.renderChip)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ChipSet.propTypes = { | ||
className: PropTypes.string, | ||
filter: PropTypes.bool, | ||
children: PropTypes.node, | ||
}; | ||
|
||
ChipSet.defaultProps = { | ||
className: '', | ||
filter: false, | ||
children: null, | ||
}; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.