123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * SonarQube
- * Copyright (C) 2009-2022 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- import classNames from 'classnames';
- import { uniqueId } from 'lodash';
- import * as React from 'react';
- import { translate } from '../../helpers/l10n';
- import DeferredSpinner from '../ui/DeferredSpinner';
- import Checkbox from './Checkbox';
- import { SelectListFilter } from './SelectList';
- import SelectListListElement from './SelectListListElement';
-
- interface Props {
- allowBulkSelection?: boolean;
- elements: string[];
- disabledElements: string[];
- filter: SelectListFilter;
- onSelect: (element: string) => Promise<void>;
- onUnselect: (element: string) => Promise<void>;
- readOnly?: boolean;
- renderElement: (element: string) => React.ReactNode;
- selectedElements: string[];
- }
-
- interface State {
- loading: boolean;
- }
-
- export default class SelectListListContainer extends React.PureComponent<Props, State> {
- mounted = false;
- state: State = { loading: false };
-
- componentDidMount() {
- this.mounted = true;
- }
-
- componentWillUnmount() {
- this.mounted = false;
- }
-
- stopLoading = () => {
- if (this.mounted) {
- this.setState({ loading: false });
- }
- };
-
- isDisabled = (element: string): boolean => {
- return this.props.readOnly || this.props.disabledElements.includes(element);
- };
-
- isSelected = (element: string): boolean => {
- return this.props.selectedElements.includes(element);
- };
-
- handleBulkChange = (checked: boolean) => {
- this.setState({ loading: true });
- if (checked) {
- Promise.all(this.props.elements.map(element => this.props.onSelect(element)))
- .then(this.stopLoading)
- .catch(this.stopLoading);
- } else {
- Promise.all(this.props.selectedElements.map(element => this.props.onUnselect(element)))
- .then(this.stopLoading)
- .catch(this.stopLoading);
- }
- };
-
- renderBulkSelector() {
- const { elements, readOnly, selectedElements } = this.props;
- return (
- <>
- <li>
- <Checkbox
- checked={selectedElements.length > 0}
- disabled={this.state.loading || readOnly}
- onCheck={this.handleBulkChange}
- thirdState={selectedElements.length > 0 && elements.length !== selectedElements.length}>
- <span className="big-spacer-left">
- {translate('bulk_change')}
- <DeferredSpinner className="spacer-left" loading={this.state.loading} timeout={10} />
- </span>
- </Checkbox>
- </li>
- <li className="divider" />
- </>
- );
- }
-
- render() {
- const { allowBulkSelection, elements, filter } = this.props;
-
- return (
- <div className={classNames('select-list-list-container spacer-top')}>
- <ul className="menu">
- {allowBulkSelection &&
- elements.length > 0 &&
- filter === SelectListFilter.All &&
- this.renderBulkSelector()}
- {elements.map(element => (
- <SelectListListElement
- disabled={this.isDisabled(element)}
- element={element}
- key={uniqueId()}
- onSelect={this.props.onSelect}
- onUnselect={this.props.onUnselect}
- renderElement={this.props.renderElement}
- selected={this.isSelected(element)}
- />
- ))}
- </ul>
- </div>
- );
- }
- }
|