|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react'; |
| 8 | +import { i18n } from '@kbn/i18n'; |
| 9 | +import { |
| 10 | + EuiFormRow, |
| 11 | + EuiComboBox, |
| 12 | + EuiComboBoxOptionOption, |
| 13 | + EuiDescribedFormGroup, |
| 14 | + EuiSpacer, |
| 15 | +} from '@elastic/eui'; |
| 16 | + |
| 17 | +import { UseField } from '../../../shared_imports'; |
| 18 | +import { DataType } from '../../../types'; |
| 19 | +import { getFieldConfig } from '../../../lib'; |
| 20 | +import { RUNTIME_FIELD_OPTIONS, TYPE_DEFINITION } from '../../../constants'; |
| 21 | +import { EditFieldFormRow, FieldDescriptionSection } from '../fields/edit_field'; |
| 22 | + |
| 23 | +interface Props { |
| 24 | + stack?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export const RuntimeTypeParameter = ({ stack }: Props) => { |
| 28 | + return ( |
| 29 | + <UseField path="runtime_type" config={getFieldConfig('runtime_type')}> |
| 30 | + {(runtimeTypeField) => { |
| 31 | + const { label, value, setValue } = runtimeTypeField; |
| 32 | + const typeDefinition = |
| 33 | + TYPE_DEFINITION[(value as EuiComboBoxOptionOption[])[0]!.value as DataType]; |
| 34 | + |
| 35 | + const field = ( |
| 36 | + <> |
| 37 | + <EuiFormRow label={label} fullWidth> |
| 38 | + <EuiComboBox |
| 39 | + placeholder={i18n.translate( |
| 40 | + 'xpack.idxMgmt.mappingsEditor.runtimeType.placeholderLabel', |
| 41 | + { |
| 42 | + defaultMessage: 'Select a type', |
| 43 | + } |
| 44 | + )} |
| 45 | + singleSelection={{ asPlainText: true }} |
| 46 | + options={RUNTIME_FIELD_OPTIONS} |
| 47 | + selectedOptions={value as EuiComboBoxOptionOption[]} |
| 48 | + onChange={setValue} |
| 49 | + isClearable={false} |
| 50 | + fullWidth |
| 51 | + /> |
| 52 | + </EuiFormRow> |
| 53 | + |
| 54 | + <EuiSpacer size="m" /> |
| 55 | + |
| 56 | + {/* Field description */} |
| 57 | + {typeDefinition && ( |
| 58 | + <FieldDescriptionSection isMultiField={false}> |
| 59 | + {typeDefinition.description?.() as JSX.Element} |
| 60 | + </FieldDescriptionSection> |
| 61 | + )} |
| 62 | + </> |
| 63 | + ); |
| 64 | + |
| 65 | + const fieldTitle = i18n.translate('xpack.idxMgmt.mappingsEditor.runtimeType.title', { |
| 66 | + defaultMessage: 'Emitted type', |
| 67 | + }); |
| 68 | + |
| 69 | + const fieldDescription = i18n.translate( |
| 70 | + 'xpack.idxMgmt.mappingsEditor.runtimeType.description', |
| 71 | + { |
| 72 | + defaultMessage: 'Select the type of value emitted by the runtime field.', |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + if (stack) { |
| 77 | + return ( |
| 78 | + <EditFieldFormRow title={fieldTitle} description={fieldDescription} withToggle={false}> |
| 79 | + {field} |
| 80 | + </EditFieldFormRow> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <EuiDescribedFormGroup |
| 86 | + title={<h3>{fieldTitle}</h3>} |
| 87 | + description={fieldDescription} |
| 88 | + fullWidth={true} |
| 89 | + > |
| 90 | + {field} |
| 91 | + </EuiDescribedFormGroup> |
| 92 | + ); |
| 93 | + }} |
| 94 | + </UseField> |
| 95 | + ); |
| 96 | +}; |
0 commit comments