Commit 0d7b09b0 by Erik Sundell

add basic button group component, using the the same label style as is

remove not used code

cleanup
parent a6ef1569
import React, { PureComponent } from 'react';
interface ToggleButtonProps {
onChange?: (value) => void;
selected?: boolean;
value: any;
classNames?: string;
}
interface ToggleButtonState {}
export default class ToggleButton extends PureComponent<ToggleButtonProps, ToggleButtonState> {
static defaultProps = {
classNames: '',
};
handleChange = () => {
const { onChange, value } = this.props;
if (onChange) {
onChange(value);
}
};
render() {
const { children, selected, classNames } = this.props;
const btnClassName = `btn ${classNames} ${selected ? 'active' : ''}`;
return (
<button className={btnClassName} onClick={this.handleChange}>
<span>{children}</span>
</button>
);
}
}
import React, { PureComponent, ReactElement } from 'react';
interface ToggleButtonGroupProps {
onChange: (value) => void;
value?: any;
label?: string;
}
export default class ToggleButtonGroup extends PureComponent<ToggleButtonGroupProps> {
getValues() {
const { children } = this.props;
return React.Children.toArray(children).map(c => c['props'].value);
}
handleToggle(toggleValue) {
const { value, onChange } = this.props;
if (value && value === toggleValue) {
return;
}
onChange(toggleValue);
}
render() {
const { children, value, label } = this.props;
const values = this.getValues();
const selectedValue = value || values[0];
const childClones = React.Children.map(children, (child: ReactElement<any>) => {
const { value: buttonValue } = child.props;
return React.cloneElement(child, {
selected: buttonValue === selectedValue,
onChange: this.handleToggle.bind(this),
});
});
return (
<div className="gf-form">
<div className="toggle-button-group">
{label && <label className="gf-form-label">{label}</label>}
{childClones}
</div>
</div>
);
}
}
......@@ -101,6 +101,7 @@
@import 'components/delete_button';
@import 'components/add_data_source.scss';
@import 'components/page_loader';
@import 'components/toggle_button_group';
// PAGES
@import 'pages/login';
......
.toggle-button-group {
display: flex;
.gf-form-label {
background-color: $input-label-bg;
}
.btn {
@include buttonBackground($input-bg, $input-bg);
&.active {
background-color: lighten($input-label-bg, 5%);
color: $link-color;
&:hover {
cursor: default;
}
}
}
&:first-child {
border-radius: 2px 0 0 2px;
margin: 0;
}
&:last-child {
border-radius: 0 2px 2px 0;
margin-left: 0 !important;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment