RepeatRowSelect.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { FC, useCallback, useMemo } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import { SelectableValue } from '@grafana/data';
  4. import { Select } from '@grafana/ui';
  5. import { StoreState } from '../../../../types';
  6. import { getLastKey, getVariablesByKey } from '../../../variables/state/selectors';
  7. export interface Props {
  8. id?: string;
  9. repeat?: string | null;
  10. onChange: (name: string | null) => void;
  11. }
  12. export const RepeatRowSelect: FC<Props> = ({ repeat, onChange, id }) => {
  13. const variables = useSelector((state: StoreState) => {
  14. return getVariablesByKey(getLastKey(state), state);
  15. });
  16. const variableOptions = useMemo(() => {
  17. const options = variables.map((item: any) => {
  18. return { label: item.name, value: item.name };
  19. });
  20. if (options.length === 0) {
  21. options.unshift({
  22. label: 'No template variables found',
  23. value: null,
  24. });
  25. }
  26. options.unshift({
  27. label: 'Disable repeating',
  28. value: null,
  29. });
  30. return options;
  31. }, [variables]);
  32. const onSelectChange = useCallback((option: SelectableValue<string | null>) => onChange(option.value!), [onChange]);
  33. return <Select inputId={id} value={repeat} onChange={onSelectChange} options={variableOptions} />;
  34. };