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.

SearchBox.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 classNames from 'classnames';
  21. import { Cancelable, debounce } from 'lodash';
  22. import * as React from 'react';
  23. import { translate, translateWithParameters } from '../../helpers/l10n';
  24. import SearchIcon from '../icons/SearchIcon';
  25. import DeferredSpinner from '../ui/DeferredSpinner';
  26. import { ClearButton } from './buttons';
  27. import './SearchBox.css';
  28. interface Props {
  29. autoFocus?: boolean;
  30. className?: string;
  31. id?: string;
  32. innerRef?: (node: HTMLInputElement | null) => void;
  33. loading?: boolean;
  34. maxLength?: number;
  35. minLength?: number;
  36. onChange: (value: string) => void;
  37. onClick?: React.MouseEventHandler<HTMLInputElement>;
  38. onFocus?: React.FocusEventHandler<HTMLInputElement>;
  39. onKeyDown?: React.KeyboardEventHandler<HTMLInputElement>;
  40. placeholder: string;
  41. value?: string;
  42. }
  43. interface State {
  44. value: string;
  45. }
  46. const DEFAULT_MAX_LENGTH = 100;
  47. export default class SearchBox extends React.PureComponent<Props, State> {
  48. debouncedOnChange: ((query: string) => void) & Cancelable;
  49. input?: HTMLInputElement | null;
  50. constructor(props: Props) {
  51. super(props);
  52. this.state = { value: props.value || '' };
  53. this.debouncedOnChange = debounce(this.props.onChange, 250);
  54. }
  55. componentDidUpdate(prevProps: Props) {
  56. if (
  57. // input is controlled
  58. this.props.value !== undefined &&
  59. // parent is aware of last change
  60. // can happen when previous value was less than min length
  61. this.state.value === prevProps.value &&
  62. this.state.value !== this.props.value
  63. ) {
  64. this.setState({ value: this.props.value });
  65. }
  66. }
  67. changeValue = (value: string, debounced = true) => {
  68. const { minLength } = this.props;
  69. if (value.length === 0) {
  70. // immediately notify when value is empty
  71. this.props.onChange('');
  72. // and cancel scheduled callback
  73. this.debouncedOnChange.cancel();
  74. } else if (!minLength || minLength <= value.length) {
  75. if (debounced) {
  76. this.debouncedOnChange(value);
  77. } else {
  78. this.props.onChange(value);
  79. }
  80. }
  81. };
  82. handleInputChange = (event: React.SyntheticEvent<HTMLInputElement>) => {
  83. const { value } = event.currentTarget;
  84. this.setState({ value });
  85. this.changeValue(value);
  86. };
  87. handleInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
  88. if (event.keyCode === 27) {
  89. // escape
  90. event.preventDefault();
  91. this.handleResetClick();
  92. }
  93. if (this.props.onKeyDown) {
  94. this.props.onKeyDown(event);
  95. }
  96. };
  97. handleResetClick = () => {
  98. this.changeValue('', false);
  99. if (this.props.value === undefined || this.props.value === '') {
  100. this.setState({ value: '' });
  101. }
  102. if (this.input) {
  103. this.input.focus();
  104. }
  105. };
  106. ref = (node: HTMLInputElement | null) => {
  107. this.input = node;
  108. if (this.props.innerRef) {
  109. this.props.innerRef(node);
  110. }
  111. };
  112. render() {
  113. const { loading, minLength, maxLength = DEFAULT_MAX_LENGTH } = this.props;
  114. const { value } = this.state;
  115. const inputClassName = classNames('search-box-input', {
  116. touched: value.length > 0 && (!minLength || minLength > value.length),
  117. });
  118. const tooShort = minLength !== undefined && value.length > 0 && value.length < minLength;
  119. return (
  120. <div
  121. className={classNames('search-box', this.props.className)}
  122. id={this.props.id}
  123. title={tooShort ? translateWithParameters('select2.tooShort', minLength!) : ''}>
  124. <input
  125. aria-label={translate('search_verb')}
  126. autoComplete="off"
  127. autoFocus={this.props.autoFocus}
  128. className={inputClassName}
  129. maxLength={maxLength}
  130. onChange={this.handleInputChange}
  131. onClick={this.props.onClick}
  132. onFocus={this.props.onFocus}
  133. onKeyDown={this.handleInputKeyDown}
  134. placeholder={this.props.placeholder}
  135. ref={this.ref}
  136. type="search"
  137. value={value}
  138. />
  139. <DeferredSpinner loading={loading !== undefined ? loading : false}>
  140. <SearchIcon className="search-box-magnifier" />
  141. </DeferredSpinner>
  142. {value && (
  143. <ClearButton
  144. aria-label={translate('clear')}
  145. className="button-tiny search-box-clear"
  146. iconProps={{ size: 12 }}
  147. onClick={this.handleResetClick}
  148. />
  149. )}
  150. {tooShort && (
  151. <span className="search-box-note">
  152. {translateWithParameters('select2.tooShort', minLength!)}
  153. </span>
  154. )}
  155. </div>
  156. );
  157. }
  158. }