Skip to content

Commit

Permalink
fix(generic-chart-axes): set x-axis if unset and ff is enabled (#20107)
Browse files Browse the repository at this point in the history
* fix(generic-chart-axes): set x-axis if unset and ff is enabled

* simplify

* simplify

* continue cleanup

* yet more cleanup
  • Loading branch information
villebro committed May 19, 2022
1 parent 660af40 commit 0b3d3dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import React from 'react';
import { t, validateNonEmpty } from '@superset-ui/core';
import {
formatSelectOptionsForRange,
ColumnOption,
columnChoices,
ColumnOption,
ColumnMeta,
ControlPanelConfig,
ControlState,
formatSelectOptionsForRange,
sections,
SelectControlConfig,
ColumnMeta,
} from '@superset-ui/chart-controls';

const config: ControlPanelConfig = {
Expand All @@ -46,10 +47,8 @@ const config: ControlPanelConfig = {
choices: columnChoices(state?.datasource),
}),
// choices is from `mapStateToProps`
default: (control: { choices?: string[] }) =>
control.choices && control.choices.length > 0
? control.choices[0][0]
: null,
default: (control: ControlState) =>
control.choices?.[0]?.[0] || null,
validators: [validateNonEmpty],
},
},
Expand Down
26 changes: 24 additions & 2 deletions superset-frontend/plugins/plugin-chart-echarts/src/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
* under the License.
*/
import React from 'react';
import { t, validateNonEmpty } from '@superset-ui/core';
import {
FeatureFlag,
isFeatureEnabled,
t,
validateNonEmpty,
} from '@superset-ui/core';
import {
ControlPanelsContainerProps,
ControlPanelState,
ControlSetItem,
ControlSetRow,
ControlState,
sharedControls,
} from '@superset-ui/chart-controls';
import { DEFAULT_LEGEND_FORM_DATA } from './types';
Expand Down Expand Up @@ -143,7 +150,22 @@ export const xAxisControl: ControlSetItem = {
config: {
...sharedControls.groupby,
label: t('X-axis'),
default: null,
default: (
control: ControlState,
controlPanel: Partial<ControlPanelState>,
) => {
// default to the chosen time column if x-axis is unset and the
// GENERIC_CHART_AXES feature flag is enabled
const { value } = control;
if (value) {
return value;
}
const timeColumn = controlPanel?.form_data?.granularity_sqla;
if (isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) && timeColumn) {
return timeColumn;
}
return null;
},
multi: false,
description: t('Dimension to use on x-axis.'),
validators: [validateNonEmpty],
Expand Down
11 changes: 4 additions & 7 deletions superset-frontend/src/explore/controlUtils/getControlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function handleMissingChoice<T = ControlType>(control: ControlState<T>) {

export function applyMapStateToPropsToControl<T = ControlType>(
controlState: ControlState<T>,
controlPanelState: Partial<ControlPanelState>,
controlPanelState: Partial<ControlPanelState> | null,
) {
const { mapStateToProps } = controlState;
let state = { ...controlState };
Expand Down Expand Up @@ -120,7 +120,7 @@ export function applyMapStateToPropsToControl<T = ControlType>(

export function getControlStateFromControlConfig<T = ControlType>(
controlConfig: ControlConfig<T> | null,
controlPanelState: Partial<ControlPanelState>,
controlPanelState: Partial<ControlPanelState> | null,
value?: JsonValue,
) {
// skip invalid config values
Expand All @@ -130,10 +130,7 @@ export function getControlStateFromControlConfig<T = ControlType>(
const controlState = { ...controlConfig, value } as ControlState<T>;
// only apply mapStateToProps when control states have been initialized
// or when explicitly didn't provide control panel state (mostly for testing)
if (
(controlPanelState && controlPanelState.controls) ||
controlPanelState === null
) {
if (controlPanelState?.controls || controlPanelState === null) {
return applyMapStateToPropsToControl(controlState, controlPanelState);
}
return controlState;
Expand All @@ -155,7 +152,7 @@ export function getControlState(
export function getAllControlsState(
vizType: string,
datasourceType: DatasourceType,
state: ControlPanelState,
state: ControlPanelState | null,
formData: QueryFormData,
) {
const controlsState = {};
Expand Down
9 changes: 6 additions & 3 deletions superset-frontend/src/explore/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ export function getControlsState(state, inputFormData) {
export function applyDefaultFormData(inputFormData) {
const datasourceType = inputFormData.datasource.split('__')[1];
const vizType = inputFormData.viz_type;
const controlsState = getAllControlsState(vizType, datasourceType, null, {
...inputFormData,
});
const controlsState = getAllControlsState(
vizType,
datasourceType,
null,
inputFormData,
);
const controlFormData = getFormDataFromControls(controlsState);

const formData = {};
Expand Down

0 comments on commit 0b3d3dd

Please sign in to comment.