12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- * 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 * as React from 'react';
- import Checkbox from './Checkbox';
-
- interface Props {
- active?: boolean;
- disabled?: boolean;
- element: string;
- onSelect: (element: string) => Promise<void>;
- onUnselect: (element: string) => Promise<void>;
- renderElement: (element: string) => React.ReactNode | [React.ReactNode, React.ReactNode];
- selected: boolean;
- }
-
- interface State {
- loading: boolean;
- }
-
- export default class SelectListListElement 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 });
- }
- };
-
- handleCheck = (checked: boolean) => {
- this.setState({ loading: true });
- const request = checked ? this.props.onSelect : this.props.onUnselect;
- request(this.props.element).then(this.stopLoading, this.stopLoading);
- };
-
- render() {
- let item = this.props.renderElement(this.props.element);
- let extra;
- if (Array.isArray(item)) {
- extra = item[1];
- item = item[0];
- }
- return (
- <li
- className={classNames('display-flex-center', {
- 'select-list-list-disabled': this.props.disabled
- })}>
- <Checkbox
- checked={this.props.selected}
- className={classNames('select-list-list-checkbox flex-1', {
- active: this.props.active
- })}
- disabled={this.props.disabled}
- loading={this.state.loading}
- onCheck={this.handleCheck}>
- <span className="little-spacer-left">{item}</span>
- </Checkbox>
- {extra && <span className="select-list-list-extra">{extra}</span>}
- </li>
- );
- }
- }
|