RuleSettingsArray.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React, { useState } from 'react';
  2. import { Select } from '@grafana/ui';
  3. import { SelectableValue } from '../../../../../packages/grafana-data/src';
  4. import { RuleSettingsEditor } from './RuleSettingsEditor';
  5. import { RuleType, RuleSetting, PipeLineEntitiesInfo } from './types';
  6. interface Props {
  7. ruleType: RuleType;
  8. onChange: (value: RuleSetting[]) => void;
  9. value: RuleSetting[];
  10. entitiesInfo: PipeLineEntitiesInfo;
  11. }
  12. export const RuleSettingsArray: React.FC<Props> = ({ onChange, value, ruleType, entitiesInfo }) => {
  13. const [index, setIndex] = useState<number>(0);
  14. const arr = value ?? [];
  15. const onRuleChange = (v: RuleSetting) => {
  16. if (!value) {
  17. onChange([v]);
  18. } else {
  19. const copy = [...value];
  20. copy[index] = v;
  21. onChange(copy);
  22. }
  23. };
  24. // create array of value.length + 1
  25. let indexArr: Array<SelectableValue<number>> = [];
  26. for (let i = 0; i <= arr.length; i++) {
  27. indexArr.push({
  28. label: `${ruleType}: ${i}`,
  29. value: i,
  30. });
  31. }
  32. return (
  33. <>
  34. <Select
  35. placeholder="Select an index"
  36. options={indexArr}
  37. value={index}
  38. onChange={(index) => {
  39. // set index to find the correct setting
  40. setIndex(index.value!);
  41. }}
  42. ></Select>
  43. <RuleSettingsEditor onChange={onRuleChange} value={arr[index]} ruleType={ruleType} entitiesInfo={entitiesInfo} />
  44. </>
  45. );
  46. };