Skip to content

Commit bb56efa

Browse files
committed
Hide _meta json editor behind toggle
1 parent 9499417 commit bb56efa

File tree

6 files changed

+70
-30
lines changed

6 files changed

+70
-30
lines changed

src/plugins/es_ui_shared/static/forms/components/form_row.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ export const FormRow = ({
5757
titleWrapped = title;
5858
}
5959

60-
if (!children && !field) {
61-
throw new Error('You need to provide either children or a field to the FormRow');
62-
}
63-
6460
return (
6561
<EuiDescribedFormGroup title={titleWrapped} description={description} fullWidth>
66-
{children ? children : <Field field={field!} {...rest} />}
62+
{children ? children : field ? <Field field={field!} {...rest} /> : null}
6763
</EuiDescribedFormGroup>
6864
);
6965
};

x-pack/plugins/index_management/public/application/components/component_templates/component_template_wizard/component_template_form/steps/step_logistics.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const StepLogistics: React.FunctionComponent<Props> = React.memo(
200200
</>
201201
}
202202
>
203-
{isMetaVisible ? (
203+
{isMetaVisible && (
204204
<UseField
205205
path="_meta"
206206
component={JsonEditorField}
@@ -211,16 +211,12 @@ export const StepLogistics: React.FunctionComponent<Props> = React.memo(
211211
'aria-label': i18n.translate(
212212
'xpack.idxMgmt.componentTemplateForm.stepLogistics.metaAriaLabel',
213213
{
214-
defaultMessage: 'Metadata JSON editor',
214+
defaultMessage: '_meta field data editor',
215215
}
216216
),
217217
},
218218
}}
219219
/>
220-
) : (
221-
// <FormRow/> requires children or a field
222-
// For now, we return an empty <div> if the editor is not visible
223-
<div />
224220
)}
225221
</FormRow>
226222
</Form>

x-pack/plugins/index_management/public/application/components/component_templates/component_template_wizard/component_template_form/steps/step_logistics_schema.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const logisticsFormSchema: FormSchema = {
6565
},
6666
_meta: {
6767
label: i18n.translate('xpack.idxMgmt.componentTemplateForm.stepLogistics.metaFieldLabel', {
68-
defaultMessage: 'Metadata (optional)',
68+
defaultMessage: '_meta field data (optional)',
6969
}),
7070
helpText: (
7171
<FormattedMessage

x-pack/plugins/index_management/public/application/components/template_form/steps/step_logistics.tsx

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Field,
1717
Forms,
1818
JsonEditorField,
19+
FormDataProvider,
1920
} from '../../../../shared_imports';
2021
import { documentationService } from '../../../services/documentation';
2122
import { schemas, nameConfig, nameConfigWithoutValidations } from '../template_form_schemas';
@@ -57,10 +58,10 @@ const fieldsMeta = {
5758
},
5859
priority: {
5960
title: i18n.translate('xpack.idxMgmt.templateForm.stepLogistics.priorityTitle', {
60-
defaultMessage: 'Merge priority',
61+
defaultMessage: 'Priority',
6162
}),
6263
description: i18n.translate('xpack.idxMgmt.templateForm.stepLogistics.priorityDescription', {
63-
defaultMessage: 'The merge priority when multiple templates match an index.',
64+
defaultMessage: 'Only the highest priority template will be applied.',
6465
}),
6566
testSubject: 'priorityField',
6667
},
@@ -75,19 +76,45 @@ const fieldsMeta = {
7576
},
7677
};
7778

79+
interface LogisticsForm {
80+
[key: string]: any;
81+
}
82+
83+
interface LogisticsFormInternal extends LogisticsForm {
84+
__internal__: {
85+
addMeta: boolean;
86+
};
87+
}
88+
7889
interface Props {
79-
defaultValue: { [key: string]: any };
90+
defaultValue: LogisticsForm;
8091
onChange: (content: Forms.Content) => void;
8192
isEditing?: boolean;
8293
isLegacy?: boolean;
8394
}
8495

96+
function formDeserializer(formData: LogisticsForm): LogisticsFormInternal {
97+
return {
98+
...formData,
99+
__internal__: {
100+
addMeta: Boolean(formData._meta && Object.keys(formData._meta).length),
101+
},
102+
};
103+
}
104+
105+
function formSerializer(formData: LogisticsFormInternal): LogisticsForm {
106+
const { __internal__, ...data } = formData;
107+
return data;
108+
}
109+
85110
export const StepLogistics: React.FunctionComponent<Props> = React.memo(
86111
({ defaultValue, isEditing = false, onChange, isLegacy = false }) => {
87112
const { form } = useForm({
88113
schema: schemas.logistics,
89114
defaultValue,
90115
options: { stripEmptyFields: false },
116+
serializer: formSerializer,
117+
deserializer: formDeserializer,
91118
});
92119

93120
/**
@@ -226,25 +253,35 @@ export const StepLogistics: React.FunctionComponent<Props> = React.memo(
226253
id="xpack.idxMgmt.templateForm.stepLogistics.metaFieldDescription"
227254
defaultMessage="Use the _meta field to store any metadata you want."
228255
/>
256+
<EuiSpacer size="m" />
257+
<UseField path="__internal__.addMeta" data-test-subj="metaToggle" />
229258
</>
230259
}
231260
>
232-
<UseField
233-
path="_meta"
234-
component={JsonEditorField}
235-
componentProps={{
236-
euiCodeEditorProps: {
237-
height: '280px',
238-
'aria-label': i18n.translate(
239-
'xpack.idxMgmt.templateForm.stepLogistics.metaFieldEditorAriaLabel',
240-
{
241-
defaultMessage: '_meta field data editor',
242-
}
243-
),
244-
'data-test-subj': 'metaField',
245-
},
261+
<FormDataProvider pathsToWatch="__internal__.addMeta">
262+
{({ '__internal__.addMeta': addMeta }) => {
263+
return (
264+
addMeta && (
265+
<UseField
266+
path="_meta"
267+
component={JsonEditorField}
268+
componentProps={{
269+
euiCodeEditorProps: {
270+
height: '280px',
271+
'aria-label': i18n.translate(
272+
'xpack.idxMgmt.templateForm.stepLogistics.metaFieldEditorAriaLabel',
273+
{
274+
defaultMessage: '_meta field data editor',
275+
}
276+
),
277+
'data-test-subj': 'metaField',
278+
},
279+
}}
280+
/>
281+
)
282+
);
246283
}}
247-
/>
284+
</FormDataProvider>
248285
</FormRow>
249286
)}
250287
</Form>

x-pack/plugins/index_management/public/application/components/template_form/template_form_schemas.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,13 @@ export const schemas: Record<string, FormSchema> = {
187187
}
188188
},
189189
},
190+
__internal__: {
191+
addMeta: {
192+
type: FIELD_TYPES.TOGGLE,
193+
label: i18n.translate('xpack.idxMgmt.templateForm.stepLogistics.addMetadataLabel', {
194+
defaultMessage: 'Add metadata',
195+
}),
196+
} as FieldConfig,
197+
},
190198
},
191199
};

x-pack/plugins/index_management/public/shared_imports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export {
2222
useForm,
2323
Form,
2424
getUseField,
25+
UseField,
26+
FormDataProvider,
2527
} from '../../../../src/plugins/es_ui_shared/static/forms/hook_form_lib';
2628

2729
export {
@@ -33,6 +35,7 @@ export {
3335
export {
3436
getFormRow,
3537
Field,
38+
ToggleField,
3639
JsonEditorField,
3740
} from '../../../../src/plugins/es_ui_shared/static/forms/components';
3841

0 commit comments

Comments
 (0)