mirror of
https://github.com/ansible/awx.git
synced 2026-05-15 05:17:36 -02:30
add more robust handling of JSON/YAML toggle to prevent expanding YAML expressions
This commit is contained in:
@@ -11,7 +11,6 @@ import 'ace-builds/src-noconflict/theme-github';
|
|||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import debounce from '../../util/debounce';
|
|
||||||
|
|
||||||
config.set('loadWorkerFromBlob', false);
|
config.set('loadWorkerFromBlob', false);
|
||||||
|
|
||||||
@@ -140,7 +139,8 @@ function CodeEditor({
|
|||||||
mode={aceModes[mode] || 'text'}
|
mode={aceModes[mode] || 'text'}
|
||||||
className={`pf-c-form-control ${className}`}
|
className={`pf-c-form-control ${className}`}
|
||||||
theme="github"
|
theme="github"
|
||||||
onChange={debounce(onChange, 250)}
|
onChange={onChange}
|
||||||
|
debounceChangePeriod={250}
|
||||||
value={value}
|
value={value}
|
||||||
onFocus={onFocus}
|
onFocus={onFocus}
|
||||||
onBlur={onBlur}
|
onBlur={onBlur}
|
||||||
|
|||||||
@@ -73,13 +73,15 @@ function VariablesField({
|
|||||||
},
|
},
|
||||||
[shouldValidate, validate] // eslint-disable-line react-hooks/exhaustive-deps
|
[shouldValidate, validate] // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
);
|
);
|
||||||
const [initialYamlValue] = useState(mode === YAML_MODE ? field.value : null);
|
const [lastYamlValue, setLastYamlValue] = useState(
|
||||||
const [isEdited, setIsEdited] = useState(false);
|
mode === YAML_MODE ? field.value : null
|
||||||
|
);
|
||||||
|
const [isJsonEdited, setIsJsonEdited] = useState(false);
|
||||||
const [isExpanded, setIsExpanded] = useState(false);
|
const [isExpanded, setIsExpanded] = useState(false);
|
||||||
|
|
||||||
const handleModeChange = newMode => {
|
const handleModeChange = newMode => {
|
||||||
if (newMode === YAML_MODE && !isEdited && initialYamlValue) {
|
if (newMode === YAML_MODE && !isJsonEdited && lastYamlValue !== null) {
|
||||||
helpers.setValue(initialYamlValue);
|
helpers.setValue(lastYamlValue);
|
||||||
setMode(newMode);
|
setMode(newMode);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -96,6 +98,16 @@ function VariablesField({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleChange = newVal => {
|
||||||
|
helpers.setValue(newVal);
|
||||||
|
if (mode === JSON_MODE) {
|
||||||
|
setIsJsonEdited(true);
|
||||||
|
} else {
|
||||||
|
setLastYamlValue(newVal);
|
||||||
|
setIsJsonEdited(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<VariablesFieldInternals
|
<VariablesFieldInternals
|
||||||
@@ -109,8 +121,8 @@ function VariablesField({
|
|||||||
onExpand={() => setIsExpanded(true)}
|
onExpand={() => setIsExpanded(true)}
|
||||||
mode={mode}
|
mode={mode}
|
||||||
setMode={handleModeChange}
|
setMode={handleModeChange}
|
||||||
setIsEdited={setIsEdited}
|
|
||||||
setShouldValidate={setShouldValidate}
|
setShouldValidate={setShouldValidate}
|
||||||
|
handleChange={handleChange}
|
||||||
/>
|
/>
|
||||||
<Modal
|
<Modal
|
||||||
variant="xlarge"
|
variant="xlarge"
|
||||||
@@ -141,8 +153,8 @@ function VariablesField({
|
|||||||
fullHeight
|
fullHeight
|
||||||
mode={mode}
|
mode={mode}
|
||||||
setMode={handleModeChange}
|
setMode={handleModeChange}
|
||||||
setIsEdited={setIsEdited}
|
|
||||||
setShouldValidate={setShouldValidate}
|
setShouldValidate={setShouldValidate}
|
||||||
|
handleChange={handleChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
@@ -178,10 +190,10 @@ function VariablesFieldInternals({
|
|||||||
mode,
|
mode,
|
||||||
setMode,
|
setMode,
|
||||||
onExpand,
|
onExpand,
|
||||||
setIsEdited,
|
|
||||||
setShouldValidate,
|
setShouldValidate,
|
||||||
|
handleChange,
|
||||||
}) {
|
}) {
|
||||||
const [field, meta, helpers] = useField(name);
|
const [field, meta] = useField(name);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pf-c-form__group">
|
<div className="pf-c-form__group">
|
||||||
@@ -226,10 +238,7 @@ function VariablesFieldInternals({
|
|||||||
mode={mode}
|
mode={mode}
|
||||||
readOnly={readOnly}
|
readOnly={readOnly}
|
||||||
{...field}
|
{...field}
|
||||||
onChange={newVal => {
|
onChange={handleChange}
|
||||||
setIsEdited(true);
|
|
||||||
helpers.setValue(newVal);
|
|
||||||
}}
|
|
||||||
fullHeight={fullHeight}
|
fullHeight={fullHeight}
|
||||||
onFocus={() => setShouldValidate(false)}
|
onFocus={() => setShouldValidate(false)}
|
||||||
onBlur={() => setShouldValidate(true)}
|
onBlur={() => setShouldValidate(true)}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ describe('VariablesField', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should retain non-expanded yaml if value not edited', async () => {
|
it('should retain non-expanded yaml if JSON value not edited', async () => {
|
||||||
const value = '---\na: &aa [a,b,c]\nb: *aa';
|
const value = '---\na: &aa [a,b,c]\nb: *aa';
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
<Formik initialValues={{ variables: value }}>
|
<Formik initialValues={{ variables: value }}>
|
||||||
@@ -80,7 +80,7 @@ describe('VariablesField', () => {
|
|||||||
expect(wrapper.find('CodeEditor').prop('value')).toEqual(value);
|
expect(wrapper.find('CodeEditor').prop('value')).toEqual(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should retain expanded yaml if value is edited', async () => {
|
it('should retain expanded yaml if JSON value is edited', async () => {
|
||||||
const value = '---\na: &aa [a,b,c]\nb: *aa';
|
const value = '---\na: &aa [a,b,c]\nb: *aa';
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
<Formik initialValues={{ variables: value }}>
|
<Formik initialValues={{ variables: value }}>
|
||||||
@@ -109,6 +109,34 @@ describe('VariablesField', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should retain non-expanded yaml if YAML value is edited', async () => {
|
||||||
|
const value = '---\na: &aa [a,b,c]\nb: *aa';
|
||||||
|
const wrapper = mountWithContexts(
|
||||||
|
<Formik initialValues={{ variables: value }}>
|
||||||
|
{() => (
|
||||||
|
<VariablesField id="the-field" name="variables" label="Variables" />
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
);
|
||||||
|
wrapper.find('CodeEditor').invoke('onChange')(
|
||||||
|
'---\na: &aa [a,b,c]\nb: *aa\n'
|
||||||
|
);
|
||||||
|
const buttons = wrapper.find('Button');
|
||||||
|
await act(async () => {
|
||||||
|
buttons.at(1).simulate('click');
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
const buttons2 = wrapper.find('Button');
|
||||||
|
await act(async () => {
|
||||||
|
buttons2.at(0).simulate('click');
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
expect(wrapper.find('CodeEditor').prop('mode')).toEqual('yaml');
|
||||||
|
expect(wrapper.find('CodeEditor').prop('value')).toEqual(
|
||||||
|
'---\na: &aa [a,b,c]\nb: *aa\n'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('should set Formik error if yaml is invalid', async () => {
|
it('should set Formik error if yaml is invalid', async () => {
|
||||||
const value = '---\nfoo bar\n';
|
const value = '---\nfoo bar\n';
|
||||||
const wrapper = mountWithContexts(
|
const wrapper = mountWithContexts(
|
||||||
|
|||||||
Reference in New Issue
Block a user