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

chore: Adds the theme object to chart properties #19951

Merged
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 @@ -24,6 +24,7 @@ import ErrorBoundary, {
} from 'react-error-boundary';
import { ParentSize } from '@vx/responsive';
import { createSelector } from 'reselect';
import { withTheme } from '@emotion/react';
import { parseLength, Dimension } from '../../dimension';
import SuperChartCore, { Props as SuperChartCoreProps } from './SuperChartCore';
import DefaultFallbackComponent from './FallbackComponent';
Expand Down Expand Up @@ -88,7 +89,7 @@ export type Props = Omit<SuperChartCoreProps, 'chartProps'> &

type PropsWithDefault = Props & Readonly<typeof defaultProps>;

export default class SuperChart extends React.PureComponent<Props, {}> {
class SuperChart extends React.PureComponent<Props, {}> {
/**
* SuperChart's core
*/
Expand Down Expand Up @@ -156,6 +157,7 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
queriesData,
enableNoResults,
noResults,
theme,
...rest
} = this.props as PropsWithDefault;

Expand All @@ -164,6 +166,7 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
queriesData,
height,
width,
theme,
});

let chart;
Expand Down Expand Up @@ -247,3 +250,5 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
return this.renderChart(widthInfo.value, heightInfo.value);
}
}

export default withTheme(SuperChart);
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from '../..';
import { HandlerFunction, PlainObject, SetDataMaskHook } from '../types/Base';
import { QueryData, DataRecordFilters } from '..';
import { SupersetTheme } from '../../style';

// TODO: more specific typing for these fields of ChartProps
type AnnotationData = PlainObject;
Expand Down Expand Up @@ -93,6 +94,8 @@ export interface ChartPropsConfig {
isRefreshing?: boolean;
/** chart ref */
inputRef?: RefObject<any>;
/** Theme object */
theme: SupersetTheme;
}

