mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 11:27:36 -02:30
Merge pull request #13708 from marshmalien/13675-code-editor-lastYaml
Match CodeMirror mode to value type on initialization
This commit is contained in:
@@ -6,7 +6,12 @@ import { useField } from 'formik';
|
|||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import { Split, SplitItem, Button, Modal } from '@patternfly/react-core';
|
import { Split, SplitItem, Button, Modal } from '@patternfly/react-core';
|
||||||
import { ExpandArrowsAltIcon } from '@patternfly/react-icons';
|
import { ExpandArrowsAltIcon } from '@patternfly/react-icons';
|
||||||
import { yamlToJson, jsonToYaml, isJsonString } from 'util/yaml';
|
import {
|
||||||
|
yamlToJson,
|
||||||
|
jsonToYaml,
|
||||||
|
isJsonString,
|
||||||
|
parseVariableField,
|
||||||
|
} from 'util/yaml';
|
||||||
import { CheckboxField } from '../FormField';
|
import { CheckboxField } from '../FormField';
|
||||||
import MultiButtonToggle from '../MultiButtonToggle';
|
import MultiButtonToggle from '../MultiButtonToggle';
|
||||||
import CodeEditor from './CodeEditor';
|
import CodeEditor from './CodeEditor';
|
||||||
@@ -37,36 +42,24 @@ function VariablesField({
|
|||||||
// track focus manually, because the Code Editor library doesn't wire
|
// track focus manually, because the Code Editor library doesn't wire
|
||||||
// into Formik completely
|
// into Formik completely
|
||||||
const [shouldValidate, setShouldValidate] = useState(false);
|
const [shouldValidate, setShouldValidate] = useState(false);
|
||||||
const [mode, setMode] = useState(initialMode || YAML_MODE);
|
|
||||||
const validate = useCallback(
|
const validate = useCallback(
|
||||||
(value) => {
|
(value) => {
|
||||||
if (!shouldValidate) {
|
if (!shouldValidate) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (mode === YAML_MODE) {
|
parseVariableField(value);
|
||||||
yamlToJson(value);
|
|
||||||
} else {
|
|
||||||
JSON.parse(value);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return error.message;
|
return error.message;
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
},
|
},
|
||||||
[shouldValidate, mode]
|
[shouldValidate]
|
||||||
);
|
);
|
||||||
const [field, meta, helpers] = useField({ name, validate });
|
const [field, meta, helpers] = useField({ name, validate });
|
||||||
|
const [mode, setMode] = useState(() =>
|
||||||
useEffect(() => {
|
isJsonString(field.value) ? JSON_MODE : initialMode || YAML_MODE
|
||||||
if (isJsonString(field.value)) {
|
);
|
||||||
// mode's useState above couldn't be initialized to JSON_MODE because
|
|
||||||
// the field value had to be defined below it
|
|
||||||
setMode(JSON_MODE);
|
|
||||||
onModeChange(JSON_MODE);
|
|
||||||
helpers.setValue(JSON.stringify(JSON.parse(field.value), null, 2));
|
|
||||||
}
|
|
||||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
() => {
|
() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user