2019-01-17 02:27:43 -06:00
|
|
|
import React, { FC, ReactNode, PureComponent } from 'react';
|
2019-01-08 13:51:00 -06:00
|
|
|
import { Tooltip } from '@grafana/ui';
|
2018-12-05 08:35:18 -06:00
|
|
|
|
|
|
|
interface ToggleButtonGroupProps {
|
|
|
|
label?: string;
|
2018-12-07 10:15:46 -06:00
|
|
|
children: JSX.Element[];
|
|
|
|
transparent?: boolean;
|
2018-12-05 08:35:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class ToggleButtonGroup extends PureComponent<ToggleButtonGroupProps> {
|
|
|
|
render() {
|
2018-12-07 10:15:46 -06:00
|
|
|
const { children, label, transparent } = this.props;
|
2018-12-05 08:35:18 -06:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="gf-form">
|
2018-12-07 10:15:46 -06:00
|
|
|
{label && <label className={`gf-form-label ${transparent ? 'gf-form-label--transparent' : ''}`}>{label}</label>}
|
|
|
|
<div className={`toggle-button-group ${transparent ? 'toggle-button-group--transparent' : ''}`}>{children}</div>
|
2018-12-05 08:35:18 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 09:27:14 -06:00
|
|
|
|
|
|
|
interface ToggleButtonProps {
|
2019-05-13 02:38:19 -05:00
|
|
|
onChange?: (value: any) => void;
|
2018-12-05 09:27:14 -06:00
|
|
|
selected?: boolean;
|
|
|
|
value: any;
|
|
|
|
className?: string;
|
|
|
|
children: ReactNode;
|
2018-12-11 03:00:29 -06:00
|
|
|
tooltip?: string;
|
2018-12-05 09:27:14 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:27:43 -06:00
|
|
|
export const ToggleButton: FC<ToggleButtonProps> = ({
|
2018-12-09 06:06:34 -06:00
|
|
|
children,
|
|
|
|
selected,
|
|
|
|
className = '',
|
2018-12-11 03:00:29 -06:00
|
|
|
value = null,
|
|
|
|
tooltip,
|
2018-12-09 06:06:34 -06:00
|
|
|
onChange,
|
|
|
|
}) => {
|
2019-05-13 02:38:19 -05:00
|
|
|
const onClick = (event: React.SyntheticEvent) => {
|
2018-12-05 09:27:14 -06:00
|
|
|
event.stopPropagation();
|
2019-05-16 02:52:22 -05:00
|
|
|
if (!selected && onChange) {
|
2018-12-05 09:27:14 -06:00
|
|
|
onChange(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const btnClassName = `btn ${className} ${selected ? 'active' : ''}`;
|
2018-12-11 03:00:29 -06:00
|
|
|
const button = (
|
2019-03-11 11:03:10 -05:00
|
|
|
<button className={btnClassName} onClick={onClick}>
|
2018-12-05 09:27:14 -06:00
|
|
|
<span>{children}</span>
|
|
|
|
</button>
|
|
|
|
);
|
2018-12-11 03:00:29 -06:00
|
|
|
|
|
|
|
if (tooltip) {
|
2018-12-22 23:46:26 -06:00
|
|
|
return (
|
|
|
|
<Tooltip content={tooltip} placement="bottom">
|
|
|
|
{button}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
2018-12-11 03:00:29 -06:00
|
|
|
} else {
|
|
|
|
return button;
|
|
|
|
}
|
2018-12-05 09:27:14 -06:00
|
|
|
};
|