Edit relationship on double click (#68)

This commit is contained in:
1ilit 2024-05-08 04:03:04 +03:00
parent d0572ee55a
commit fc904fb740
3 changed files with 58 additions and 14 deletions

View File

@ -1,11 +1,13 @@
import { useRef } from "react"; import { useRef } from "react";
import { Cardinality } from "../../data/constants"; import { Cardinality, ObjectType, Tab } from "../../data/constants";
import { calcPath } from "../../utils/calcPath"; import { calcPath } from "../../utils/calcPath";
import { useTables, useSettings } from "../../hooks"; import { useTables, useSettings, useLayout, useSelect } from "../../hooks";
export default function Relationship({ data }) { export default function Relationship({ data }) {
const { settings } = useSettings(); const { settings } = useSettings();
const { tables } = useTables(); const { tables } = useTables();
const { layout } = useLayout();
const { selectedElement, setSelectedElement } = useSelect();
const pathRef = useRef(); const pathRef = useRef();
let cardinalityStart = "1"; let cardinalityStart = "1";
@ -47,8 +49,31 @@ export default function Relationship({ data }) {
cardinalityEndY = point2.y; cardinalityEndY = point2.y;
} }
const edit = () => {
if (!layout.sidebar) {
setSelectedElement((prev) => ({
...prev,
element: ObjectType.RELATIONSHIP,
id: data.id,
open: true,
}));
} else {
setSelectedElement((prev) => ({
...prev,
currentTab: Tab.RELATIONSHIPS,
element: ObjectType.RELATIONSHIP,
id: data.id,
open: true,
}));
if (selectedElement.currentTab !== Tab.RELATIONSHIPS) return;
document
.getElementById(`scroll_ref_${data.id}`)
.scrollIntoView({ behavior: "smooth" });
}
};
return ( return (
<g className="select-none group"> <g className="select-none group" onDoubleClick={edit}>
<path <path
ref={pathRef} ref={pathRef}
d={calcPath( d={calcPath(

View File

@ -1,20 +1,32 @@
import { useState } from "react";
import { Collapse } from "@douyinfe/semi-ui"; import { Collapse } from "@douyinfe/semi-ui";
import { useTables } from "../../../hooks"; import { useSelect, useTables } from "../../../hooks";
import Empty from "../Empty"; import Empty from "../Empty";
import SearchBar from "./SearchBar"; import SearchBar from "./SearchBar";
import RelationshipInfo from "./RelationshipInfo"; import RelationshipInfo from "./RelationshipInfo";
import { ObjectType } from "../../../data/constants";
export default function RelationshipsTab() { export default function RelationshipsTab() {
const { relationships } = useTables(); const { relationships } = useTables();
const [refActiveIndex, setRefActiveIndex] = useState(""); const { selectedElement, setSelectedElement } = useSelect();
return ( return (
<> <>
<SearchBar setRefActiveIndex={setRefActiveIndex} /> <SearchBar />
<Collapse <Collapse
activeKey={refActiveIndex} activeKey={
onChange={(k) => setRefActiveIndex(k)} selectedElement.open &&
selectedElement.element === ObjectType.RELATIONSHIP
? `${selectedElement.id}`
: ""
}
onChange={(k) =>
setSelectedElement((prev) => ({
...prev,
open: true,
id: parseInt(k),
element: ObjectType.RELATIONSHIP,
}))
}
accordion accordion
> >
{relationships.length <= 0 ? ( {relationships.length <= 0 ? (

View File

@ -1,18 +1,20 @@
import { useState } from "react"; import { useState } from "react";
import { useTables } from "../../../hooks"; import { useSelect, useTables } from "../../../hooks";
import { AutoComplete } from "@douyinfe/semi-ui"; import { AutoComplete } from "@douyinfe/semi-ui";
import { IconSearch } from "@douyinfe/semi-icons"; import { IconSearch } from "@douyinfe/semi-icons";
import { ObjectType } from "../../../data/constants";
export default function SearchBar({ setRefActiveIndex }) { export default function SearchBar() {
const { relationships } = useTables(); const { relationships } = useTables();
const [searchText, setSearchText] = useState(""); const [searchText, setSearchText] = useState("");
const { setSelectedElement } = useSelect();
const [filteredResult, setFilteredResult] = useState( const [filteredResult, setFilteredResult] = useState(
relationships.map((t) => t.name) relationships.map((t) => t.name),
); );
const handleStringSearch = (value) => { const handleStringSearch = (value) => {
setFilteredResult( setFilteredResult(
relationships.map((t) => t.name).filter((i) => i.includes(value)) relationships.map((t) => t.name).filter((i) => i.includes(value)),
); );
}; };
@ -30,7 +32,12 @@ export default function SearchBar({ setRefActiveIndex }) {
onChange={(v) => setSearchText(v)} onChange={(v) => setSearchText(v)}
onSelect={(v) => { onSelect={(v) => {
const { id } = relationships.find((t) => t.name === v); const { id } = relationships.find((t) => t.name === v);
setRefActiveIndex(`${id}`); setSelectedElement((prev) => ({
...prev,
id: id,
open: true,
element: ObjectType.RELATIONSHIP,
}));
document document
.getElementById(`scroll_ref_${id}`) .getElementById(`scroll_ref_${id}`)
.scrollIntoView({ behavior: "smooth" }); .scrollIntoView({ behavior: "smooth" });