1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add radiogroup ButtonGroup variant (#9383)

Add a prop to the `ButtonGroup` component to make it act as a group of
radio buttons, rather than a group of regular buttons. These commits
were written by @danjm - I just pulled them into this branch.
This commit is contained in:
Mark Stacey 2020-09-10 13:08:38 -03:00 committed by GitHub
parent 24f4973afc
commit b80ab53396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 4 deletions

View File

@ -11,11 +11,13 @@ export default class ButtonGroup extends PureComponent {
className: PropTypes.string, className: PropTypes.string,
style: PropTypes.object, style: PropTypes.object,
newActiveButtonIndex: PropTypes.number, newActiveButtonIndex: PropTypes.number,
variant: PropTypes.oneOf(['radiogroup', 'default']),
} }
static defaultProps = { static defaultProps = {
className: 'button-group', className: 'button-group',
defaultActiveButtonIndex: 0, defaultActiveButtonIndex: 0,
variant: 'default',
} }
state = { state = {
@ -36,14 +38,21 @@ export default class ButtonGroup extends PureComponent {
} }
renderButtons () { renderButtons () {
const { children, disabled } = this.props const { children, disabled, variant } = this.props
return React.Children.map(children, (child, index) => { return React.Children.map(children, (child, index) => {
return child && ( return child && (
<button <button
role={variant === 'radiogroup' ? 'radio' : undefined}
aria-checked={index === this.state.activeButtonIndex}
className={classnames( className={classnames(
'button-group__button', 'button-group__button',
{ 'button-group__button--active': index === this.state.activeButtonIndex }, child.props.className,
{
'radio-button': variant === 'radiogroup',
'button-group__button--active': index === this.state.activeButtonIndex,
'radio-button--active': variant === 'radiogroup' && index === this.state.activeButtonIndex,
},
)} )}
onClick={() => { onClick={() => {
this.handleButtonClick(index) this.handleButtonClick(index)
@ -59,11 +68,12 @@ export default class ButtonGroup extends PureComponent {
} }
render () { render () {
const { className, style } = this.props const { className, style, variant } = this.props
return ( return (
<div <div
className={className} className={classnames(className, { 'radio-button-group': variant === 'radiogroup' })}
role={variant === 'radiogroup' ? 'radiogroup' : undefined}
style={style} style={style}
> >
{ this.renderButtons() } { this.renderButtons() }

View File

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import { action } from '@storybook/addon-actions' import { action } from '@storybook/addon-actions'
import classnames from 'classnames'
import { text, boolean } from '@storybook/addon-knobs/react' import { text, boolean } from '@storybook/addon-knobs/react'
import Button from '../button' import Button from '../button'
import ButtonGroup from '.' import ButtonGroup from '.'
@ -50,3 +51,30 @@ export const withDisabledButton = () => (
</Button> </Button>
</ButtonGroup> </ButtonGroup>
) )
export const radioButtons = () => (
<ButtonGroup
style={{ width: '300px' }}
defaultActiveButtonIndex={1}
variant="radiogroup"
>
<Button
onClick={action('radio 1')}
>
{text('Button1', '1%')}
</Button>
<Button
onClick={action('radio 2')}
>
{text('Button2', '2%')}
</Button>
<Button
onClick={action('radio 3')}
className={classnames({
'radio-button--danger': boolean('Button3 warning', false),
})}
>
{text('Button3', '5%')}
</Button>
</ButtonGroup>
)

View File

@ -35,3 +35,41 @@
} }
} }
} }
.radio-button-group {
.radio-button {
@include H7;
color: $Grey-700;
border: 1px solid $Grey-100;
background: $white;
border-radius: 25px;
height: 25px;
margin-right: 8px;
min-width: 48px;
padding: 0;
&--active {
font-weight: bold;
background: $Blue-300;
color: white;
}
&--danger {
border: 1px solid $Red-500;
color: $Red-500;
background: $white;
}
&:hover {
box-shadow: 0 0 14px rgba(0, 0, 0, 0.18);
};
}
.radio-button--active.radio-button--danger {
border: 1px solid $Red-500;
color: white;
background: $Red-500;
}
}