mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[Lens] Improve field drag defaults (#140050)
* improve field drag defaults * update snapshot * Update x-pack/plugins/lens/public/indexpattern_datasource/dimension_panel/droppable/on_drop_handler.ts Co-authored-by: Marco Liberati <dej611@users.noreply.github.com> * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' Co-authored-by: Marco Liberati <dej611@users.noreply.github.com> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
06c68f58a2
commit
d6b871d661
6 changed files with 80 additions and 4 deletions
|
@ -44,12 +44,13 @@ export function getNewOperation(
|
|||
field: IndexPatternField | undefined | false,
|
||||
filterOperations: (meta: OperationMetadata) => boolean,
|
||||
targetColumn?: GenericIndexPatternColumn,
|
||||
prioritizedOperation?: GenericIndexPatternColumn['operationType']
|
||||
prioritizedOperation?: GenericIndexPatternColumn['operationType'],
|
||||
alreadyUsedOperations?: Set<string>
|
||||
) {
|
||||
if (!field) {
|
||||
return;
|
||||
}
|
||||
const newOperations = getOperationTypesForField(field, filterOperations);
|
||||
const newOperations = getOperationTypesForField(field, filterOperations, alreadyUsedOperations);
|
||||
if (!newOperations.length) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -146,6 +146,56 @@ describe('IndexPatternDimensionEditorPanel: onDrop', () => {
|
|||
},
|
||||
});
|
||||
});
|
||||
it('does not re-use an operation if there is another operation available', () => {
|
||||
const localState = {
|
||||
...state,
|
||||
layers: {
|
||||
...state.layers,
|
||||
first: {
|
||||
...state.layers.first,
|
||||
columns: {
|
||||
...state.layers.first.columns,
|
||||
col1: {
|
||||
...state.layers.first.columns.col1,
|
||||
sourceField: mockedDraggedField.field.name,
|
||||
operationType: 'median',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
onDrop({
|
||||
...defaultProps,
|
||||
state: localState,
|
||||
source: mockedDraggedField,
|
||||
dropType: 'field_replace',
|
||||
target: {
|
||||
...defaultProps.target,
|
||||
filterOperations: (op: OperationMetadata) => !op.isBucketed,
|
||||
columnId: 'col2',
|
||||
},
|
||||
});
|
||||
|
||||
expect(setState).toBeCalledTimes(1);
|
||||
expect(setState).toHaveBeenCalledWith({
|
||||
...localState,
|
||||
layers: {
|
||||
...localState.layers,
|
||||
first: {
|
||||
...localState.layers.first,
|
||||
columnOrder: ['col1', 'col2'],
|
||||
columns: {
|
||||
...localState.layers.first.columns,
|
||||
col2: expect.objectContaining({
|
||||
operationType: 'average',
|
||||
dataType: 'number',
|
||||
sourceField: mockedDraggedField.field.name,
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
it('keeps the operation when dropping a different compatible field', () => {
|
||||
onDrop({
|
||||
...defaultProps,
|
||||
|
|
|
@ -124,9 +124,22 @@ function onFieldDrop(props: DropHandlerProps<DraggedField>, shouldAddField?: boo
|
|||
const layer = state.layers[target.layerId];
|
||||
const indexPattern = indexPatterns[layer.indexPatternId];
|
||||
const targetColumn = layer.columns[target.columnId];
|
||||
// discourage already used operations for a field
|
||||
const alreadyUsedOperations = new Set(
|
||||
Object.values(layer.columns)
|
||||
.filter((column) => 'sourceField' in column && column.sourceField === source.field.name)
|
||||
.map((column) => column.operationType)
|
||||
);
|
||||
|
||||
const newOperation = shouldAddField
|
||||
? targetColumn.operationType
|
||||
: getNewOperation(source.field, target.filterOperations, targetColumn, prioritizedOperation);
|
||||
: getNewOperation(
|
||||
source.field,
|
||||
target.filterOperations,
|
||||
targetColumn,
|
||||
prioritizedOperation,
|
||||
alreadyUsedOperations
|
||||
);
|
||||
|
||||
if (
|
||||
!isDraggedField(source) ||
|
||||
|
|
|
@ -111,7 +111,8 @@ export function hasOperationSupportForMultipleFields(
|
|||
*/
|
||||
export function getOperationTypesForField(
|
||||
field: IndexPatternField,
|
||||
filterOperations?: (operation: OperationMetadata) => boolean
|
||||
filterOperations?: (operation: OperationMetadata) => boolean,
|
||||
alreadyUsedOperations?: Set<string>
|
||||
): OperationType[] {
|
||||
return operationDefinitions
|
||||
.filter((operationDefinition) => {
|
||||
|
@ -124,6 +125,15 @@ export function getOperationTypesForField(
|
|||
: possibleOperation;
|
||||
})
|
||||
.sort(getSortScoreByPriorityForField(field))
|
||||
.sort((a, b) => {
|
||||
if (!alreadyUsedOperations) return 0;
|
||||
// if some operations are used already, order them so the unused operations come first
|
||||
const aAlreadyUsed = alreadyUsedOperations.has(a.type);
|
||||
const bAlreadyUsed = alreadyUsedOperations.has(b.type);
|
||||
if (aAlreadyUsed === bAlreadyUsed) return 0;
|
||||
if (aAlreadyUsed) return 1;
|
||||
return -1;
|
||||
})
|
||||
.map(({ type }) => type);
|
||||
}
|
||||
|
||||
|
|
|
@ -69,6 +69,7 @@ Object {
|
|||
"paramEditorCustomProps": Object {
|
||||
"headingLabel": "Value",
|
||||
},
|
||||
"prioritizedOperation": "max",
|
||||
"required": false,
|
||||
"supportStaticValue": true,
|
||||
"supportsMoreColumns": false,
|
||||
|
|
|
@ -349,6 +349,7 @@ export const getMetricVisualization = ({
|
|||
enableFormatSelector: false,
|
||||
formatSelectorOptions: formatterOptions,
|
||||
supportStaticValue: true,
|
||||
prioritizedOperation: 'max',
|
||||
required: false,
|
||||
groupTooltip: i18n.translate('xpack.lens.metric.maxTooltip', {
|
||||
defaultMessage:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue