import React, { ComponentType, PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { ClickOutsideWrapper } from '@grafana/ui'; import { LoadingState } from '@grafana/data'; import { StoreState } from 'app/types'; import { VariableInput } from '../shared/VariableInput'; import { commitChangesToVariable, filterOrSearchOptions, navigateOptions, openOptions } from './actions'; import { OptionsPickerState, toggleAllOptions, toggleOption } from './reducer'; import { VariableOption, VariableWithMultiSupport, VariableWithOptions } from '../../types'; import { VariableOptions } from '../shared/VariableOptions'; import { isMulti } from '../../guard'; import { VariablePickerProps } from '../types'; import { formatVariableLabel } from '../../shared/formatVariable'; import { toVariableIdentifier } from '../../state/types'; import { getVariableQueryRunner } from '../../query/VariableQueryRunner'; import { VariableLink } from '../shared/VariableLink'; export const optionPickerFactory = (): ComponentType< VariablePickerProps > => { const mapDispatchToProps = { openOptions, commitChangesToVariable, filterOrSearchOptions, toggleAllOptions, toggleOption, navigateOptions, }; const mapStateToProps = (state: StoreState) => ({ picker: state.templating.optionsPicker, }); const connector = connect(mapStateToProps, mapDispatchToProps); interface OwnProps extends VariablePickerProps {} type Props = OwnProps & ConnectedProps; class OptionsPickerUnconnected extends PureComponent { onShowOptions = () => this.props.openOptions(toVariableIdentifier(this.props.variable), this.props.onVariableChange); onHideOptions = () => this.props.commitChangesToVariable(this.props.onVariableChange); onToggleOption = (option: VariableOption, clearOthers: boolean) => { const toggleFunc = isMulti(this.props.variable) && this.props.variable.multi ? this.onToggleMultiValueVariable : this.onToggleSingleValueVariable; toggleFunc(option, clearOthers); }; onToggleSingleValueVariable = (option: VariableOption, clearOthers: boolean) => { this.props.toggleOption({ option, clearOthers, forceSelect: false }); this.onHideOptions(); }; onToggleMultiValueVariable = (option: VariableOption, clearOthers: boolean) => { this.props.toggleOption({ option, clearOthers, forceSelect: false }); }; render() { const { variable, picker } = this.props; const showOptions = picker.id === variable.id; return (
{showOptions ? this.renderOptions(picker) : this.renderLink(variable)}
); } renderLink(variable: VariableWithOptions) { const linkText = formatVariableLabel(variable); const loading = variable.state === LoadingState.Loading; return ; } onCancel = () => { getVariableQueryRunner().cancelRequest(toVariableIdentifier(this.props.variable)); }; renderOptions(picker: OptionsPickerState) { return ( ); } } const OptionsPicker = connector(OptionsPickerUnconnected); OptionsPicker.displayName = 'OptionsPicker'; return OptionsPicker; };