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.

SelectList.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 * as React from 'react';
  21. import { translate } from '../../helpers/l10n';
  22. import ButtonToggle from './ButtonToggle';
  23. import ListFooter from './ListFooter';
  24. import SearchBox from './SearchBox';
  25. import './SelectList.css';
  26. import SelectListListContainer from './SelectListListContainer';
  27. export enum SelectListFilter {
  28. All = 'all',
  29. Selected = 'selected',
  30. Unselected = 'deselected'
  31. }
  32. interface Props {
  33. allowBulkSelection?: boolean;
  34. elements: string[];
  35. elementsTotalCount?: number;
  36. disabledElements?: string[];
  37. labelSelected?: string;
  38. labelUnselected?: string;
  39. labelAll?: string;
  40. needToReload?: boolean;
  41. onSearch: (searchParams: SelectListSearchParams) => Promise<void>;
  42. onSelect: (element: string) => Promise<void>;
  43. onUnselect: (element: string) => Promise<void>;
  44. pageSize?: number;
  45. readOnly?: boolean;
  46. renderElement: (element: string) => React.ReactNode;
  47. selectedElements: string[];
  48. withPaging?: boolean;
  49. }
  50. export interface SelectListSearchParams {
  51. filter: SelectListFilter;
  52. page?: number;
  53. pageSize?: number;
  54. query: string;
  55. }
  56. interface State {
  57. lastSearchParams: SelectListSearchParams;
  58. loading: boolean;
  59. }
  60. const DEFAULT_PAGE_SIZE = 100;
  61. export default class SelectList extends React.PureComponent<Props, State> {
  62. mounted = false;
  63. constructor(props: Props) {
  64. super(props);
  65. this.state = {
  66. lastSearchParams: {
  67. filter: SelectListFilter.Selected,
  68. page: 1,
  69. pageSize: props.pageSize ? props.pageSize : DEFAULT_PAGE_SIZE,
  70. query: ''
  71. },
  72. loading: false
  73. };
  74. }
  75. componentDidMount() {
  76. this.mounted = true;
  77. this.search({});
  78. }
  79. componentWillUnmount() {
  80. this.mounted = false;
  81. }
  82. stopLoading = () => {
  83. if (this.mounted) {
  84. this.setState({ loading: false });
  85. }
  86. };
  87. getFilter = () =>
  88. this.state.lastSearchParams.query === ''
  89. ? this.state.lastSearchParams.filter
  90. : SelectListFilter.All;
  91. search = (searchParams: Partial<SelectListSearchParams>) =>
  92. this.setState(
  93. prevState => ({
  94. loading: true,
  95. lastSearchParams: { ...prevState.lastSearchParams, ...searchParams }
  96. }),
  97. () =>
  98. this.props
  99. .onSearch({
  100. filter: this.getFilter(),
  101. page: this.props.withPaging ? this.state.lastSearchParams.page : undefined,
  102. pageSize: this.props.withPaging ? this.state.lastSearchParams.pageSize : undefined,
  103. query: this.state.lastSearchParams.query
  104. })
  105. .then(this.stopLoading)
  106. .catch(this.stopLoading)
  107. );
  108. changeFilter = (filter: SelectListFilter) => this.search({ filter, page: 1 });
  109. handleQueryChange = (query: string) => this.search({ page: 1, query });
  110. onLoadMore = () =>
  111. this.search({
  112. page:
  113. this.state.lastSearchParams.page != null ? this.state.lastSearchParams.page + 1 : undefined
  114. });
  115. onReload = () => this.search({ page: 1 });
  116. render() {
  117. const {
  118. labelSelected = translate('selected'),
  119. labelUnselected = translate('unselected'),
  120. labelAll = translate('all')
  121. } = this.props;
  122. const { filter } = this.state.lastSearchParams;
  123. const disabled = this.state.lastSearchParams.query !== '';
  124. return (
  125. <div className="select-list">
  126. <div className="display-flex-center">
  127. <span className="select-list-filter spacer-right">
  128. <ButtonToggle
  129. onCheck={this.changeFilter}
  130. disabled={disabled}
  131. options={[
  132. { label: labelSelected, value: SelectListFilter.Selected },
  133. { label: labelUnselected, value: SelectListFilter.Unselected },
  134. { label: labelAll, value: SelectListFilter.All }
  135. ]}
  136. value={filter}
  137. />
  138. </span>
  139. <SearchBox
  140. autoFocus={true}
  141. loading={this.state.loading}
  142. onChange={this.handleQueryChange}
  143. placeholder={translate('search_verb')}
  144. value={this.state.lastSearchParams.query}
  145. />
  146. </div>
  147. <SelectListListContainer
  148. allowBulkSelection={this.props.allowBulkSelection}
  149. disabledElements={this.props.disabledElements || []}
  150. elements={this.props.elements}
  151. filter={this.getFilter()}
  152. onSelect={this.props.onSelect}
  153. onUnselect={this.props.onUnselect}
  154. readOnly={this.props.readOnly}
  155. renderElement={this.props.renderElement}
  156. selectedElements={this.props.selectedElements}
  157. />
  158. {!!this.props.elementsTotalCount && (
  159. <ListFooter
  160. count={this.props.elements.length}
  161. loadMore={this.onLoadMore}
  162. needReload={this.props.needToReload}
  163. reload={this.onReload}
  164. total={this.props.elementsTotalCount}
  165. />
  166. )}
  167. </div>
  168. );
  169. }
  170. }