Skip to content

Commit

Permalink
[Lens] Retain column config (elastic#90048)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Feb 8, 2021
1 parent d152723 commit 4a1946b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ describe('Datatable Visualization', () => {
expect(suggestions.length).toBeGreaterThan(0);
});

it('should retain width and hidden config from existing state', () => {
const suggestions = datatableVisualization.getSuggestions({
state: {
layerId: 'first',
columns: [
{ columnId: 'col1', width: 123 },
{ columnId: 'col2', hidden: true },
],
sorting: {
columnId: 'col1',
direction: 'asc',
},
},
table: {
isMultiRow: true,
layerId: 'first',
changeType: 'initial',
columns: [numCol('col1'), strCol('col2'), strCol('col3')],
},
keptLayerIds: [],
});

expect(suggestions.length).toBeGreaterThan(0);
expect(suggestions[0].state.columns).toEqual([
{ columnId: 'col1', width: 123 },
{ columnId: 'col2', hidden: true },
{ columnId: 'col3' },
]);
expect(suggestions[0].state.sorting).toEqual({
columnId: 'col1',
direction: 'asc',
});
});

it('should not make suggestions when the table is unchanged', () => {
const suggestions = datatableVisualization.getSuggestions({
state: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export const datatableVisualization: Visualization<DatatableVisualizationState>
) {
return [];
}
const oldColumnSettings: Record<string, ColumnState> = {};
if (state) {
state.columns.forEach((column) => {
oldColumnSettings[column.columnId] = column;
});
}
const title =
table.changeType === 'unchanged'
? i18n.translate('xpack.lens.datatable.suggestionLabel', {
Expand Down Expand Up @@ -126,8 +132,12 @@ export const datatableVisualization: Visualization<DatatableVisualizationState>
// table with >= 10 columns will have a score of 0.4, fewer columns reduce score
score: (Math.min(table.columns.length, 10) / 10) * 0.4,
state: {
...(state || {}),
layerId: table.layerId,
columns: table.columns.map((col) => ({ columnId: col.columnId })),
columns: table.columns.map((col) => ({
...(oldColumnSettings[col.columnId] || {}),
columnId: col.columnId,
})),
},
previewIcon: LensIconChartDatatable,
// tables are hidden from suggestion bar, but used for drag & drop and chart switching
Expand Down

0 comments on commit 4a1946b

Please sign in to comment.