You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiSelector.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { MultiSelectMenu } from './input/MultiSelectMenu';
  21. interface Props {
  22. allowNewElements?: boolean;
  23. allowSearch?: boolean;
  24. createElementLabel: string;
  25. elements: string[];
  26. headerLabel: string;
  27. listSize?: number;
  28. noResultsLabel: string;
  29. onSearch?: (query: string) => Promise<void>;
  30. onSelect: (item: string) => void;
  31. onUnselect: (item: string) => void;
  32. renderTooltip?: (item: string, disabled: boolean) => React.ReactNode;
  33. searchInputAriaLabel: string;
  34. selectedElements: string[];
  35. selectedElementsDisabled?: string[];
  36. }
  37. const LIST_SIZE = 10;
  38. export function MultiSelector(props: Readonly<Props>) {
  39. const {
  40. allowNewElements,
  41. createElementLabel,
  42. selectedElementsDisabled,
  43. headerLabel,
  44. noResultsLabel,
  45. searchInputAriaLabel,
  46. selectedElements,
  47. elements,
  48. allowSearch = true,
  49. renderTooltip,
  50. listSize = LIST_SIZE,
  51. } = props;
  52. return (
  53. <MultiSelectMenu
  54. allowNewElements={allowNewElements}
  55. allowSearch={allowSearch}
  56. createElementLabel={createElementLabel}
  57. elements={elements}
  58. headerNode={<div className="sw-mt-4 sw-font-semibold">{headerLabel}</div>}
  59. listSize={listSize}
  60. noResultsLabel={noResultsLabel}
  61. onSearch={props.onSearch}
  62. onSelect={props.onSelect}
  63. onUnselect={props.onUnselect}
  64. placeholder={searchInputAriaLabel}
  65. renderTooltip={renderTooltip}
  66. searchInputAriaLabel={searchInputAriaLabel}
  67. selectedElements={selectedElements}
  68. selectedElementsDisabled={selectedElementsDisabled}
  69. validateSearchInput={validateElement}
  70. />
  71. );
  72. }
  73. export function validateElement(value: string) {
  74. // Allow only a-z, 0-9, '+', '-', '#', '.'
  75. return value.toLowerCase().replace(/[^-a-z0-9+#.]/gi, '');
  76. }