-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7bbe40
commit 09b06c6
Showing
10 changed files
with
296 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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'; |
121 changes: 121 additions & 0 deletions
121
...gement/public/app/components/mappings_editor/components/templates_form/templates_form.tsx
This file contains 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,121 @@ | ||
/* | ||
* 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); | ||
} 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: '400px', | ||
'aria-label': i18n.translate( | ||
'xpack.idxMgmt.mappingsEditor.dynamicTemplatesEditorAriaLabel', | ||
{ | ||
defaultMessage: 'Dynamic templates editor', | ||
} | ||
), | ||
}, | ||
}} | ||
/> | ||
</Form> | ||
</> | ||
); | ||
}); |
51 changes: 51 additions & 0 deletions
51
...public/app/components/mappings_editor/components/templates_form/templates_form_schema.tsx
This file contains 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,51 @@ | ||
/* | ||
* 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 from 'react'; | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { EuiCode } from '@elastic/eui'; | ||
|
||
import { FormSchema, fieldValidators } from '../../shared_imports'; | ||
import { MappingsTemplate } from '../../reducer'; | ||
|
||
const { isJsonField } = fieldValidators; | ||
|
||
export const templatesFormSchema: FormSchema<MappingsTemplate> = { | ||
dynamicTemplates: { | ||
label: i18n.translate('xpack.idxMgmt.mappingsEditor.templates.dynamicTemplatesEditorLabel', { | ||
defaultMessage: 'Dynamic templates data', | ||
}), | ||
helpText: ( | ||
<FormattedMessage | ||
id="xpack.idxMgmt.mappingsEditor.templates.dynamicTemplatesEditorHelpText" | ||
defaultMessage="Use JSON format: {code}" | ||
values={{ | ||
code: ( | ||
<EuiCode> | ||
{JSON.stringify([ | ||
{ | ||
my_template_name: { | ||
mapping: {}, | ||
}, | ||
}, | ||
])} | ||
</EuiCode> | ||
), | ||
}} | ||
/> | ||
), | ||
validations: [ | ||
{ | ||
validator: isJsonField( | ||
i18n.translate('xpack.idxMgmt.mappingsEditor.templates.dynamicTemplatesEditorJsonError', { | ||
defaultMessage: 'The dynamic templates JSON is not valid.', | ||
}) | ||
), | ||
}, | ||
], | ||
}, | ||
}; |
This file contains 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 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 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.