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.

Select.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 * as React from 'react';
  21. import {
  22. defaultFilterOptions as reactSelectDefaultFilterOptions,
  23. ReactAsyncSelectProps,
  24. ReactCreatableSelectProps,
  25. ReactSelectProps,
  26. } from 'react-select';
  27. import { lazyLoadComponent } from '../lazyLoadComponent';
  28. import { ClearButton } from './buttons';
  29. import './Select.css';
  30. declare module 'react-select' {
  31. export function defaultFilterOptions(...args: any[]): any;
  32. }
  33. const ReactSelectLib = import('react-select');
  34. const ReactSelect = lazyLoadComponent(() => ReactSelectLib);
  35. const ReactCreatable = lazyLoadComponent(() =>
  36. ReactSelectLib.then((lib) => ({ default: lib.Creatable }))
  37. );
  38. const ReactAsync = lazyLoadComponent(() => ReactSelectLib.then((lib) => ({ default: lib.Async })));
  39. function renderInput() {
  40. return <ClearButton className="button-tiny spacer-left text-middle" iconProps={{ size: 12 }} />;
  41. }
  42. interface WithInnerRef {
  43. innerRef?: (element: React.Component) => void;
  44. }
  45. export default function Select({ innerRef, ...props }: WithInnerRef & ReactSelectProps) {
  46. // TODO try to define good defaults, if any
  47. // ReactSelect doesn't declare `clearRenderer` prop
  48. const ReactSelectAny = ReactSelect as any;
  49. // hide the "x" icon when select is empty
  50. const clearable = props.clearable ? Boolean(props.value) : false;
  51. return (
  52. <ReactSelectAny {...props} clearable={clearable} clearRenderer={renderInput} ref={innerRef} />
  53. );
  54. }
  55. export const defaultFilterOptions = reactSelectDefaultFilterOptions;
  56. export function Creatable(props: ReactCreatableSelectProps) {
  57. // ReactSelect doesn't declare `clearRenderer` prop
  58. const ReactCreatableAny = ReactCreatable as any;
  59. return <ReactCreatableAny {...props} clearRenderer={renderInput} />;
  60. }
  61. // TODO figure out why `ref` prop is incompatible
  62. export function AsyncSelect(props: ReactAsyncSelectProps & { ref?: any }) {
  63. return <ReactAsync {...props} />;
  64. }