Skip to content

Commit

Permalink
chore: Enable suppressing default chart context menu (#30613)
Browse files Browse the repository at this point in the history
(cherry picked from commit 47c5334)
  • Loading branch information
kgabryje authored and sadpandajoe committed Nov 12, 2024
1 parent 6f12303 commit 475a757
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export interface ChartMetadataConfig {
labelExplanation?: string | null;
queryObjectCount?: number;
parseMethod?: ParseMethod;
// suppressContextMenu: true hides the default context menu for the chart.
// This is useful for viz plugins that define their own context menu.
suppressContextMenu?: boolean;
}

export default class ChartMetadata {
Expand Down Expand Up @@ -91,6 +94,8 @@ export default class ChartMetadata {

parseMethod: ParseMethod;

suppressContextMenu?: boolean;

constructor(config: ChartMetadataConfig) {
const {
name,
Expand All @@ -111,6 +116,7 @@ export default class ChartMetadata {
labelExplanation = null,
queryObjectCount = 1,
parseMethod = 'json-bigint',
suppressContextMenu = false,
} = config;

this.name = name;
Expand Down Expand Up @@ -140,6 +146,7 @@ export default class ChartMetadata {
this.labelExplanation = labelExplanation;
this.queryObjectCount = queryObjectCount;
this.parseMethod = parseMethod;
this.suppressContextMenu = suppressContextMenu;
}

canBeAnnotationType(type: string): boolean {
Expand Down
4 changes: 4 additions & 0 deletions superset-frontend/src/components/Chart/ChartRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ const defaultProps = {
class ChartRenderer extends Component {
constructor(props) {
super(props);
const suppressContextMenu = getChartMetadataRegistry().get(
props.formData.viz_type ?? props.vizType,
)?.suppressContextMenu;
this.state = {
showContextMenu:
props.source === ChartSource.Dashboard &&
!suppressContextMenu &&
(isFeatureEnabled(FeatureFlag.DrillToDetail) ||
isFeatureEnabled(FeatureFlag.DashboardCrossFilters)),
inContextMenu: false,
Expand Down
32 changes: 31 additions & 1 deletion superset-frontend/src/components/Chart/ChartRenderer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/
import { render } from 'spec/helpers/testing-library';

import { ChartMetadata, getChartMetadataRegistry } from '@superset-ui/core';
import ChartRenderer from 'src/components/Chart/ChartRenderer';
import { ChartSource } from 'src/types/ChartSource';

jest.mock('@superset-ui/core', () => ({
...jest.requireActual('@superset-ui/core'),
Expand All @@ -40,8 +41,16 @@ const requiredProps = {
testControl: 'bar',
},
vizType: 'table',
source: ChartSource.Dashboard,
};

beforeAll(() => {
window.featureFlags = { DRILL_TO_DETAIL: true };
});
afterAll(() => {
window.featureFlags = {};
});

test('should render SuperChart', () => {
const { getByTestId } = render(
<ChartRenderer {...requiredProps} chartIsStale={false} />,
Expand All @@ -57,3 +66,24 @@ test('should use latestQueryFormData instead of formData when chartIsStale is tr
JSON.stringify({ testControl: 'bar' }),
);
});

test('should render chart context menu', () => {
const { getByTestId } = render(<ChartRenderer {...requiredProps} />);
expect(getByTestId('mock-chart-context-menu')).toBeInTheDocument();
});

test('should not render chart context menu if the context menu is suppressed for given viz plugin', () => {
getChartMetadataRegistry().registerValue(
'chart_without_context_menu',
new ChartMetadata({
name: 'chart with suppressed context menu',
thumbnail: '.png',
useLegacyApi: false,
suppressContextMenu: true,
}),
);
const { queryByTestId } = render(
<ChartRenderer {...requiredProps} vizType="chart_without_context_menu" />,
);
expect(queryByTestId('mock-chart-context-menu')).not.toBeInTheDocument();
});

0 comments on commit 475a757

Please sign in to comment.