Skip to content

Commit 06e48d4

Browse files
In scripted fields, unable to switch the Type - getting a console error which says - Class constructor DecoratedFieldFormat cannot be invoked without 'new' (#59285)
Closes: #58763 Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 2643f99 commit 06e48d4

File tree

4 files changed

+33
-96
lines changed

4 files changed

+33
-96
lines changed

src/legacy/ui/public/field_editor/field_editor.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import { ScriptingHelpFlyout } from './components/scripting_help';
6666
import { FieldFormatEditor } from './components/field_format_editor';
6767

6868
import { FIELD_TYPES_BY_LANG, DEFAULT_FIELD_TYPES } from './constants';
69-
import { copyField, getDefaultFormat, executeScript, isScriptValid } from './lib';
69+
import { copyField, executeScript, isScriptValid } from './lib';
7070

7171
import { i18n } from '@kbn/i18n';
7272
import { FormattedMessage } from '@kbn/i18n/react';
@@ -76,6 +76,25 @@ import 'brace/mode/groovy';
7676

7777
const getFieldFormats = () => npStart.plugins.data.fieldFormats;
7878

79+
const getFieldTypeFormatsList = (field, defaultFieldFormat) => {
80+
const fieldFormats = getFieldFormats();
81+
const formatsByType = fieldFormats.getByFieldType(field.type).map(({ id, title }) => ({
82+
id,
83+
title,
84+
}));
85+
86+
return [
87+
{
88+
id: '',
89+
defaultFieldFormat,
90+
title: i18n.translate('common.ui.fieldEditor.defaultFormatDropDown', {
91+
defaultMessage: '- Default -',
92+
}),
93+
},
94+
...formatsByType,
95+
];
96+
};
97+
7998
export class FieldEditor extends PureComponent {
8099
static propTypes = {
81100
indexPattern: PropTypes.object.isRequired,
@@ -137,11 +156,7 @@ export class FieldEditor extends PureComponent {
137156
field.type = fieldTypes.includes(field.type) ? field.type : fieldTypes[0];
138157

139158
const fieldFormats = getFieldFormats();
140-
141-
const fieldTypeFormats = [
142-
getDefaultFormat(fieldFormats.getDefaultType(field.type, field.esTypes)),
143-
...fieldFormats.getByFieldType(field.type),
144-
];
159+
const DefaultFieldFormat = fieldFormats.getDefaultType(field.type, field.esTypes);
145160

146161
this.setState({
147162
isReady: true,
@@ -150,14 +165,14 @@ export class FieldEditor extends PureComponent {
150165
errors: [],
151166
scriptingLangs,
152167
fieldTypes,
153-
fieldTypeFormats,
168+
fieldTypeFormats: getFieldTypeFormatsList(field, DefaultFieldFormat),
154169
fieldFormatId: get(indexPattern, ['fieldFormatMap', field.name, 'type', 'id']),
155170
fieldFormatParams: field.format.params(),
156171
});
157172
}
158173

159174
onFieldChange = (fieldName, value) => {
160-
const field = this.state.field;
175+
const { field } = this.state;
161176
field[fieldName] = value;
162177
this.forceUpdate();
163178
};
@@ -169,18 +184,11 @@ export class FieldEditor extends PureComponent {
169184
const DefaultFieldFormat = fieldFormats.getDefaultType(type);
170185

171186
field.type = type;
172-
173-
const fieldTypeFormats = [
174-
getDefaultFormat(DefaultFieldFormat),
175-
...getFieldFormats().getByFieldType(field.type),
176-
];
177-
178-
const FieldFormat = fieldTypeFormats[0];
179-
field.format = new FieldFormat(null, getConfig);
187+
field.format = new DefaultFieldFormat(null, getConfig);
180188

181189
this.setState({
182-
fieldTypeFormats,
183-
fieldFormatId: FieldFormat.id,
190+
fieldTypeFormats: getFieldTypeFormatsList(field, DefaultFieldFormat),
191+
fieldFormatId: DefaultFieldFormat.id,
184192
fieldFormatParams: field.format.params(),
185193
});
186194
};
@@ -197,12 +205,13 @@ export class FieldEditor extends PureComponent {
197205
};
198206

199207
onFormatChange = (formatId, params) => {
200-
const { getConfig } = this.props.helpers;
208+
const fieldFormats = getFieldFormats();
201209
const { field, fieldTypeFormats } = this.state;
202-
const FieldFormat =
203-
fieldTypeFormats.find(format => format.id === formatId) || fieldTypeFormats[0];
210+
const FieldFormat = fieldFormats.getType(
211+
formatId || fieldTypeFormats[0]?.defaultFieldFormat.id
212+
);
204213

205-
field.format = new FieldFormat(params, getConfig);
214+
field.format = new FieldFormat(params, this.props.helpers.getConfig);
206215

207216
this.setState({
208217
fieldFormatId: FieldFormat.id,
@@ -416,7 +425,8 @@ export class FieldEditor extends PureComponent {
416425
renderFormat() {
417426
const { field, fieldTypeFormats, fieldFormatId, fieldFormatParams } = this.state;
418427
const { fieldFormatEditors } = this.props.helpers;
419-
const defaultFormat = fieldTypeFormats[0] && fieldTypeFormats[0].resolvedTitle;
428+
const defaultFormat = fieldTypeFormats[0]?.defaultFieldFormat.title;
429+
420430
const label = defaultFormat ? (
421431
<FormattedMessage
422432
id="common.ui.fieldEditor.defaultFormatHeader"

src/legacy/ui/public/field_editor/lib/__tests__/get_default_format.test.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/legacy/ui/public/field_editor/lib/get_default_format.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/legacy/ui/public/field_editor/lib/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818
*/
1919

2020
export { copyField } from './copy_field';
21-
export { getDefaultFormat } from './get_default_format';
2221
export { executeScript, isScriptValid } from './validate_script';

0 commit comments

Comments
 (0)