refactored NamedRanges logic and styles
This commit is contained in:
committed by
Nicolás Hatcher Andrés
parent
f2757e7d76
commit
4660f0e456
@@ -14,33 +14,49 @@ import { t } from "i18next";
|
|||||||
import { BookOpen, Plus, X } from "lucide-react";
|
import { BookOpen, Plus, X } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { getFullRangeToString } from "../util";
|
import { getFullRangeToString } from "../util";
|
||||||
import NamedRange from "./NamedRange";
|
import NamedRangeActive from "./NamedRangeActive";
|
||||||
|
import NamedRangeInactive from "./NamedRangeInactive";
|
||||||
|
|
||||||
interface NameManagerDialogProperties {
|
interface NameManagerDialogProperties {
|
||||||
onClose: () => void;
|
|
||||||
onNamesChanged: () => void;
|
|
||||||
open: boolean;
|
open: boolean;
|
||||||
model: Model;
|
model: Model;
|
||||||
|
onClose: () => void;
|
||||||
|
onNamesChanged: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function NameManagerDialog(properties: NameManagerDialogProperties) {
|
function NameManagerDialog(properties: NameManagerDialogProperties) {
|
||||||
const { onClose, open, model, onNamesChanged } = properties;
|
const { open, model, onClose, onNamesChanged } = properties;
|
||||||
|
// If editingNameIndex is -1, then we are adding a new name
|
||||||
const [showNewName, setShowNewName] = useState(false);
|
// If editingNameIndex is -2, then we are not editing any name
|
||||||
|
// If editingNameIndex is a positive number, then we are editing that index
|
||||||
|
const [editingNameIndex, setEditingNameIndex] = useState(-2);
|
||||||
const [showOptions, setShowOptions] = useState(true);
|
const [showOptions, setShowOptions] = useState(true);
|
||||||
|
const worksheets = model.getWorksheetsProperties();
|
||||||
|
const definedNameList = model.getDefinedNameList();
|
||||||
|
|
||||||
|
// reset modal state in case editing was in progress
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setShowNewName(false);
|
if (open) {
|
||||||
|
setEditingNameIndex(-2);
|
||||||
|
}
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
// enable/disable options while editing
|
||||||
|
useEffect(() => {
|
||||||
|
if (editingNameIndex !== -2) {
|
||||||
|
setShowOptions(false);
|
||||||
|
} else {
|
||||||
setShowOptions(true);
|
setShowOptions(true);
|
||||||
}, []);
|
}
|
||||||
|
}, [editingNameIndex]);
|
||||||
|
|
||||||
const handleNewName = () => {
|
const handleNewName = () => {
|
||||||
toggleShowNewName();
|
setEditingNameIndex(-1);
|
||||||
toggleOptions();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCreate = () => {
|
const handleSave = () => {
|
||||||
toggleShowNewName();
|
setEditingNameIndex(-2);
|
||||||
|
onNamesChanged();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = () => {
|
const handleDelete = () => {
|
||||||
@@ -54,22 +70,11 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
return getFullRangeToString(selectedView, worksheetNames);
|
return getFullRangeToString(selectedView, worksheetNames);
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleOptions = () => {
|
|
||||||
setShowOptions(!showOptions);
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleShowNewName = () => {
|
|
||||||
setShowNewName(!showNewName);
|
|
||||||
};
|
|
||||||
|
|
||||||
const worksheets = model.getWorksheetsProperties();
|
|
||||||
const definedNameList = model.getDefinedNameList();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledDialog open={open} onClose={onClose} maxWidth={false} scroll="paper">
|
<StyledDialog open={open} onClose={onClose} maxWidth={false} scroll="paper">
|
||||||
<StyledDialogTitle>
|
<StyledDialogTitle>
|
||||||
{t("name_manager_dialog.title")}
|
{t("name_manager_dialog.title")}
|
||||||
<IconButton onClick={() => onClose()}>
|
<IconButton onClick={onClose}>
|
||||||
<X size={16} />
|
<X size={16} />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</StyledDialogTitle>
|
</StyledDialogTitle>
|
||||||
@@ -79,8 +84,24 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
<StyledBox>{t("name_manager_dialog.range")}</StyledBox>
|
<StyledBox>{t("name_manager_dialog.range")}</StyledBox>
|
||||||
<StyledBox>{t("name_manager_dialog.scope")}</StyledBox>
|
<StyledBox>{t("name_manager_dialog.scope")}</StyledBox>
|
||||||
</StyledRangesHeader>
|
</StyledRangesHeader>
|
||||||
{definedNameList.map((definedName) => (
|
<NameListWrapper>
|
||||||
<NamedRange
|
{definedNameList.map((definedName, index) => {
|
||||||
|
if (index === editingNameIndex) {
|
||||||
|
return (
|
||||||
|
<NamedRangeActive
|
||||||
|
model={model}
|
||||||
|
worksheets={worksheets}
|
||||||
|
name={definedName.name}
|
||||||
|
scope={definedName.scope}
|
||||||
|
formula={definedName.formula}
|
||||||
|
key={definedName.name}
|
||||||
|
onSave={handleSave}
|
||||||
|
onCancel={() => setEditingNameIndex(-2)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<NamedRangeInactive
|
||||||
model={model}
|
model={model}
|
||||||
worksheets={worksheets}
|
worksheets={worksheets}
|
||||||
name={definedName.name}
|
name={definedName.name}
|
||||||
@@ -88,20 +109,20 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
formula={definedName.formula}
|
formula={definedName.formula}
|
||||||
key={definedName.name}
|
key={definedName.name}
|
||||||
showOptions={showOptions}
|
showOptions={showOptions}
|
||||||
toggleOptions={toggleOptions}
|
onEdit={() => setEditingNameIndex(index)}
|
||||||
onDelete={handleDelete}
|
onDelete={handleDelete}
|
||||||
/>
|
/>
|
||||||
))}
|
);
|
||||||
{showNewName && (
|
})}
|
||||||
<NamedRange
|
</NameListWrapper>
|
||||||
|
{editingNameIndex === -1 && (
|
||||||
|
<NamedRangeActive
|
||||||
model={model}
|
model={model}
|
||||||
worksheets={worksheets}
|
worksheets={worksheets}
|
||||||
name={""}
|
name={""}
|
||||||
formula={formatFormula()}
|
formula={formatFormula()}
|
||||||
showOptions={showOptions}
|
onSave={handleSave}
|
||||||
toggleOptions={toggleOptions}
|
onCancel={() => setEditingNameIndex(-2)}
|
||||||
toggleShowNewName={toggleShowNewName}
|
|
||||||
onCreate={handleCreate}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</StyledDialogContent>
|
</StyledDialogContent>
|
||||||
@@ -118,7 +139,7 @@ function NameManagerDialog(properties: NameManagerDialogProperties) {
|
|||||||
disableElevation
|
disableElevation
|
||||||
sx={{ textTransform: "none" }}
|
sx={{ textTransform: "none" }}
|
||||||
startIcon={<Plus size={16} />}
|
startIcon={<Plus size={16} />}
|
||||||
disabled={!showOptions}
|
disabled={editingNameIndex > -2}
|
||||||
>
|
>
|
||||||
{t("name_manager_dialog.new")}
|
{t("name_manager_dialog.new")}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -143,6 +164,11 @@ align-items: center;
|
|||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const NameListWrapper = styled(Stack)`
|
||||||
|
overflow-y: auto;
|
||||||
|
gap: 12px;
|
||||||
|
`;
|
||||||
|
|
||||||
const StyledBox = styled(Box)`
|
const StyledBox = styled(Box)`
|
||||||
width: 171px;
|
width: 171px;
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import {
|
|||||||
styled,
|
styled,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import { Check, PencilLine, Trash2, X } from "lucide-react";
|
import { Check, X } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
interface NamedRangeProperties {
|
interface NamedRangeProperties {
|
||||||
model: Model;
|
model: Model;
|
||||||
@@ -17,91 +17,39 @@ interface NamedRangeProperties {
|
|||||||
name: string;
|
name: string;
|
||||||
scope?: number;
|
scope?: number;
|
||||||
formula: string;
|
formula: string;
|
||||||
onCreate?: () => void;
|
onSave: () => void;
|
||||||
onDelete?: () => void;
|
onDelete?: () => void;
|
||||||
toggleShowNewName?: () => void;
|
onCancel?: () => void;
|
||||||
toggleOptions: () => void;
|
|
||||||
showOptions?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function NamedRange(properties: NamedRangeProperties) {
|
function NamedRangeActive(properties: NamedRangeProperties) {
|
||||||
const {
|
const { model, worksheets, name, scope, formula, onSave, onCancel } =
|
||||||
model,
|
properties;
|
||||||
worksheets,
|
|
||||||
name,
|
|
||||||
scope,
|
|
||||||
formula,
|
|
||||||
onCreate,
|
|
||||||
onDelete,
|
|
||||||
toggleShowNewName,
|
|
||||||
toggleOptions,
|
|
||||||
showOptions,
|
|
||||||
} = properties;
|
|
||||||
|
|
||||||
const [newName, setNewName] = useState(name);
|
const [newName, setNewName] = useState(name);
|
||||||
const [newScope, setNewScope] = useState(scope);
|
const [newScope, setNewScope] = useState(scope);
|
||||||
const [newFormula, setNewFormula] = useState(formula);
|
const [newFormula, setNewFormula] = useState(formula);
|
||||||
const [readOnly, setReadOnly] = useState(true);
|
|
||||||
const [showEditDelete, setShowEditDelete] = useState(false);
|
|
||||||
|
|
||||||
// todo: add error messages for validations
|
// todo: add error messages for validations
|
||||||
const [nameError, setNameError] = useState(false);
|
const [nameError, setNameError] = useState(false);
|
||||||
const [formulaError, setFormulaError] = useState(false);
|
const [formulaError, setFormulaError] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const handleSaveUpdate = () => {
|
||||||
// set state for new name
|
|
||||||
const definedNamesModel = model.getDefinedNameList();
|
|
||||||
if (!definedNamesModel.find((n) => n.name === newName)) {
|
|
||||||
setReadOnly(false);
|
|
||||||
setShowEditDelete(true);
|
|
||||||
}
|
|
||||||
}, [newName, model]);
|
|
||||||
|
|
||||||
const handleCreateUpdate = () => {
|
|
||||||
const definedNamesModel = model.getDefinedNameList();
|
const definedNamesModel = model.getDefinedNameList();
|
||||||
|
|
||||||
if (definedNamesModel.find((n) => n.name === name)) {
|
if (definedNamesModel.find((n) => n.name === name)) {
|
||||||
// update
|
|
||||||
try {
|
try {
|
||||||
model.updateDefinedName(name, scope, newName, newScope, newFormula);
|
model.updateDefinedName(name, scope, newName, newScope, newFormula);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("DefinedName update failed", error);
|
console.log("DefinedName update failed", error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// create
|
|
||||||
try {
|
try {
|
||||||
model.newDefinedName(newName, newScope, newFormula);
|
model.newDefinedName(newName, newScope, newFormula);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("DefinedName save failed", error);
|
console.log("DefinedName save failed", error);
|
||||||
}
|
}
|
||||||
onCreate?.();
|
|
||||||
}
|
}
|
||||||
setShowEditDelete(false);
|
onSave();
|
||||||
toggleOptions();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleCancel = () => {
|
|
||||||
setReadOnly(true);
|
|
||||||
setShowEditDelete(false);
|
|
||||||
toggleOptions();
|
|
||||||
setNewName(name);
|
|
||||||
setNewScope(scope);
|
|
||||||
toggleShowNewName?.(); // if it's newName remove it from modal
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEdit = () => {
|
|
||||||
setReadOnly(false);
|
|
||||||
setShowEditDelete(true);
|
|
||||||
toggleOptions();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDelete = () => {
|
|
||||||
try {
|
|
||||||
model.deleteDefinedName(newName, newScope);
|
|
||||||
} catch (error) {
|
|
||||||
console.log("DefinedName delete failed", error);
|
|
||||||
}
|
|
||||||
onDelete?.();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -113,7 +61,6 @@ function NamedRange(properties: NamedRangeProperties) {
|
|||||||
size="small"
|
size="small"
|
||||||
margin="none"
|
margin="none"
|
||||||
fullWidth
|
fullWidth
|
||||||
InputProps={{ readOnly: readOnly }}
|
|
||||||
error={nameError}
|
error={nameError}
|
||||||
value={newName}
|
value={newName}
|
||||||
onChange={(event) => setNewName(event.target.value)}
|
onChange={(event) => setNewName(event.target.value)}
|
||||||
@@ -129,7 +76,6 @@ function NamedRange(properties: NamedRangeProperties) {
|
|||||||
size="small"
|
size="small"
|
||||||
margin="none"
|
margin="none"
|
||||||
fullWidth
|
fullWidth
|
||||||
InputProps={{ readOnly: readOnly }}
|
|
||||||
value={newScope ?? "global"}
|
value={newScope ?? "global"}
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
event.target.value === "global"
|
event.target.value === "global"
|
||||||
@@ -152,7 +98,6 @@ function NamedRange(properties: NamedRangeProperties) {
|
|||||||
size="small"
|
size="small"
|
||||||
margin="none"
|
margin="none"
|
||||||
fullWidth
|
fullWidth
|
||||||
InputProps={{ readOnly: readOnly }}
|
|
||||||
error={formulaError}
|
error={formulaError}
|
||||||
value={newFormula}
|
value={newFormula}
|
||||||
onChange={(event) => setNewFormula(event.target.value)}
|
onChange={(event) => setNewFormula(event.target.value)}
|
||||||
@@ -161,34 +106,14 @@ function NamedRange(properties: NamedRangeProperties) {
|
|||||||
}}
|
}}
|
||||||
onClick={(event) => event.stopPropagation()}
|
onClick={(event) => event.stopPropagation()}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{showEditDelete ? (
|
|
||||||
// save cancel
|
|
||||||
<>
|
<>
|
||||||
<IconButton
|
<IconButton onClick={handleSaveUpdate}>
|
||||||
onClick={handleCreateUpdate}
|
<StyledCheck size={12} />
|
||||||
sx={{ color: (theme) => theme.palette.success.main }}
|
|
||||||
>
|
|
||||||
<Check size={12} />
|
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<IconButton
|
<StyledIconButton onClick={onCancel}>
|
||||||
onClick={handleCancel}
|
|
||||||
sx={{ color: (theme) => theme.palette.error.main }}
|
|
||||||
>
|
|
||||||
<X size={12} />
|
<X size={12} />
|
||||||
</IconButton>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
// edit delete
|
|
||||||
<>
|
|
||||||
<IconButton onClick={handleEdit} disabled={!showOptions}>
|
|
||||||
<PencilLine size={12} />
|
|
||||||
</IconButton>
|
|
||||||
<StyledIconButton onClick={handleDelete} disabled={!showOptions}>
|
|
||||||
<Trash2 size={12} />
|
|
||||||
</StyledIconButton>
|
</StyledIconButton>
|
||||||
</>
|
</>
|
||||||
)}
|
|
||||||
</StyledBox>
|
</StyledBox>
|
||||||
<Divider />
|
<Divider />
|
||||||
</>
|
</>
|
||||||
@@ -205,6 +130,11 @@ const StyledTextField = styled(TextField)(() => ({
|
|||||||
"& .MuiInputBase-root": {
|
"& .MuiInputBase-root": {
|
||||||
height: "28px",
|
height: "28px",
|
||||||
margin: 0,
|
margin: 0,
|
||||||
|
fontFamily: "Inter",
|
||||||
|
fontSize: "12px",
|
||||||
|
},
|
||||||
|
"& .MuiInputBase-input": {
|
||||||
|
padding: "8px",
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -216,4 +146,8 @@ const StyledIconButton = styled(IconButton)(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export default NamedRange;
|
const StyledCheck = styled(Check)(({ theme }) => ({
|
||||||
|
color: theme.palette.success.main,
|
||||||
|
}));
|
||||||
|
|
||||||
|
export default NamedRangeActive;
|
||||||
93
webapp/src/components/NameManager/NamedRangeInactive.tsx
Normal file
93
webapp/src/components/NameManager/NamedRangeInactive.tsx
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import type { Model, WorksheetProperties } from "@ironcalc/wasm";
|
||||||
|
import { Box, Divider, IconButton, styled } from "@mui/material";
|
||||||
|
import { t } from "i18next";
|
||||||
|
import { PencilLine, Trash2 } from "lucide-react";
|
||||||
|
|
||||||
|
interface NamedRangeInactiveProperties {
|
||||||
|
model: Model;
|
||||||
|
worksheets: WorksheetProperties[];
|
||||||
|
name: string;
|
||||||
|
scope?: number;
|
||||||
|
formula: string;
|
||||||
|
onDelete: () => void;
|
||||||
|
onEdit: () => void;
|
||||||
|
showOptions: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function NamedRangeInactive(properties: NamedRangeInactiveProperties) {
|
||||||
|
const {
|
||||||
|
model,
|
||||||
|
worksheets,
|
||||||
|
name,
|
||||||
|
scope,
|
||||||
|
formula,
|
||||||
|
onDelete,
|
||||||
|
onEdit,
|
||||||
|
showOptions,
|
||||||
|
} = properties;
|
||||||
|
|
||||||
|
const handleDelete = () => {
|
||||||
|
try {
|
||||||
|
model.deleteDefinedName(name, scope);
|
||||||
|
} catch (error) {
|
||||||
|
console.log("DefinedName delete failed", error);
|
||||||
|
}
|
||||||
|
onDelete();
|
||||||
|
};
|
||||||
|
|
||||||
|
const scopeName =
|
||||||
|
worksheets.find((sheet, index) => index === scope)?.name ||
|
||||||
|
t("name_manager_dialog.workbook");
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<WrappedLine>
|
||||||
|
<StyledDiv>{name}</StyledDiv>
|
||||||
|
<StyledDiv>{scopeName}</StyledDiv>
|
||||||
|
<StyledDiv>{formula}</StyledDiv>
|
||||||
|
<WrappedIcons>
|
||||||
|
<StyledIconButtonBlack onClick={onEdit} disabled={!showOptions}>
|
||||||
|
<PencilLine size={12} />
|
||||||
|
</StyledIconButtonBlack>
|
||||||
|
<StyledIconButtonRed onClick={handleDelete} disabled={!showOptions}>
|
||||||
|
<Trash2 size={12} />
|
||||||
|
</StyledIconButtonRed>
|
||||||
|
</WrappedIcons>
|
||||||
|
</WrappedLine>
|
||||||
|
<Divider />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const StyledIconButtonBlack = styled(IconButton)(({ theme }) => ({
|
||||||
|
color: theme.palette.common.black,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const StyledIconButtonRed = styled(IconButton)(({ theme }) => ({
|
||||||
|
color: theme.palette.error.main,
|
||||||
|
"&.Mui-disabled": {
|
||||||
|
opacity: 0.6,
|
||||||
|
color: theme.palette.error.light,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
const WrappedLine = styled(Box)({
|
||||||
|
display: "flex",
|
||||||
|
height: "28px",
|
||||||
|
alignItems: "center",
|
||||||
|
});
|
||||||
|
|
||||||
|
const StyledDiv = styled("div")(({ theme }) => ({
|
||||||
|
fontFamily: theme.typography.fontFamily,
|
||||||
|
fontSize: "12px",
|
||||||
|
fontWeight: "400",
|
||||||
|
color: theme.palette.common.black,
|
||||||
|
width: "171px",
|
||||||
|
}));
|
||||||
|
|
||||||
|
const WrappedIcons = styled(Box)({
|
||||||
|
display: "flex",
|
||||||
|
gap: "0px",
|
||||||
|
});
|
||||||
|
|
||||||
|
export default NamedRangeInactive;
|
||||||
@@ -59,12 +59,14 @@
|
|||||||
"num_fmt": {
|
"num_fmt": {
|
||||||
"title": "Custom number format",
|
"title": "Custom number format",
|
||||||
"label": "Number format",
|
"label": "Number format",
|
||||||
|
"close": "Close dialog",
|
||||||
"save": "Save"
|
"save": "Save"
|
||||||
},
|
},
|
||||||
"sheet_rename": {
|
"sheet_rename": {
|
||||||
"rename": "Save",
|
"rename": "Save",
|
||||||
"label": "New name",
|
"label": "New name",
|
||||||
"title": "Rename Sheet"
|
"title": "Rename Sheet",
|
||||||
|
"close": "Close dialog"
|
||||||
},
|
},
|
||||||
"formula_input": {
|
"formula_input": {
|
||||||
"update": "Update",
|
"update": "Update",
|
||||||
|
|||||||
Reference in New Issue
Block a user