MappingsConfiguration.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { ChangeEvent, useState } from 'react';
  2. import { Button, Icon, InlineField, InlineFieldRow, Input } from '@grafana/ui';
  3. import MappingsHelp from './MappingsHelp';
  4. type Props = {
  5. mappings: string[];
  6. onChange: (mappings: string[]) => void;
  7. onDismiss: () => void;
  8. onRestoreHelp: () => void;
  9. showHelp: boolean;
  10. };
  11. export const MappingsConfiguration = (props: Props): JSX.Element => {
  12. const [mappings, setMappings] = useState(props.mappings || []);
  13. return (
  14. <div>
  15. <h3 className="page-heading">Label mappings</h3>
  16. {!props.showHelp && (
  17. <p>
  18. <Button fill="text" onClick={props.onRestoreHelp}>
  19. Learn how label mappings work
  20. </Button>
  21. </p>
  22. )}
  23. {props.showHelp && <MappingsHelp onDismiss={props.onDismiss} />}
  24. <div className="gf-form-group">
  25. {mappings.map((mapping, i) => (
  26. <InlineFieldRow key={i}>
  27. <InlineField label={`Mapping (${i + 1})`}>
  28. <Input
  29. width={50}
  30. onChange={(changeEvent: ChangeEvent<HTMLInputElement>) => {
  31. let newMappings = mappings.concat();
  32. newMappings[i] = changeEvent.target.value;
  33. setMappings(newMappings);
  34. }}
  35. onBlur={() => {
  36. props.onChange(mappings);
  37. }}
  38. placeholder="e.g. test.metric.(labelName).*"
  39. value={mapping}
  40. />
  41. </InlineField>
  42. <Button
  43. type="button"
  44. aria-label="Remove header"
  45. variant="secondary"
  46. size="xs"
  47. onClick={(_) => {
  48. let newMappings = mappings.concat();
  49. newMappings.splice(i, 1);
  50. setMappings(newMappings);
  51. props.onChange(newMappings);
  52. }}
  53. >
  54. <Icon name="trash-alt" />
  55. </Button>
  56. </InlineFieldRow>
  57. ))}
  58. <Button
  59. variant="secondary"
  60. icon="plus"
  61. type="button"
  62. onClick={() => {
  63. setMappings([...mappings, '']);
  64. }}
  65. >
  66. Add label mapping
  67. </Button>
  68. </div>
  69. </div>
  70. );
  71. };