Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Maintain order of operations in dimension panel #78864

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ export interface DimensionEditorProps extends IndexPatternDimensionEditorProps {
currentIndexPattern: IndexPattern;
}

function asOperationOptions(operationTypes: OperationType[], compatibleWithCurrentField: boolean) {
return [...operationTypes]
.sort((opType1, opType2) => {
return operationPanels[opType1].displayName.localeCompare(
operationPanels[opType2].displayName
);
})
.map((operationType) => ({
operationType,
compatibleWithCurrentField,
}));
}

const LabelInput = ({ value, onChange }: { value: string; onChange: (value: string) => void }) => {
const [inputValue, setInputValue] = useState(value);

Expand Down Expand Up @@ -98,7 +85,7 @@ export function DimensionEditor(props: DimensionEditorProps) {
currentIndexPattern,
hideGrouping,
} = props;
const { operationByField, fieldByOperation } = operationSupportMatrix;
const { fieldByOperation, operationWithoutField } = operationSupportMatrix;
const [
incompatibleSelectedOperationType,
setInvalidOperationType,
Expand All @@ -117,30 +104,35 @@ export function DimensionEditor(props: DimensionEditorProps) {
return fields;
}, [currentIndexPattern]);

function getOperationTypes() {
const possibleOperationTypes = Object.keys(fieldByOperation) as OperationType[];
const validOperationTypes: OperationType[] = [];

if (!selectedColumn) {
validOperationTypes.push(...(Object.keys(fieldByOperation) as OperationType[]));
} else if (hasField(selectedColumn) && operationByField[selectedColumn.sourceField]) {
validOperationTypes.push(...operationByField[selectedColumn.sourceField]!);
}
const possibleOperations = useMemo(() => {
return Object.values(operationDefinitionMap)
.sort((op1, op2) => {
return op1.displayName.localeCompare(op2.displayName);
})
.map((def) => def.type)
.filter(
(type) => fieldByOperation[type]?.length || operationWithoutField.indexOf(type) !== -1
);
}, [fieldByOperation, operationWithoutField]);

return _.uniqBy(
[
...asOperationOptions(validOperationTypes, true),
...asOperationOptions(possibleOperationTypes, false),
...asOperationOptions(
operationSupportMatrix.operationWithoutField,
!selectedColumn || !hasField(selectedColumn)
),
],
'operationType'
);
}
// Operations are compatible if they match inputs. They are always compatible in
// the empty state. Field-based operations are not compatible with field-less operations.
const operationsWithCompatibility = [...possibleOperations].map((operationType) => {
const definition = operationDefinitionMap[operationType];

const sideNavItems: EuiListGroupItemProps[] = getOperationTypes().map(
return {
operationType,
compatibleWithCurrentField:
!selectedColumn ||
(selectedColumn &&
hasField(selectedColumn) &&
definition.input === 'field' &&
fieldByOperation[operationType]?.indexOf(selectedColumn.sourceField) !== -1) ||
(selectedColumn && !hasField(selectedColumn) && definition.input !== 'field'),
};
});

const sideNavItems: EuiListGroupItemProps[] = operationsWithCompatibility.map(
({ operationType, compatibleWithCurrentField }) => {
const isActive = Boolean(
incompatibleSelectedOperationType === operationType ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,12 @@ describe('IndexPatternDimensionEditorPanel', () => {
const items: EuiListGroupItemProps[] = wrapper.find(EuiListGroup).prop('listItems') || [];

expect(items.map(({ label }: { label: React.ReactNode }) => label)).toEqual([
'Unique count',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests already covered the behavior of the buttons, so the only change here is to the ordering.

'Average',
'Count',
'Maximum',
'Minimum',
'Sum',
'Unique count',
]);
});

Expand Down