diff --git a/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.test.tsx b/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.test.tsx
index 562f15301590b..7d79200bc6f87 100644
--- a/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.test.tsx
+++ b/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.test.tsx
@@ -6,10 +6,12 @@
* Side Public License, v 1.
*/
+import React, { useState, useMemo } from 'react';
import { act } from 'react-dom/test-utils';
import '../../test_utils/setup_environment';
import { registerTestBed, TestBed, getCommonActions } from '../../test_utils';
+import { RuntimeFieldPainlessError } from '../../lib';
import { Field } from '../../types';
import { FieldEditor, Props, FieldEditorFormState } from './field_editor';
@@ -208,5 +210,66 @@ describe('', () => {
expect(getLastStateUpdate().isValid).toBe(true);
expect(form.getErrorsMessages()).toEqual([]);
});
+
+ test('should clear the painless syntax error whenever the field type changes', async () => {
+ const field: Field = {
+ name: 'myRuntimeField',
+ type: 'keyword',
+ script: { source: 'emit(6)' },
+ };
+
+ const TestComponent = () => {
+ const dummyError = {
+ reason: 'Awwww! Painless syntax error',
+ message: '',
+ position: { offset: 0, start: 0, end: 0 },
+ scriptStack: [''],
+ };
+ const [error, setError] = useState(null);
+ const clearError = useMemo(() => () => setError(null), []);
+ const syntaxError = useMemo(() => ({ error, clear: clearError }), [error, clearError]);
+
+ return (
+ <>
+
+
+ {/* Button to forward dummy syntax error */}
+
+ >
+ );
+ };
+
+ const customTestbed = registerTestBed(TestComponent, {
+ memoryRouter: {
+ wrapComponent: false,
+ },
+ })() as TestBed;
+
+ testBed = {
+ ...customTestbed,
+ actions: getCommonActions(customTestbed),
+ };
+
+ const {
+ form,
+ component,
+ find,
+ actions: { changeFieldType },
+ } = testBed;
+
+ // We set some dummy painless error
+ act(() => {
+ find('setPainlessErrorButton').simulate('click');
+ });
+ component.update();
+
+ expect(form.getErrorsMessages()).toEqual(['Awwww! Painless syntax error']);
+
+ // We change the type and expect the form error to not be there anymore
+ await changeFieldType('long');
+ expect(form.getErrorsMessages()).toEqual([]);
+ });
});
});
diff --git a/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.tsx b/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.tsx
index afb87bd1e7334..3785096e20627 100644
--- a/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.tsx
+++ b/src/plugins/index_pattern_field_editor/public/components/field_editor/field_editor.tsx
@@ -21,6 +21,7 @@ import type { CoreStart } from 'src/core/public';
import {
Form,
useForm,
+ useFormData,
FormHook,
UseField,
TextField,
@@ -184,6 +185,9 @@ const FieldEditorComponent = ({
serializer: formSerializer,
});
const { submit, isValid: isFormValid, isSubmitted } = form;
+ const { clear: clearSyntaxError } = syntaxError;
+
+ const [{ type }] = useFormData({ form });
const nameFieldConfig = getNameFieldConfig(namesNotAllowed, field);
const i18nTexts = geti18nTexts();
@@ -194,6 +198,12 @@ const FieldEditorComponent = ({
}
}, [onChange, isFormValid, isSubmitted, submit]);
+ useEffect(() => {
+ // Whenever the field "type" changes we clear any possible painless syntax
+ // error as it is possibly stale.
+ clearSyntaxError();
+ }, [type, clearSyntaxError]);
+
return (