searchBar.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. import Button from 'sentry/components/button';
  5. import Input from 'sentry/components/forms/controls/input';
  6. import {IconSearch} from 'sentry/icons';
  7. import {IconClose} from 'sentry/icons/iconClose';
  8. import {t} from 'sentry/locale';
  9. import {callIfFunction} from 'sentry/utils/callIfFunction';
  10. type DefaultProps = {
  11. defaultQuery: string;
  12. onSearch: (query: string) => void;
  13. query: string;
  14. };
  15. type Props = DefaultProps & {
  16. onChange?: (query: string) => void;
  17. width?: string;
  18. } & Omit<React.ComponentProps<typeof Input>, 'onChange'>;
  19. type State = {
  20. dropdownVisible: boolean;
  21. query: string;
  22. };
  23. class SearchBar extends React.PureComponent<Props, State> {
  24. static defaultProps: DefaultProps = {
  25. query: '',
  26. defaultQuery: '',
  27. onSearch: function () {},
  28. };
  29. state: State = {
  30. query: this.props.query || this.props.defaultQuery,
  31. dropdownVisible: false,
  32. };
  33. UNSAFE_componentWillReceiveProps(nextProps: Props) {
  34. if (nextProps.query !== this.props.query) {
  35. this.setState({
  36. query: nextProps.query,
  37. });
  38. }
  39. }
  40. searchInputRef = React.createRef<HTMLInputElement>();
  41. blur = () => {
  42. if (this.searchInputRef.current) {
  43. this.searchInputRef.current.blur();
  44. }
  45. };
  46. onSubmit = (evt: React.FormEvent<HTMLFormElement>) => {
  47. evt.preventDefault();
  48. this.blur();
  49. this.props.onSearch(this.state.query);
  50. };
  51. clearSearch = () => {
  52. this.setState({query: this.props.defaultQuery}, () => {
  53. this.props.onSearch(this.state.query);
  54. callIfFunction(this.props.onChange, this.state.query);
  55. });
  56. };
  57. onQueryFocus = () => {
  58. this.setState({
  59. dropdownVisible: true,
  60. });
  61. };
  62. onQueryBlur = () => {
  63. this.setState({dropdownVisible: false});
  64. };
  65. onQueryChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
  66. const {value} = evt.target;
  67. this.setState({query: value});
  68. callIfFunction(this.props.onChange, value);
  69. };
  70. render() {
  71. // Remove keys that should not be passed into Input
  72. const {
  73. className,
  74. width,
  75. query: _q,
  76. defaultQuery,
  77. onChange: _oC,
  78. onSearch: _oS,
  79. ...inputProps
  80. } = this.props;
  81. return (
  82. <div className={classNames('search', className)}>
  83. <form className="form-horizontal" onSubmit={this.onSubmit}>
  84. <div>
  85. <StyledInput
  86. {...inputProps}
  87. type="text"
  88. className="search-input"
  89. name="query"
  90. ref={this.searchInputRef}
  91. autoComplete="off"
  92. value={this.state.query}
  93. onBlur={this.onQueryBlur}
  94. onChange={this.onQueryChange}
  95. width={width}
  96. />
  97. <StyledIconSearch className="search-input-icon" size="sm" color="gray300" />
  98. {this.state.query !== defaultQuery && (
  99. <SearchClearButton
  100. type="button"
  101. className="search-clear-form"
  102. priority="link"
  103. onClick={this.clearSearch}
  104. size="xsmall"
  105. icon={<IconClose />}
  106. aria-label={t('Clear')}
  107. />
  108. )}
  109. </div>
  110. </form>
  111. </div>
  112. );
  113. }
  114. }
  115. const StyledInput = styled(Input)`
  116. width: ${p => (p.width ? p.width : undefined)};
  117. &.focus-visible {
  118. box-shadow: 0 0 0 1px ${p => p.theme.focusBorder};
  119. border-color: ${p => p.theme.focusBorder};
  120. outline: none;
  121. }
  122. `;
  123. const StyledIconSearch = styled(IconSearch)`
  124. position: absolute;
  125. top: 50%;
  126. transform: translateY(-50%);
  127. left: 14px;
  128. `;
  129. const SearchClearButton = styled(Button)`
  130. position: absolute;
  131. top: 50%;
  132. height: 16px;
  133. transform: translateY(-50%);
  134. right: 10px;
  135. font-size: ${p => p.theme.fontSizeLarge};
  136. color: ${p => p.theme.gray200};
  137. &:hover {
  138. color: ${p => p.theme.gray300};
  139. }
  140. `;
  141. export default SearchBar;