import { css, cx } from '@emotion/css'; import React, { PureComponent } from 'react'; import { selectors } from '@grafana/e2e-selectors'; import { Tooltip } from '@grafana/ui'; import { VariableOption } from '../../types'; export interface Props extends React.HTMLProps { multi: boolean; values: VariableOption[]; selectedValues: VariableOption[]; highlightIndex: number; onToggle: (option: VariableOption, clearOthers: boolean) => void; onToggleAll: () => void; /** * Used for aria-controls */ id: string; } export class VariableOptions extends PureComponent { onToggle = (option: VariableOption) => (event: React.MouseEvent) => { const clearOthers = event.shiftKey || event.ctrlKey || event.metaKey; this.handleEvent(event); this.props.onToggle(option, clearOthers); }; onToggleAll = (event: React.MouseEvent) => { this.handleEvent(event); this.props.onToggleAll(); }; handleEvent(event: React.MouseEvent) { event.preventDefault(); event.stopPropagation(); } render() { // Don't want to pass faulty rest props to the div const { multi, values, highlightIndex, selectedValues, onToggle, onToggleAll, ...restProps } = this.props; return (
    {this.renderMultiToggle()} {values.map((option, index) => this.renderOption(option, index))}
); } renderOption(option: VariableOption, index: number) { const { highlightIndex } = this.props; const selectClass = option.selected ? 'variable-option pointer selected' : 'variable-option pointer'; const highlightClass = index === highlightIndex ? `${selectClass} highlighted` : selectClass; return (
  • {option.text}
  • ); } renderMultiToggle() { const { multi, selectedValues } = this.props; if (!multi) { return null; } return ( 1 ? 'variable-options-column-header many-selected' : 'variable-options-column-header' }`} role="checkbox" aria-checked={selectedValues.length > 1 ? 'mixed' : 'false'} onClick={this.onToggleAll} aria-label="Toggle all values" data-placement="top" > Selected ({selectedValues.length}) ); } } const listStyles = cx( 'variable-options-column', css` list-style-type: none; ` );