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.

SearchSelect.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 { debounce } from 'lodash';
  22. import { translate, translateWithParameters } from 'sonar-ui-common/helpers/l10n';
  23. import Select, { Creatable } from './Select';
  24. interface Props<T> {
  25. autofocus?: boolean;
  26. canCreate?: boolean;
  27. className?: string;
  28. clearable?: boolean;
  29. defaultOptions?: T[];
  30. minimumQueryLength?: number;
  31. multi?: boolean;
  32. onSearch: (query: string) => Promise<T[]>;
  33. onSelect?: (option: T) => void;
  34. onMultiSelect?: (options: T[]) => void;
  35. promptTextCreator?: (label: string) => string;
  36. renderOption?: (option: T) => JSX.Element;
  37. resetOnBlur?: boolean;
  38. value?: T | T[];
  39. }
  40. interface State<T> {
  41. loading: boolean;
  42. options: T[];
  43. query: string;
  44. }
  45. export default class SearchSelect<T extends { value: string }> extends React.PureComponent<
  46. Props<T>,
  47. State<T>
  48. > {
  49. mounted = false;
  50. constructor(props: Props<T>) {
  51. super(props);
  52. this.state = { loading: false, options: props.defaultOptions || [], query: '' };
  53. this.handleSearch = debounce(this.handleSearch, 250);
  54. }
  55. componentDidMount() {
  56. this.mounted = true;
  57. }
  58. componentWillUnmount() {
  59. this.mounted = false;
  60. }
  61. get autofocus() {
  62. return this.props.autofocus !== undefined ? this.props.autofocus : true;
  63. }
  64. get minimumQueryLength() {
  65. return this.props.minimumQueryLength !== undefined ? this.props.minimumQueryLength : 2;
  66. }
  67. get resetOnBlur() {
  68. return this.props.resetOnBlur !== undefined ? this.props.resetOnBlur : true;
  69. }
  70. handleSearch = (query: string) => {
  71. // Ignore the result if the query changed
  72. const currentQuery = query;
  73. this.props.onSearch(currentQuery).then(
  74. options => {
  75. if (this.mounted) {
  76. this.setState(state => ({
  77. loading: false,
  78. options: state.query === currentQuery ? options : state.options
  79. }));
  80. }
  81. },
  82. () => {
  83. if (this.mounted) {
  84. this.setState({ loading: false });
  85. }
  86. }
  87. );
  88. };
  89. handleChange = (option: T | T[]) => {
  90. if (Array.isArray(option)) {
  91. if (this.props.onMultiSelect) {
  92. this.props.onMultiSelect(option);
  93. }
  94. } else if (this.props.onSelect) {
  95. this.props.onSelect(option);
  96. }
  97. };
  98. handleInputChange = (query: string) => {
  99. if (query.length >= this.minimumQueryLength) {
  100. this.setState({ loading: true, query });
  101. this.handleSearch(query);
  102. } else {
  103. // `onInputChange` is called with an empty string after a user selects a value
  104. // in this case we shouldn't reset `options`, because it also resets select value :(
  105. const options = (query.length === 0 && this.props.defaultOptions) || [];
  106. this.setState({ options, query });
  107. }
  108. };
  109. // disable internal filtering
  110. handleFilterOption = () => true;
  111. render() {
  112. const Component = this.props.canCreate ? Creatable : Select;
  113. return (
  114. <Component
  115. autoFocus={this.autofocus}
  116. className={this.props.className}
  117. clearable={this.props.clearable}
  118. escapeClearsValue={false}
  119. filterOption={this.handleFilterOption}
  120. isLoading={this.state.loading}
  121. multi={this.props.multi}
  122. noResultsText={
  123. this.state.query.length < this.minimumQueryLength
  124. ? translateWithParameters('select2.tooShort', this.minimumQueryLength)
  125. : translate('select2.noMatches')
  126. }
  127. onBlurResetsInput={this.resetOnBlur}
  128. onChange={this.handleChange}
  129. onInputChange={this.handleInputChange}
  130. optionRenderer={this.props.renderOption}
  131. options={this.state.options}
  132. placeholder={translate('search_verb')}
  133. promptTextCreator={this.props.promptTextCreator}
  134. searchable={true}
  135. value={this.props.value}
  136. valueRenderer={this.props.renderOption}
  137. />
  138. );
  139. }
  140. }