const DEFAULT_WIDTH = 800;
Expand Down Expand Up @@ -133,6 +136,8 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {

inputRef?: RefObject<any>;

theme: SupersetTheme;

constructor(config: ChartPropsConfig & { formData?: FormData } = {}) {
const {
annotationData = {},
Expand All @@ -149,6 +154,7 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {
appSection,
isRefreshing,
inputRef,
theme,
} = config;
this.width = width;
this.height = height;
Expand All @@ -166,6 +172,7 @@ export default class ChartProps<FormData extends RawFormData = RawFormData> {
this.appSection = appSection;
this.isRefreshing = isRefreshing;
this.inputRef = inputRef;
this.theme = theme;
}
}

Expand All @@ -186,6 +193,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
input => input.appSection,
input => input.isRefreshing,
input => input.inputRef,
input => input.theme,
(
annotationData,
datasource,
Expand All @@ -201,6 +209,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
appSection,
isRefreshing,
inputRef,
theme,
) =>
new ChartProps({
annotationData,
Expand All @@ -217,6 +226,7 @@ ChartProps.createSelector = function create(): ChartPropsSelector {
appSection,
isRefreshing,
inputRef,
theme,
}),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import {
ThemeProvider,
} from '@superset-ui/core';
import { mount as enzymeMount } from 'enzyme';
import RealSuperChart, {
WrapperProps,
} from '../../../src/chart/components/SuperChart';
import { WrapperProps } from '../../../src/chart/components/SuperChart';
import NoResultsComponent from '../../../src/chart/components/NoResultsComponent';

import {
Expand Down Expand Up @@ -111,9 +109,8 @@ describe('SuperChart', () => {
expectedErrors = 0;
});

it('renders default FallbackComponent', () => {
it('renders default FallbackComponent', async () => {
expectedErrors = 1;
jest.spyOn(RealSuperChart.defaultProps, 'FallbackComponent');
const wrapper = mount(
<SuperChart
chartType={ChartKeys.BUGGY}
Expand All @@ -122,14 +119,9 @@ describe('SuperChart', () => {
height="200"
/>,
);
const renderedWrapper = wrapper.render();

return promiseTimeout(() => {
expect(renderedWrapper.find('div.test-component')).toHaveLength(0);
expect(
RealSuperChart.defaultProps.FallbackComponent,
).toHaveBeenCalledTimes(1);
}, 100);
await new Promise(resolve => setImmediate(resolve));
wrapper.update();
expect(wrapper.text()).toContain('Oops! An error occured!');
});
it('renders custom FallbackComponent', () => {
expectedErrors = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React from 'react';
import { mount, shallow } from 'enzyme';
import mockConsole, { RestoreConsole } from 'jest-mock-console';

import { ChartProps, promiseTimeout } from '@superset-ui/core';
import { ChartProps, promiseTimeout, supersetTheme } from '@superset-ui/core';
import SuperChartCore from '../../../src/chart/components/SuperChartCore';
import {
ChartKeys,
Expand Down Expand Up @@ -123,6 +123,7 @@ describe('SuperChartCore', () => {
it('uses preTransformProps when specified', () => {
const chartPropsWithPayload = new ChartProps({
queriesData: [{ message: 'hulk' }],
theme: supersetTheme,
});
const wrapper = shallow(
<SuperChartCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getChartControlPanelRegistry,
QueryFormData,
DatasourceType,
supersetTheme,
} from '@superset-ui/core';

describe('ChartPlugin', () => {
Expand Down Expand Up @@ -130,6 +131,7 @@ describe('ChartPlugin', () => {
width: 400,
height: 400,
queriesData: [{}],
theme: supersetTheme,
});
it('defaults to identity function', () => {
const plugin = new ChartPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { Behavior, ChartProps } from '@superset-ui/core';
import { Behavior, ChartProps, supersetTheme } from '@superset-ui/core';

const RAW_FORM_DATA = {
some_field: 1,
Expand All @@ -42,6 +42,7 @@ describe('ChartProps', () => {
height: 600,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
theme: supersetTheme,
});
expect(props).toBeInstanceOf(ChartProps);
});
Expand All @@ -52,6 +53,7 @@ describe('ChartProps', () => {
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
theme: supersetTheme,
});
expect(props.formData.someField as number).toEqual(1);
expect(props.datasource.columnFormats).toEqual(
Expand All @@ -75,6 +77,7 @@ describe('ChartProps', () => {
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
theme: supersetTheme,
});
const props2 = selector({
width: 800,
Expand All @@ -84,6 +87,7 @@ describe('ChartProps', () => {
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
theme: supersetTheme,
});
expect(props1).toBe(props2);
});
Expand All @@ -101,6 +105,7 @@ describe('ChartProps', () => {
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: false,
theme: supersetTheme,
});
const props2 = selector({
width: 800,
Expand All @@ -110,6 +115,7 @@ describe('ChartProps', () => {
queriesData: QUERIES_DATA,
behaviors: BEHAVIORS,
isRefreshing: true,
theme: supersetTheme,
});
expect(props1).not.toBe(props2);
});
Expand All @@ -120,20 +126,23 @@ describe('ChartProps', () => {
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
theme: supersetTheme,
});
const props2 = selector({
width: 800,
height: 600,
datasource: RAW_DATASOURCE,
formData: { new_field: 3 },
queriesData: QUERIES_DATA,
theme: supersetTheme,
});
const props3 = selector({
width: 800,
height: 600,
datasource: RAW_DATASOURCE,
formData: RAW_FORM_DATA,
queriesData: QUERIES_DATA,
theme: supersetTheme,
});
expect(props1).not.toBe(props2);
expect(props1).not.toBe(props3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import { DatasourceType, TimeGranularity } from '@superset-ui/core';
import {
DatasourceType,
supersetTheme,
TimeGranularity,
} from '@superset-ui/core';
import transformProps from '../../src/BigNumber/BigNumberWithTrendline/transformProps';
import {
BigNumberDatum,
Expand Down Expand Up @@ -89,6 +93,7 @@ function generateProps(
ownState: {},
filterState: {},
behaviors: [],
theme: supersetTheme,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps, SqlaFormData } from '@superset-ui/core';
import { ChartProps, SqlaFormData, supersetTheme } from '@superset-ui/core';
import { EchartsBoxPlotChartProps } from '../../src/BoxPlot/types';
import transformProps from '../../src/BoxPlot/transformProps';

Expand Down Expand Up @@ -66,6 +66,7 @@ describe('BoxPlot transformProps', () => {
],
},
],
theme: supersetTheme,
});

it('should transform chart props for viz', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps, getNumberFormatter } from '@superset-ui/core';
import {
ChartProps,
getNumberFormatter,
supersetTheme,
} from '@superset-ui/core';
import transformProps, {
formatFunnelLabel,
} from '../../src/Funnel/transformProps';
Expand Down Expand Up @@ -45,6 +49,7 @@ describe('Funnel transformProps', () => {
],
},
],
theme: supersetTheme,
});

it('should transform chart props for viz', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps, SqlaFormData } from '@superset-ui/core';
import { ChartProps, SqlaFormData, supersetTheme } from '@superset-ui/core';
import transformProps from '../../src/Gauge/transformProps';
import { EchartsGaugeChartProps } from '../../src/Gauge/types';

Expand Down Expand Up @@ -63,6 +63,7 @@ describe('Echarts Gauge transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down Expand Up @@ -123,6 +124,7 @@ describe('Echarts Gauge transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down Expand Up @@ -200,6 +202,7 @@ describe('Echarts Gauge transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down Expand Up @@ -279,6 +282,7 @@ describe('Echarts Gauge transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps } from '@superset-ui/core';
import { ChartProps, supersetTheme } from '@superset-ui/core';
import transformProps from '../../src/Graph/transformProps';
import { DEFAULT_GRAPH_SERIES_OPTION } from '../../src/Graph/constants';

Expand Down Expand Up @@ -53,6 +53,7 @@ describe('EchartsGraph transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down Expand Up @@ -192,6 +193,7 @@ describe('EchartsGraph transformProps', () => {
width: 800,
height: 600,
queriesData,
theme: supersetTheme,
};

const chartProps = new ChartProps(chartPropsConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ChartProps,
getNumberFormatter,
SqlaFormData,
supersetTheme,
} from '@superset-ui/core';
import transformProps, { formatPieLabel } from '../../src/Pie/transformProps';
import { EchartsPieChartProps, EchartsPieLabelType } from '../../src/Pie/types';
Expand All @@ -45,6 +46,7 @@ describe('Pie transformProps', () => {
],
},
],
theme: supersetTheme,
});

it('should transform chart props for viz', () => {
Expand Down
Loading