fix: set label on adhoc column should persist (#26154)

This commit is contained in:
Beto Dealmeida
2023-12-01 08:40:52 -05:00
committed by GitHub
parent f405ba033e
commit b2ea97a984
3 changed files with 121 additions and 8 deletions

View File

@@ -68,6 +68,7 @@ interface ColumnSelectPopoverProps {
editedColumn?: ColumnMeta | AdhocColumn;
onChange: (column: ColumnMeta | AdhocColumn) => void;
onClose: () => void;
hasCustomLabel: boolean;
setLabel: (title: string) => void;
getCurrentTab: (tab: string) => void;
label: string;
@@ -93,13 +94,14 @@ const getInitialColumnValues = (
const ColumnSelectPopover = ({
columns,
editedColumn,
getCurrentTab,
hasCustomLabel,
isTemporal,
label,
onChange,
onClose,
setDatasetModal,
setLabel,
getCurrentTab,
label,
isTemporal,
}: ColumnSelectPopoverProps) => {
const datasourceType = useSelector<ExplorePageState, string | undefined>(
state => state.explore.datasource.type,
@@ -117,6 +119,7 @@ const ColumnSelectPopover = ({
const [selectedSimpleColumn, setSelectedSimpleColumn] = useState<
ColumnMeta | undefined
>(initialSimpleColumn);
const [selectedTab, setSelectedTab] = useState<string | null>(null);
const [resizeButton, width, height] = useResizeButton(
POPOVER_INITIAL_WIDTH,
@@ -188,7 +191,34 @@ const ColumnSelectPopover = ({
useEffect(() => {
getCurrentTab(defaultActiveTabKey);
}, [defaultActiveTabKey, getCurrentTab]);
setSelectedTab(defaultActiveTabKey);
}, [defaultActiveTabKey, getCurrentTab, setSelectedTab]);
useEffect(() => {
/* if the adhoc column is not set (because it was never edited) but the
* tab is selected and the label has changed, then we need to set the
* adhoc column manually */
if (
adhocColumn === undefined &&
selectedTab === 'sqlExpression' &&
hasCustomLabel
) {
const sqlExpression =
selectedSimpleColumn?.column_name ||
selectedCalculatedColumn?.expression ||
'';
setAdhocColumn({ label, sqlExpression, expressionType: 'SQL' });
}
}, [
adhocColumn,
defaultActiveTabKey,
hasCustomLabel,
getCurrentTab,
label,
selectedCalculatedColumn,
selectedSimpleColumn,
selectedTab,
]);
const onSave = useCallback(() => {
if (adhocColumn && adhocColumn.label !== label) {
@@ -225,6 +255,7 @@ const ColumnSelectPopover = ({
const onTabChange = useCallback(
tab => {
getCurrentTab(tab);
setSelectedTab(tab);
// @ts-ignore
sqlEditorRef.current?.editor.focus();
},