-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Mappings editor] Support for dynamic templates #54523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alisonelizabeth
merged 6 commits into
elastic:feature/mappings-editor
from
alisonelizabeth:mappings-editor/dynamic-templates
Jan 14, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09b06c6
add dynamic templates tab
alisonelizabeth 3ac9cf9
cleanup
alisonelizabeth d7f42df
Merge branch 'feature/mappings-editor' of github.com:elastic/kibana i…
alisonelizabeth b50fd12
address review feedback
alisonelizabeth b33f956
address review feedback
alisonelizabeth 9dda272
Merge branch 'feature/mappings-editor' into mappings-editor/dynamic-t…
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,5 @@ | |
| export * from './configuration_form'; | ||
|
|
||
| export * from './document_fields'; | ||
|
|
||
| export * from './templates_form'; | ||
7 changes: 7 additions & 0 deletions
7
...index_management/public/app/components/mappings_editor/components/templates_form/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| export { TemplatesForm } from './templates_form'; |
126 changes: 126 additions & 0 deletions
126
...gement/public/app/components/mappings_editor/components/templates_form/templates_form.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import React, { useEffect, useRef } from 'react'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { FormattedMessage } from '@kbn/i18n/react'; | ||
|
|
||
| import { EuiText, EuiLink, EuiSpacer } from '@elastic/eui'; | ||
| import { useForm, Form, SerializerFunc, UseField, JsonEditorField } from '../../shared_imports'; | ||
| import { Types, useDispatch } from '../../mappings_state'; | ||
| import { templatesFormSchema } from './templates_form_schema'; | ||
| import { documentationService } from '../../../../services/documentation'; | ||
|
|
||
| type MappingsTemplates = Types['MappingsTemplates']; | ||
|
|
||
| interface Props { | ||
| defaultValue?: MappingsTemplates; | ||
| } | ||
|
|
||
| const stringifyJson = (json: { [key: string]: any }) => | ||
| Array.isArray(json) ? JSON.stringify(json, null, 2) : '[\n\n]'; | ||
|
|
||
| const formSerializer: SerializerFunc<MappingsTemplates> = formData => { | ||
| const { dynamicTemplates } = formData; | ||
|
|
||
| let parsedTemplates; | ||
| try { | ||
| parsedTemplates = JSON.parse(dynamicTemplates); | ||
|
|
||
| if (!Array.isArray(parsedTemplates)) { | ||
| // User provided an object, but we need an array of objects | ||
| parsedTemplates = [parsedTemplates]; | ||
| } | ||
| } catch { | ||
| parsedTemplates = []; | ||
| } | ||
|
|
||
| return { | ||
| dynamic_templates: parsedTemplates, | ||
| }; | ||
| }; | ||
|
|
||
| const formDeserializer = (formData: { [key: string]: any }) => { | ||
| const { dynamic_templates } = formData; | ||
|
|
||
| return { | ||
| dynamicTemplates: stringifyJson(dynamic_templates), | ||
| }; | ||
| }; | ||
|
|
||
| export const TemplatesForm = React.memo(({ defaultValue }: Props) => { | ||
| const didMountRef = useRef(false); | ||
|
|
||
| const { form } = useForm<MappingsTemplates>({ | ||
| schema: templatesFormSchema, | ||
| serializer: formSerializer, | ||
| deserializer: formDeserializer, | ||
| defaultValue, | ||
| }); | ||
| const dispatch = useDispatch(); | ||
|
|
||
| useEffect(() => { | ||
| const subscription = form.subscribe(updatedTemplates => { | ||
| dispatch({ type: 'templates.update', value: { ...updatedTemplates, form } }); | ||
| }); | ||
| return subscription.unsubscribe; | ||
| }, [form]); | ||
|
|
||
| useEffect(() => { | ||
| if (didMountRef.current) { | ||
| // If the defaultValue has changed (it probably means that we have loaded a new JSON) | ||
| // we need to reset the form to update the fields values. | ||
| form.reset({ resetValues: true }); | ||
| } else { | ||
| // Avoid reseting the form on component mount. | ||
| didMountRef.current = true; | ||
| } | ||
| }, [defaultValue]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| // On unmount => save in the state a snapshot of the current form data. | ||
| dispatch({ type: 'templates.save' }); | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <> | ||
| <EuiText size="s" color="subdued"> | ||
| <FormattedMessage | ||
| id="xpack.idxMgmt.mappingsEditor.dynamicTemplatesDescription" | ||
| defaultMessage="Use dynamic templates to define custom mappings that can be applied to dynamically added fields. {docsLink}" | ||
| values={{ | ||
| docsLink: ( | ||
| <EuiLink href={documentationService.getDynamicTemplatesLink()} target="_blank"> | ||
| {i18n.translate('xpack.idxMgmt.mappingsEditor.dynamicTemplatesDocumentationLink', { | ||
| defaultMessage: 'Learn more.', | ||
| })} | ||
| </EuiLink> | ||
| ), | ||
| }} | ||
| /> | ||
| </EuiText> | ||
| <EuiSpacer size="m" /> | ||
| <Form form={form} isInvalid={form.isSubmitted && !form.isValid} error={form.getErrors()}> | ||
| <UseField | ||
| path="dynamicTemplates" | ||
| component={JsonEditorField} | ||
| componentProps={{ | ||
| euiCodeEditorProps: { | ||
| height: '600px', | ||
| 'aria-label': i18n.translate( | ||
| 'xpack.idxMgmt.mappingsEditor.dynamicTemplatesEditorAriaLabel', | ||
| { | ||
| defaultMessage: 'Dynamic templates editor', | ||
| } | ||
| ), | ||
| }, | ||
| }} | ||
| /> | ||
| </Form> | ||
| </> | ||
| ); | ||
| }); | ||
29 changes: 29 additions & 0 deletions
29
.../public/app/components/mappings_editor/components/templates_form/templates_form_schema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { i18n } from '@kbn/i18n'; | ||
|
|
||
| import { FormSchema, fieldValidators } from '../../shared_imports'; | ||
| import { MappingsTemplates } from '../../reducer'; | ||
|
|
||
| const { isJsonField } = fieldValidators; | ||
|
|
||
| export const templatesFormSchema: FormSchema<MappingsTemplates> = { | ||
| dynamicTemplates: { | ||
| label: i18n.translate('xpack.idxMgmt.mappingsEditor.templates.dynamicTemplatesEditorLabel', { | ||
| defaultMessage: 'Dynamic templates data', | ||
| }), | ||
| validations: [ | ||
| { | ||
| validator: isJsonField( | ||
| i18n.translate('xpack.idxMgmt.mappingsEditor.templates.dynamicTemplatesEditorJsonError', { | ||
| defaultMessage: 'The dynamic templates JSON is not valid.', | ||
| }) | ||
| ), | ||
| }, | ||
| ], | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.