123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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 * as React from 'react';
- import { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/controls/buttons';
- import Select, { Creatable } from 'sonar-ui-common/components/controls/Select';
- import SimpleModal from 'sonar-ui-common/components/controls/SimpleModal';
- import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner';
- import MandatoryFieldMarker from 'sonar-ui-common/components/ui/MandatoryFieldMarker';
- import MandatoryFieldsExplanation from 'sonar-ui-common/components/ui/MandatoryFieldsExplanation';
- import { translate } from 'sonar-ui-common/helpers/l10n';
-
- export interface MetricProps {
- description: string;
- domain?: string;
- key: string;
- name: string;
- type: string;
- }
-
- interface Props {
- confirmButtonText: string;
- domains: string[];
- metric?: T.Metric;
- header: string;
- onClose: () => void;
- onSubmit: (data: MetricProps) => Promise<void>;
- types: string[];
- }
-
- interface State extends MetricProps {}
-
- export default class Form extends React.PureComponent<Props, State> {
- constructor(props: Props) {
- super(props);
- this.state = {
- description: (props.metric && props.metric.description) || '',
- domain: props.metric && props.metric.domain,
- key: (props.metric && props.metric.key) || '',
- name: (props.metric && props.metric.name) || '',
- type: (props.metric && props.metric.type) || 'INT'
- };
- }
-
- handleSubmit = () => {
- return this.props
- .onSubmit({
- description: this.state.description,
- domain: this.state.domain,
- key: this.state.key,
- name: this.state.name,
- type: this.state.type
- })
- .then(this.props.onClose);
- };
-
- handleKeyChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- this.setState({ key: event.currentTarget.value });
- };
-
- handleDescriptionChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
- this.setState({ description: event.currentTarget.value });
- };
-
- handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- this.setState({ name: event.currentTarget.value });
- };
-
- handleDomainChange = (option: { value: string } | null) => {
- this.setState({ domain: option ? option.value : undefined });
- };
-
- handleTypeChange = ({ value }: { value: string }) => {
- this.setState({ type: value });
- };
-
- render() {
- const domains = [...this.props.domains];
- if (this.state.domain) {
- domains.push(this.state.domain);
- }
-
- return (
- <SimpleModal
- header={this.props.header}
- onClose={this.props.onClose}
- onSubmit={this.handleSubmit}
- size="small">
- {({ onCloseClick, onFormSubmit, submitting }) => (
- <form onSubmit={onFormSubmit}>
- <header className="modal-head">
- <h2>{this.props.header}</h2>
- </header>
-
- <div className="modal-body modal-container">
- <MandatoryFieldsExplanation className="modal-field" />
-
- <div className="modal-field">
- <label htmlFor="create-metric-key">
- {translate('key')}
- <MandatoryFieldMarker />
- </label>
- <input
- autoFocus={true}
- id="create-metric-key"
- maxLength={64}
- name="key"
- onChange={this.handleKeyChange}
- required={true}
- type="text"
- value={this.state.key}
- />
- </div>
- <div className="modal-field">
- <label htmlFor="create-metric-name">
- {translate('name')}
- <MandatoryFieldMarker />
- </label>
- <input
- id="create-metric-name"
- maxLength={64}
- name="name"
- onChange={this.handleNameChange}
- required={true}
- type="text"
- value={this.state.name}
- />
- </div>
- <div className="modal-field">
- <label htmlFor="create-metric-description">{translate('description')}</label>
- <textarea
- id="create-metric-description"
- name="description"
- onChange={this.handleDescriptionChange}
- value={this.state.description}
- />
- </div>
- <div className="modal-field">
- <label htmlFor="create-metric-domain">{translate('custom_metrics.domain')}</label>
- <Creatable
- id="create-metric-domain"
- onChange={this.handleDomainChange}
- options={domains.map(domain => ({ label: domain, value: domain }))}
- value={this.state.domain}
- />
- </div>
- <div className="modal-field">
- <label htmlFor="create-metric-type">
- {translate('type')}
- <MandatoryFieldMarker />
- </label>
- <Select
- clearable={false}
- id="create-metric-type"
- onChange={this.handleTypeChange}
- options={this.props.types.map(type => ({
- label: translate('metric.type', type),
- value: type
- }))}
- value={this.state.type}
- />
- </div>
- </div>
-
- <footer className="modal-foot">
- <DeferredSpinner className="spacer-right" loading={submitting} />
- <SubmitButton disabled={submitting} id="create-metric-submit">
- {this.props.confirmButtonText}
- </SubmitButton>
- <ResetButtonLink
- disabled={submitting}
- id="create-metric-cancel"
- onClick={onCloseClick}>
- {translate('cancel')}
- </ResetButtonLink>
- </footer>
- </form>
- )}
- </SimpleModal>
- );
- }
- }
|