diff --git a/public/app/core/components/ToggleButtonGroup/ToggleButton.tsx b/public/app/core/components/ToggleButtonGroup/ToggleButton.tsx new file mode 100644 index 00000000000..2fdf324ac44 --- /dev/null +++ b/public/app/core/components/ToggleButtonGroup/ToggleButton.tsx @@ -0,0 +1,33 @@ +import React, { PureComponent } from 'react'; + +interface ToggleButtonProps { + onChange?: (value) => void; + selected?: boolean; + value: any; + classNames?: string; +} +interface ToggleButtonState {} + +export default class ToggleButton extends PureComponent { + 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 ( + + ); + } +} diff --git a/public/app/core/components/ToggleButtonGroup/ToggleButtonGroup.tsx b/public/app/core/components/ToggleButtonGroup/ToggleButtonGroup.tsx new file mode 100644 index 00000000000..b3063004ebd --- /dev/null +++ b/public/app/core/components/ToggleButtonGroup/ToggleButtonGroup.tsx @@ -0,0 +1,46 @@ +import React, { PureComponent, ReactElement } from 'react'; + +interface ToggleButtonGroupProps { + onChange: (value) => void; + value?: any; + label?: string; +} + +export default class ToggleButtonGroup extends PureComponent { + 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) => { + const { value: buttonValue } = child.props; + + return React.cloneElement(child, { + selected: buttonValue === selectedValue, + onChange: this.handleToggle.bind(this), + }); + }); + + return ( +
+
+ {label && } + {childClones} +
+
+ ); + } +} diff --git a/public/sass/_grafana.scss b/public/sass/_grafana.scss index e4c7a9c59e1..b53b69a9c74 100644 --- a/public/sass/_grafana.scss +++ b/public/sass/_grafana.scss @@ -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'; diff --git a/public/sass/components/_toggle_button_group.scss b/public/sass/components/_toggle_button_group.scss new file mode 100644 index 00000000000..5107c18f1a7 --- /dev/null +++ b/public/sass/components/_toggle_button_group.scss @@ -0,0 +1,27 @@ +.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; + } +}