import { css } from '@emotion/css'; import React, { MouseEvent, useCallback, useEffect, useState } from 'react'; import { GrafanaTheme } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { Button, InlineFieldRow, InlineLabel, useStyles, VerticalGroup } from '@grafana/ui'; import { VariableOption, VariableWithOptions } from '../types'; export interface VariableValuesPreviewProps { variable: VariableWithOptions; } export const VariableValuesPreview: React.FunctionComponent = ({ variable: { options }, }) => { const [previewLimit, setPreviewLimit] = useState(20); const [previewOptions, setPreviewOptions] = useState([]); const showMoreOptions = useCallback( (event: MouseEvent) => { event.preventDefault(); setPreviewLimit(previewLimit + 20); }, [previewLimit, setPreviewLimit] ); const styles = useStyles(getStyles); useEffect(() => setPreviewOptions(options.slice(0, previewLimit)), [previewLimit, options]); if (!previewOptions.length) { return null; } return (
Preview of values
{previewOptions.map((o, index) => (
{o.text}
))}
{options.length > previewLimit && ( )}
); }; VariableValuesPreview.displayName = 'VariableValuesPreview'; function getStyles(theme: GrafanaTheme) { return { optionContainer: css` margin-left: ${theme.spacing.xs}; margin-bottom: ${theme.spacing.xs}; `, label: css` white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 50vw; `, }; }