You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
import React, { PureComponent } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import classnames from 'classnames'
|
|
|
|
export default class ButtonGroup extends PureComponent {
|
|
static propTypes = {
|
|
defaultActiveButtonIndex: PropTypes.number,
|
|
noButtonActiveByDefault: PropTypes.bool,
|
|
disabled: PropTypes.bool,
|
|
children: PropTypes.array,
|
|
className: PropTypes.string,
|
|
style: PropTypes.object,
|
|
}
|
|
|
|
static defaultProps = {
|
|
className: 'button-group',
|
|
defaultActiveButtonIndex: 0,
|
|
}
|
|
|
|
state = {
|
|
activeButtonIndex: this.props.noButtonActiveByDefault
|
|
? null
|
|
: this.props.defaultActiveButtonIndex,
|
|
}
|
|
|
|
handleButtonClick (activeButtonIndex) {
|
|
this.setState({ activeButtonIndex })
|
|
}
|
|
|
|
renderButtons () {
|
|
const { children, disabled } = this.props
|
|
|
|
return React.Children.map(children, (child, index) => {
|
|
return child && (
|
|
<button
|
|
className={classnames(
|
|
'button-group__button',
|
|
{ 'button-group__button--active': index === this.state.activeButtonIndex },
|
|
)}
|
|
onClick={() => {
|
|
this.handleButtonClick(index)
|
|
child.props.onClick && child.props.onClick()
|
|
}}
|
|
disabled={disabled || child.props.disabled}
|
|
key={index}
|
|
>
|
|
{ child.props.children }
|
|
</button>
|
|
)
|
|
})
|
|
}
|
|
|
|
render () {
|
|
const { className, style } = this.props
|
|
|
|
return (
|
|
<div
|
|
className={className}
|
|
style={style}
|
|
>
|
|
{ this.renderButtons() }
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
|