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.

SelectListListContainer.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { uniqueId } from 'lodash';
  22. import * as React from 'react';
  23. import { translate } from '../../helpers/l10n';
  24. import DeferredSpinner from '../ui/DeferredSpinner';
  25. import Checkbox from './Checkbox';
  26. import { SelectListFilter } from './SelectList';
  27. import SelectListListElement from './SelectListListElement';
  28. interface Props {
  29. allowBulkSelection?: boolean;
  30. elements: string[];
  31. disabledElements: string[];
  32. filter: SelectListFilter;
  33. onSelect: (element: string) => Promise<void>;
  34. onUnselect: (element: string) => Promise<void>;
  35. readOnly?: boolean;
  36. renderElement: (element: string) => React.ReactNode;
  37. selectedElements: string[];
  38. }
  39. interface State {
  40. loading: boolean;
  41. }
  42. export default class SelectListListContainer extends React.PureComponent<Props, State> {
  43. mounted = false;
  44. state: State = { loading: false };
  45. componentDidMount() {
  46. this.mounted = true;
  47. }
  48. componentWillUnmount() {
  49. this.mounted = false;
  50. }
  51. stopLoading = () => {
  52. if (this.mounted) {
  53. this.setState({ loading: false });
  54. }
  55. };
  56. isDisabled = (element: string): boolean => {
  57. return this.props.readOnly || this.props.disabledElements.includes(element);
  58. };
  59. isSelected = (element: string): boolean => {
  60. return this.props.selectedElements.includes(element);
  61. };
  62. handleBulkChange = (checked: boolean) => {
  63. this.setState({ loading: true });
  64. if (checked) {
  65. Promise.all(this.props.elements.map(element => this.props.onSelect(element)))
  66. .then(this.stopLoading)
  67. .catch(this.stopLoading);
  68. } else {
  69. Promise.all(this.props.selectedElements.map(element => this.props.onUnselect(element)))
  70. .then(this.stopLoading)
  71. .catch(this.stopLoading);
  72. }
  73. };
  74. renderBulkSelector() {
  75. const { elements, readOnly, selectedElements } = this.props;
  76. return (
  77. <>
  78. <li>
  79. <Checkbox
  80. checked={selectedElements.length > 0}
  81. disabled={this.state.loading || readOnly}
  82. onCheck={this.handleBulkChange}
  83. thirdState={selectedElements.length > 0 && elements.length !== selectedElements.length}>
  84. <span className="big-spacer-left">
  85. {translate('bulk_change')}
  86. <DeferredSpinner className="spacer-left" loading={this.state.loading} timeout={10} />
  87. </span>
  88. </Checkbox>
  89. </li>
  90. <li className="divider" />
  91. </>
  92. );
  93. }
  94. render() {
  95. const { allowBulkSelection, elements, filter } = this.props;
  96. return (
  97. <div className={classNames('select-list-list-container spacer-top')}>
  98. <ul className="menu">
  99. {allowBulkSelection &&
  100. elements.length > 0 &&
  101. filter === SelectListFilter.All &&
  102. this.renderBulkSelector()}
  103. {elements.map(element => (
  104. <SelectListListElement
  105. disabled={this.isDisabled(element)}
  106. element={element}
  107. key={uniqueId()}
  108. onSelect={this.props.onSelect}
  109. onUnselect={this.props.onUnselect}
  110. renderElement={this.props.renderElement}
  111. selected={this.isSelected(element)}
  112. />
  113. ))}
  114. </ul>
  115. </div>
  116. );
  117. }
  118. }