Skip to content

Commit

Permalink
simplify field format editor rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Mar 5, 2021
1 parent 02be8c1 commit db5fa24
Showing 1 changed file with 17 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
KBN_FIELD_TYPES,
ES_FIELD_TYPES,
DataPublicPluginStart,
FieldFormat,
} from 'src/plugins/data/public';
import { CoreStart } from 'src/core/public';
import { castEsToKbnFieldTypeName } from '../../../../data/public';
Expand Down Expand Up @@ -45,7 +44,6 @@ export interface FormatSelectEditorState {
fieldTypeFormats: FieldTypeFormat[];
fieldFormatId?: string;
fieldFormatParams?: { [key: string]: unknown };
format: FieldFormat;
kbnType: KBN_FIELD_TYPES;
}

Expand Down Expand Up @@ -81,67 +79,48 @@ export class FormatSelectEditor extends PureComponent<
> {
constructor(props: FormatSelectEditorProps) {
super(props);
const { fieldFormats, esTypes, value } = props;
const { fieldFormats, esTypes } = props;
const kbnType = castEsToKbnFieldTypeName(esTypes[0] || 'keyword');

// get current formatter for field, provides default if none exists
const format = value?.id
? fieldFormats.getInstance(value?.id, value?.params)
: fieldFormats.getDefaultInstance(kbnType, esTypes);

this.state = {
fieldTypeFormats: getFieldTypeFormatsList(
kbnType,
fieldFormats.getDefaultType(kbnType, esTypes) as FieldFormatInstanceType,
fieldFormats
),
format,
kbnType,
};
}
onFormatChange = (formatId: string, params?: any) => {
const { fieldTypeFormats } = this.state;
const { fieldFormats, uiSettings } = this.props;

const FieldFormatClass = fieldFormats.getType(
formatId || (fieldTypeFormats[0] as InitialFieldTypeFormat).defaultFieldFormat.id
) as FieldFormatInstanceType;

const newFormat = new FieldFormatClass(params, (key: string) => uiSettings.get(key));

this.setState(
{
fieldFormatId: formatId,
fieldFormatParams: params,
format: newFormat,
},
() => {
this.props.onChange(
formatId
? {
id: formatId,
params: params || {},
}
: undefined
);
}
onFormatChange = (formatId: string, params?: any) =>
this.props.onChange(
formatId
? {
id: formatId,
params: params || {},
}
: undefined
);
};

onFormatParamsChange = (newParams: { fieldType: string; [key: string]: any }) => {
const { fieldFormatId } = this.state;
this.onFormatChange(fieldFormatId as string, newParams);
};

render() {
const { fieldFormatEditors, onError, value } = this.props;
const { fieldFormatEditors, onError, value, fieldFormats, esTypes } = this.props;
const fieldFormatId = value?.id;
const fieldFormatParams = value?.params;
const { kbnType } = this.state;

const { fieldTypeFormats, format } = this.state;
const { fieldTypeFormats } = this.state;

const defaultFormat = (fieldTypeFormats[0] as InitialFieldTypeFormat).defaultFieldFormat.title;

// get current formatter for field, provides default if none exists
const format = value?.id
? fieldFormats.getInstance(value?.id, value?.params)
: fieldFormats.getDefaultInstance(kbnType, esTypes);

const label = defaultFormat ? (
<FormattedMessage
id="indexPatternFieldEditor.defaultFormatHeader"
Expand Down

0 comments on commit db5fa24

Please sign in to comment.