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.5KB

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