From 17f2321385d0702a531761ca7819ffee2700d53f Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Fri, 26 Feb 2021 10:27:33 +0100 Subject: [PATCH] SONAR-14498 Better JSON field --- .../components/inputs/InputForJSON.tsx | 71 ++++++++++++++++++ .../components/inputs/PrimitiveInput.tsx | 3 +- .../inputs/__tests__/InputForJSON-test.tsx | 72 +++++++++++++++++++ .../__snapshots__/InputForJSON-test.tsx.snap | 51 +++++++++++++ .../resources/org/sonar/l10n/core.properties | 3 + 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 server/sonar-web/src/main/js/apps/settings/components/inputs/InputForJSON.tsx create mode 100644 server/sonar-web/src/main/js/apps/settings/components/inputs/__tests__/InputForJSON-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/settings/components/inputs/__tests__/__snapshots__/InputForJSON-test.tsx.snap diff --git a/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForJSON.tsx b/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForJSON.tsx new file mode 100644 index 00000000000..701734e8db8 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/settings/components/inputs/InputForJSON.tsx @@ -0,0 +1,71 @@ +/* + * 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 { Button } from 'sonar-ui-common/components/controls/buttons'; +import { Alert } from 'sonar-ui-common/components/ui/Alert'; +import { translate } from 'sonar-ui-common/helpers/l10n'; +import { DefaultSpecializedInputProps } from '../../utils'; + +const JSON_SPACE_SIZE = 4; + +interface State { + formatError: boolean; +} + +export default class InputForJSON extends React.PureComponent { + state: State = { formatError: false }; + + handleInputChange = (event: React.ChangeEvent) => { + this.setState({ formatError: false }); + this.props.onChange(event.target.value); + }; + + format = () => { + const { value } = this.props; + try { + if (value) { + this.props.onChange(JSON.stringify(JSON.parse(value), undefined, JSON_SPACE_SIZE)); + } + } catch (e) { + this.setState({ formatError: true }); + } + }; + + render() { + const { formatError } = this.state; + return ( +
+