IconSelector.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React, { useState, useEffect } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { Select } from '@grafana/ui';
  5. interface Props {
  6. value: string;
  7. onChange: (v: string) => void;
  8. }
  9. const IconSelector: React.FC<Props> = ({ value, onChange }) => {
  10. const [icons, setIcons] = useState<SelectableValue[]>(value ? [{ value, label: value }] : []);
  11. const [icon, setIcon] = useState<string>();
  12. const iconRoot = (window as any).__grafana_public_path__ + 'img/icons/unicons/';
  13. const onChangeIcon = (value: string) => {
  14. onChange(value);
  15. setIcon(value);
  16. };
  17. useEffect(() => {
  18. getBackendSrv()
  19. .get(`${iconRoot}/index.json`)
  20. .then((data) => {
  21. setIcons(
  22. data.files.map((icon: string) => ({
  23. value: icon,
  24. label: icon,
  25. }))
  26. );
  27. });
  28. }, [iconRoot]);
  29. return (
  30. <Select
  31. options={icons}
  32. value={icon}
  33. onChange={(selectedValue) => {
  34. onChangeIcon(selectedValue.value!);
  35. }}
  36. />
  37. );
  38. };
  39. export default IconSelector;