keep custom labels (#68498)

This commit is contained in:
Joe Reuter 2020-06-10 18:29:22 +02:00 committed by GitHub
parent 497ecf8164
commit 8d1a319472
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 11 deletions

View file

@ -467,7 +467,7 @@ describe('IndexPatternDimensionEditorPanel', () => {
expect(setState).not.toHaveBeenCalled();
});
it('should update label on label input changes', () => {
it('should update label and custom label flag on label input changes', () => {
wrapper = mount(<IndexPatternDimensionEditorComponent {...defaultProps} />);
act(() => {
@ -485,6 +485,7 @@ describe('IndexPatternDimensionEditorPanel', () => {
...state.layers.first.columns,
col1: expect.objectContaining({
label: 'New Label',
customLabel: true,
// Other parts of this don't matter for this test
}),
},
@ -493,6 +494,104 @@ describe('IndexPatternDimensionEditorPanel', () => {
});
});
it('should not keep the label as long as it is the default label', () => {
wrapper = mount(
<IndexPatternDimensionEditorComponent
{...defaultProps}
state={{
...state,
layers: {
first: {
...state.layers.first,
columns: {
...state.layers.first.columns,
col1: {
label: 'Max of bytes',
dataType: 'number',
isBucketed: false,
// Private
operationType: 'max',
sourceField: 'bytes',
params: { format: { id: 'bytes' } },
},
},
},
},
}}
/>
);
act(() => {
wrapper.find('button[data-test-subj="lns-indexPatternDimension-min"]').simulate('click');
});
expect(setState).toHaveBeenCalledWith({
...state,
layers: {
first: {
...state.layers.first,
columns: {
...state.layers.first.columns,
col1: expect.objectContaining({
label: 'Minimum of bytes',
}),
},
},
},
});
});
it('should keep the label on operation change if it is custom', () => {
wrapper = mount(
<IndexPatternDimensionEditorComponent
{...defaultProps}
state={{
...state,
layers: {
first: {
...state.layers.first,
columns: {
...state.layers.first.columns,
col1: {
label: 'Custom label',
customLabel: true,
dataType: 'number',
isBucketed: false,
// Private
operationType: 'max',
sourceField: 'bytes',
params: { format: { id: 'bytes' } },
},
},
},
},
}}
/>
);
act(() => {
wrapper.find('button[data-test-subj="lns-indexPatternDimension-min"]').simulate('click');
});
expect(setState).toHaveBeenCalledWith({
...state,
layers: {
first: {
...state.layers.first,
columns: {
...state.layers.first.columns,
col1: expect.objectContaining({
label: 'Custom label',
customLabel: true,
}),
},
},
},
});
});
describe('transient invalid state', () => {
it('should not set the state if selecting an operation incompatible with the current field', () => {
wrapper = mount(<IndexPatternDimensionEditorComponent {...defaultProps} />);

View file

@ -314,17 +314,23 @@ export function PopoverEditor(props: PopoverEditorProps) {
data-test-subj="indexPattern-label-edit"
value={selectedColumn.label}
onChange={(e) => {
setState(
changeColumn({
state,
layerId,
columnId,
newColumn: {
...selectedColumn,
label: e.target.value,
setState({
...state,
layers: {
...state.layers,
[layerId]: {
...state.layers[layerId],
columns: {
...state.layers[layerId].columns,
[columnId]: {
...selectedColumn,
label: e.target.value,
customLabel: true,
},
},
},
})
);
},
});
}}
/>
</EuiFormRow>

View file

@ -16,6 +16,7 @@ export interface BaseIndexPatternColumn extends Operation {
operationType: string;
sourceField: string;
suggestedPriority?: DimensionPriority;
customLabel?: boolean;
}
// Formatting can optionally be added to any column

View file

@ -331,6 +331,61 @@ describe('state_helpers', () => {
);
});
it('should carry over label if customLabel flag is set', () => {
const state: IndexPatternPrivateState = {
indexPatternRefs: [],
existingFields: {},
indexPatterns: {},
currentIndexPatternId: '1',
showEmptyFields: false,
layers: {
first: {
indexPatternId: '1',
columnOrder: ['col1'],
columns: {
col1: {
label: 'My custom label',
customLabel: true,
dataType: 'date',
isBucketed: true,
// Private
operationType: 'date_histogram',
sourceField: 'timestamp',
params: {
interval: 'h',
},
},
},
},
},
};
expect(
changeColumn({
state,
layerId: 'first',
columnId: 'col2',
newColumn: {
label: 'Date histogram of order_date',
dataType: 'date',
isBucketed: true,
// Private
operationType: 'date_histogram',
sourceField: 'order_date',
params: {
interval: 'w',
},
},
}).layers.first.columns.col1
).toEqual(
expect.objectContaining({
label: 'My custom label',
customLabel: true,
})
);
});
it('should execute adjustments for other columns', () => {
const termsColumn: TermsIndexPatternColumn = {
label: 'Top values of source',

View file

@ -87,6 +87,11 @@ export function changeColumn<C extends IndexPatternColumn>({
? { ...newColumn, params: oldColumn.params }
: newColumn;
if (oldColumn && oldColumn.customLabel) {
updatedColumn.customLabel = true;
updatedColumn.label = oldColumn.label;
}
const newColumns = adjustColumnReferencesForChangedColumn(
{
...state.layers[layerId].columns,