chore: Enable suppressing default chart context menu (#30613)

This commit is contained in:
Kamil Gabryjelski 2024-10-18 15:12:54 +02:00 committed by GitHub
parent 950a21a020
commit 47c5334502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 1 deletions

View File

@ -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 {
@ -91,6 +94,8 @@ export default class ChartMetadata {
parseMethod: ParseMethod;
suppressContextMenu?: boolean;
constructor(config: ChartMetadataConfig) {
const {
name,
@ -111,6 +116,7 @@ export default class ChartMetadata {
labelExplanation = null,
queryObjectCount = 1,
parseMethod = 'json-bigint',
suppressContextMenu = false,
} = config;
this.name = name;
@ -140,6 +146,7 @@ export default class ChartMetadata {
this.labelExplanation = labelExplanation;
this.queryObjectCount = queryObjectCount;
this.parseMethod = parseMethod;
this.suppressContextMenu = suppressContextMenu;
}
canBeAnnotationType(type: string): boolean {

View File

@ -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,

View File

@ -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'),
@ -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} />,
@ -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();
});