Skip to content

Commit

Permalink
fix(dashboard): Allow selecting text in cells in Table and PivotTable…
Browse files Browse the repository at this point in the history
… without triggering cross filters (#23283)
  • Loading branch information
kgabryje authored Mar 6, 2023
1 parent 1b139d0 commit d16512b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export const getSelectedText = () => window.getSelection()?.toString();
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export { default as promiseTimeout } from './promiseTimeout';
export { default as logging } from './logging';
export { default as removeDuplicates } from './removeDuplicates';
export { lruCache } from './lruCache';
export { getSelectedText } from './getSelectedText';
export * from './featureFlags';
export * from './random';
export * from './typedMemo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { getSelectedText } from '@superset-ui/core';

test('Returns null if Selection object is null', () => {
jest.spyOn(window, 'getSelection').mockImplementationOnce(() => null);
expect(getSelectedText()).toEqual(undefined);
jest.restoreAllMocks();
});

test('Returns selection text if Selection object is not null', () => {
jest
.spyOn(window, 'getSelection')
// @ts-ignore
.mockImplementationOnce(() => ({ toString: () => 'test string' }));
expect(getSelectedText()).toEqual('test string');
jest.restoreAllMocks();
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isAdhocColumn,
BinaryQueryObjectFilterClause,
t,
getSelectedText,
} from '@superset-ui/core';
import { PivotTable, sortAs, aggregatorTemplates } from './react-pivottable';
import {
Expand Down Expand Up @@ -356,6 +357,11 @@ export default function PivotTableChart(props: PivotTableProps) {
return;
}

// allow selecting text in a cell
if (getSelectedText()) {
return;
}

const isActiveFilterValue = (key: string, val: DataRecordValue) =>
!!selectedFilters && selectedFilters[key]?.includes(val);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
DTTM_ALIAS,
ensureIsArray,
GenericDataType,
getSelectedText,
getTimeFormatterForGranularity,
BinaryQueryObjectFilterClause,
styled,
Expand Down Expand Up @@ -493,7 +494,12 @@ export default function TableChart<D extends DataRecord = DataRecord>(
title: typeof value === 'number' ? String(value) : undefined,
onClick:
emitCrossFilters && !valueRange && !isMetric
? () => toggleFilter(key, value)
? () => {
// allow selecting text in a cell
if (!getSelectedText()) {
toggleFilter(key, value);
}
}
: undefined,
onContextMenu: (e: MouseEvent) => {
if (handleContextMenu) {
Expand Down

0 comments on commit d16512b

Please sign in to comment.