Table: Support custom date formats via custom unit (#25195)

* Table: Custom date formating via custom unit

* FieldConfig: Changed so that shouldApply is not called for overrides and added unit test

* Updated
This commit is contained in:
Torkel Ödegaard
2020-06-09 17:43:29 +02:00
committed by GitHub
parent 1ea8346644
commit 6fe9982ff0
2 changed files with 37 additions and 7 deletions
@@ -22,13 +22,14 @@ import { FieldMatcherID } from '../transformations';
import { FieldConfigOptionsRegistry } from './FieldConfigOptionsRegistry';
import { getFieldDisplayName } from './fieldState';
import { locationUtil } from '../utils';
const property1 = {
const property1: any = {
id: 'custom.property1', // Match field properties
path: 'property1', // Match field properties
isCustom: true,
process: (value: any) => value,
shouldApply: () => true,
} as any;
};
const property2 = {
id: 'custom.property2', // Match field properties
@@ -36,18 +37,26 @@ const property2 = {
isCustom: true,
process: (value: any) => value,
shouldApply: () => true,
} as any;
};
const property3 = {
const property3: any = {
id: 'custom.property3.nested', // Match field properties
path: 'property3.nested', // Match field properties
isCustom: true,
process: (value: any) => value,
shouldApply: () => true,
} as any;
};
const shouldApplyFalse: any = {
id: 'custom.shouldApplyFalse', // Match field properties
path: 'shouldApplyFalse', // Match field properties
isCustom: true,
process: (value: any) => value,
shouldApply: () => false,
};
export const customFieldRegistry: FieldConfigOptionsRegistry = new Registry<FieldConfigPropertyItem>(() => {
return [property1, property2, property3, ...mockStandardProperties()];
return [property1, property2, property3, shouldApplyFalse, ...mockStandardProperties()];
});
describe('Global MinMax', () => {
@@ -353,6 +362,27 @@ describe('setDynamicConfigValue', () => {
expect(config.custom.property1).toEqual('applied');
});
it('applies overrides even when shouldApply returns false', () => {
const config: FieldConfig = {
custom: {},
};
setDynamicConfigValue(
config,
{
id: 'custom.shouldApplyFalse',
value: 'applied',
},
{
fieldConfigRegistry: customFieldRegistry,
data: [] as any,
field: { type: FieldType.number } as any,
dataFrameIndex: 0,
}
);
expect(config.custom.shouldApplyFalse).toEqual('applied');
});
it('applies nested custom dynamic config values', () => {
const config = {
custom: {
@@ -228,7 +228,7 @@ export interface FieldOverrideEnv extends FieldOverrideContext {
export function setDynamicConfigValue(config: FieldConfig, value: DynamicConfigValue, context: FieldOverrideEnv) {
const reg = context.fieldConfigRegistry;
const item = reg.getIfExists(value.id);
if (!item || !item.shouldApply(context.field!)) {
if (!item) {
return;
}