Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: rename panel (#350)

islxyqwe0db2a9840dc1
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index 18578510..ce643087 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -24,6 +24,7 @@ import { guardDataKeys } from './utils/dataPrep';
import { getComputation } from './computation/clientComputation';
import LogPanel from './fields/datasetFields/logPanel';
import BinPanel from './fields/datasetFields/binPanel';
+import RenamePanel from './components/renameField';
import { ErrorContext } from './utils/reportError';
import { ErrorBoundary } from 'react-error-boundary';
import Errorpanel from './components/errorpanel';
@@ -201,6 +202,7 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
<VisualConfig />
<LogPanel />
<BinPanel />
+ <RenamePanel />
<ComputedFieldDialog />
<Painter themeConfig={themeConfig} themeKey={themeKey} />
{vizStore.showGeoJSONConfigPanel && <GeoConfigPanel geoList={props.geoList} />}
diff --git a/packages/graphic-walker/src/components/computedField/index.tsx b/packages/graphic-walker/src/components/computedField/index.tsx
index cacc01c3..d3ebcb58 100644
--- a/packages/graphic-walker/src/components/computedField/index.tsx
+++ b/packages/graphic-walker/src/components/computedField/index.tsx
@@ -1,5 +1,5 @@
import { observer } from 'mobx-react-lite';
-import React, { useState, useEffect, useRef, useMemo } from 'react';
+import React, { useState, useRef, useMemo, useEffect } from 'react';
import { useVizStore } from '../../store';
import { isNotEmpty, parseErrorMessage } from '../../utils';
import { highlightField } from '../highlightField';
@@ -22,7 +22,7 @@ const ComputedFieldDialog: React.FC = observer(() => {
const [name, setName] = useState<string>('');
const [sql, setSql] = useState<string>('');
const [error, setError] = useState<string>('');
- const ref = useRef<{ clear(): void; setValue(v: string): void }>(null);
+ const ref = useRef<HTMLDivElement>(null);
const SQLField = useMemo(() => {
const fields = vizStore.allFields
@@ -64,7 +64,7 @@ const ComputedFieldDialog: React.FC = observer(() => {
setSql('');
setError('');
});
- ref.current?.clear();
+ ref.current && (ref.current.innerHTML = '');
} else {
const f = vizStore.allFields.find((x) => x.fid === editingComputedFieldFid);
if (!f || !f.computed || f.expression?.op !== 'expr') {
@@ -81,14 +81,16 @@ const ComputedFieldDialog: React.FC = observer(() => {
setSql(sql.value);
setError('');
});
- ref.current?.setValue(sql.value);
+ ref.current && (ref.current.innerHTML = sql.value);
}
}
}, [editingComputedFieldFid, vizStore]);
+ if (!isNotEmpty(editingComputedFieldFid)) return null;
+
return (
<Dialog
- open={isNotEmpty(editingComputedFieldFid)}
+ open={true}
onOpenChange={() => {
vizStore.setComputedFieldFid();
}}
@@ -119,7 +121,7 @@ const ComputedFieldDialog: React.FC = observer(() => {
}}
/>
<label className="text-ml whitespace-nowrap">SQL</label>
- <SQLField ref={ref} onChange={setSql} placeholder="Enter SQL..." />
+ <SQLField ref={ref} value={sql} onChange={setSql} placeholder="Enter SQL..." />
</div>
{error && <div className="text-xs text-red-500">{error}</div>}
<div className="flex justify-end space-x-2">
diff --git a/packages/graphic-walker/src/components/highlightField.tsx b/packages/graphic-walker/src/components/highlightField.tsx
index 069f3e2e..3e3372c1 100644
--- a/packages/graphic-walker/src/components/highlightField.tsx
+++ b/packages/graphic-walker/src/components/highlightField.tsx
@@ -1,25 +1,14 @@
-import React, { useState, useRef, useImperativeHandle } from 'react';
+import React from 'react';
export interface TextFieldProps {
placeholder?: string;
onChange?: (v: string) => void;
+ value: string;
}
export function highlightField(highlighter: (value: string) => string) {
- return React.forwardRef<{ clear(): void; setValue(value: string): void }, TextFieldProps>(function TextField({ placeholder, onChange }, ref) {
- const [value, setValue] = useState('');
- const divRef = useRef<HTMLDivElement>(null);
+ return React.forwardRef<HTMLDivElement, TextFieldProps>(function TextField({ placeholder, onChange, value }, ref) {
const highlightValue = highlighter(value);
- useImperativeHandle(ref, () => ({
- clear() {
- divRef.current && (divRef.current.innerHTML = '');
- setValue('');
- },
- setValue(value) {
- divRef.current && (divRef.current.innerHTML = value);
- setValue(value);
- },
- }));
return (
<div className="relative flex min-h-[60px] w-full rounded-md border border-input bg-transparent text-sm shadow-sm">
<div className="absolute whitespace-pre inset-0 pointer-events-none px-3 py-2" dangerouslySetInnerHTML={{ __html: highlightValue }} />
@@ -27,11 +16,10 @@ export function highlightField(highlighter: (value: string) => string) {
<div className="px-3 py-2 pointer-events-none text-muted-foreground absolute inset-0 select-none">{placeholder}</div>
)}
<div
- ref={divRef}
+ ref={ref}
contentEditable="plaintext-only"
onInput={(e) => {
const text = e.currentTarget.textContent ?? '';
- setValue(text);
onChange?.(text);
}}
className="px-3 py-2 w-full focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 rounded-md border-0 text-transparent caret-foreground"
diff --git a/packages/graphic-walker/src/components/renameField/index.tsx b/packages/graphic-walker/src/components/renameField/index.tsx
new file mode 100644
index 00000000..b04ba169
--- /dev/null
+++ b/packages/graphic-walker/src/components/renameField/index.tsx
@@ -0,0 +1,62 @@
+import { observer } from 'mobx-react-lite';
+import React, { useEffect, useState } from 'react';
+import { useVizStore } from '../../store';
+import { useTranslation } from 'react-i18next';
+import { ICreateField } from '../../interfaces';
+import { Dialog, DialogContent, DialogFooter } from '@/components/ui/dialog';
+import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+
+const RenamePanel: React.FC = (props) => {
+ const vizStore = useVizStore();
+ const { showRenamePanel, createField, currentEncodings } = vizStore;
+ const { t } = useTranslation();
+ const [value, setValue] = useState<string>('');
+
+ useEffect(() => {
+ if (createField) {
+ setValue(currentEncodings[createField.channel]?.[createField.index]?.name ?? '');
+ }
+ }, [showRenamePanel]);
+
+ return (
+ <Dialog open={showRenamePanel} onOpenChange={() => vizStore.setShowRenamePanel(false)}>
+ <DialogContent className="!w-fit">
+ <div className="flex flex-col justify-center items-start text-xs">
+ <h2 className="text-lg font-medium mb-2">Rename Field</h2>
+ <p className="font-normal text-muted-foreground">This action will rename this field's name in the chart.</p>
+ <div className="flex items-center space-x-2 mt-2">
+ <label className="text-ml whitespace-nowrap">New Name</label>
+ <Input
+ type="text"
+ value={value}
+ onChange={(e) => {
+ setValue(e.target.value);
+ }}
+ />
+ </div>
+ </div>
+ <DialogFooter className="mt-2">
+ <Button
+ children={t('actions.confirm')}
+ onClick={() => {
+ const field = vizStore.createField as ICreateField;
+ vizStore.renameFieldInChart(field.channel, field.index, value);
+ vizStore.setShowRenamePanel(false);
+ return;
+ }}
+ />
+ <Button
+ variant="outline"
+ children={t('actions.cancel')}
+ onClick={() => {
+ vizStore.setShowRenamePanel(false);
+ return;
+ }}
+ />
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+};
+export default observer(RenamePanel);
diff --git a/packages/graphic-walker/src/fields/datasetFields/utils.ts b/packages/graphic-walker/src/fields/datasetFields/utils.ts
index bfa19e7a..13f53ab2 100644
--- a/packages/graphic-walker/src/fields/datasetFields/utils.ts
+++ b/packages/graphic-walker/src/fields/datasetFields/utils.ts
@@ -152,6 +152,13 @@ export const useMenuActions = (channel: 'dimensions' | 'measures'): IActionMenuI
},
})),
},
+ {
+ label: "Rename Field",
+ onPress() {
+ vizStore.setShowRenamePanel(true);
+ vizStore.setCreateField({ channel: channel, index: index });
+ },
+ },
f.computed &&
!isInnerField &&
f.expression?.op === 'expr' && {
diff --git a/packages/graphic-walker/src/models/visSpecHistory.ts b/packages/graphic-walker/src/models/visSpecHistory.ts
index 5a7be73a..944dc03c 100644
--- a/packages/graphic-walker/src/models/visSpecHistory.ts
+++ b/packages/graphic-walker/src/models/visSpecHistory.ts
@@ -28,7 +28,8 @@ import { WithHistory, atWith, create, freeze, performWith, redoWith, undoWith }
import { GLOBAL_CONFIG } from '../config';
import { COUNT_FIELD_ID, DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS, MEA_KEY_ID, MEA_VAL_ID, PAINT_FIELD_ID } from '../constants';
import { algebraLint } from '../lib/gog';
-import { getSQLItemAnalyticType, parseSQLExpr } from '../lib/sql';
+import { getSQLItemAnalyticType, parseSQLExpr, replaceFid } from '../lib/sql';
+import produce from 'immer';
type normalKeys = keyof Omit<DraggableFieldState, 'filters'>;
@@ -441,6 +442,30 @@ const actions: {
);
},
[Methods.editAllField]: (data, fid, newData) => {
+ if (Object.keys(newData).includes('name')) {
+ const originalField = data.encodings.dimensions.concat(data.encodings.measures).find((x) => x.fid === fid);
+ // if name is changed, update all computed fields
+ return produce(data, (draft) => {
+ if (!originalField) return;
+ Object.values(draft.encodings).forEach((fields) =>
+ fields.forEach((field, i) => {
+ if (field.fid === fid) {
+ fields[i] = { ...field, ...newData };
+ } else if (field.expression?.op === 'expr') {
+ const sqlParam = field.expression.params.find((x) => x.type === 'sql');
+ if (sqlParam) {
+ const newVal = replaceFid(sqlParam.value, [
+ { ...originalField, fid: newData.name!, name: originalField.name ?? originalField.fid },
+ ]);
+ if (newVal !== sqlParam.value) {
+ sqlParam.value = newVal;
+ }
+ }
+ }
+ })
+ );
+ });
+ }
return mutPath(
data,
'encodings',
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 43d3bc8f..dbda3d4b 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -39,6 +39,7 @@ import {
IPaintMap,
IPaintMapV2,
IDefaultConfig,
+ FieldIdentifier,
} from '../interfaces';
import { GLOBAL_CONFIG } from '../config';
import { COUNT_FIELD_ID, DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS, PAINT_FIELD_ID, MEA_KEY_ID, MEA_VAL_ID } from '../constants';
@@ -73,6 +74,7 @@ export class VizSpecStore {
selectedMarkObject: Record<string, string | number | undefined> = {};
showLogSettingPanel: boolean = false;
showBinSettingPanel: boolean = false;
+ showRenamePanel: boolean = false;
createField: ICreateField | undefined = undefined;
localGeoJSON: FeatureCollection | undefined = undefined;
showErrorResolutionPanel: number = 0;
@@ -463,6 +465,14 @@ export class VizSpecStore {
this.visList[this.visIndex] = performers.createBinlogField(this.visList[this.visIndex], stateKey, index, scaleType, uniqueId(), logNumber);
}
+ renameFieldInChart(stateKey: keyof Omit<DraggableFieldState, 'filters'>, index: number, newName: string) {
+ const origianlField = this.currentEncodings[stateKey][index];
+ if (!origianlField) {
+ return;
+ }
+ this.visList[this.visIndex] = performers.editAllField(this.visList[this.visIndex], origianlField.fid, { name: newName });
+ }
+
public createDateTimeDrilledField(
stateKey: keyof Omit<DraggableFieldState, 'filters'>,
index: number,
@@ -681,6 +691,10 @@ export class VizSpecStore {
this.showLogSettingPanel = show;
}
+ setShowRenamePanel(show: boolean) {
+ this.showRenamePanel = show;
+ }
+
setCreateField(field: ICreateField) {
this.createField = field;
}