Skip to content

Commit

Permalink
fix: should be able to remove selection from X-AXIS control (#21371)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Sep 13, 2022
1 parent e1e9fda commit eb4ba5b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,35 @@
import {
FeatureFlag,
isFeatureEnabled,
QueryFormData,
t,
validateNonEmpty,
} from '@superset-ui/core';
import { ControlPanelState, ControlState } from '../types';

export const xAxisControlConfig = {
label: (state: ControlPanelState) => {
if (
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) &&
state?.form_data?.orientation === 'horizontal'
) {
return t('Y-axis');
}
const getAxisLabel = (
formData: QueryFormData,
): Record<'label' | 'description', string> =>
formData?.orientation === 'horizontal'
? { label: t('Y-axis'), description: t('Dimension to use on y-axis.') }
: { label: t('X-axis'), description: t('Dimension to use on x-axis.') };

return t('X-axis');
},
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;
},
export const xAxisControlConfig = {
label: (state: ControlPanelState) => getAxisLabel(state?.form_data).label,
multi: false,
description: (state: ControlPanelState) => {
description: (state: ControlPanelState) =>
getAxisLabel(state?.form_data).description,
validators: [validateNonEmpty],
initialValue: (control: ControlState, state: ControlPanelState) => {
if (
isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES) &&
state?.form_data?.orientation === 'horizontal'
state?.form_data?.granularity_sqla &&
!state.form_data?.x_axis &&
!control?.value
) {
return t('Dimension to use on y-axis.');
return state.form_data.granularity_sqla;
}

return t('Dimension to use on x-axis.');
return undefined;
},
validators: [validateNonEmpty],
default: undefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export interface BaseControlConfig<
chartState?: AnyDict,
) => ReactNode);
default?: V;
initialValue?: V;
renderTrigger?: boolean;
validators?: ControlValueValidator<T, O, V>[];
warning?: ReactNode;
Expand Down
15 changes: 15 additions & 0 deletions superset-frontend/src/explore/controlUtils/getControlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ export function applyMapStateToPropsToControl<T = ControlType>(
// `mapStateToProps` may also provide a value
value = value || state.value;
}

// InitialValue is used for setting value for the control,
// this value is not recalculated. The default value will override it.
if (typeof state.initialValue === 'function') {
state.initialValue = state.initialValue(state, controlPanelState);
// if default is still a function, discard
if (typeof state.initialValue === 'function') {
delete state.initialValue;
}
}
if (state.initialValue) {
value = state.initialValue;
delete state.initialValue;
}

// If default is a function, evaluate it
if (typeof state.default === 'function') {
state.default = state.default(state, controlPanelState);
Expand Down

0 comments on commit eb4ba5b

Please sign in to comment.