diff --git a/src/components/Dialogs/AddLinkTdDialog.tsx b/src/components/Dialogs/AddLinkTdDialog.tsx index 3595549d..3bb104ad 100644 --- a/src/components/Dialogs/AddLinkTdDialog.tsx +++ b/src/components/Dialogs/AddLinkTdDialog.tsx @@ -15,6 +15,7 @@ import React, { useCallback, useContext, useImperativeHandle, + useState, } from "react"; import ReactDOM from "react-dom"; import ediTDorContext from "../../context/ediTDorContext"; @@ -22,6 +23,7 @@ import * as fileTdService from "../../services/fileTdService"; import { checkIfLinkIsInItem } from "../../utils/tdOperations"; import DialogTemplate from "./DialogTemplate"; import BaseButton from "../../components/TDViewer/base/BaseButton"; +import DatalistInput from "../../components/base/DatalistInput"; export interface AddLinkTdDialogRef { openModal: () => void; @@ -41,12 +43,14 @@ interface AddLinkTdDialogProps { const AddLinkTdDialog = forwardRef( (props, ref) => { const context = useContext(ediTDorContext); - const [display, setDisplay] = React.useState(() => { - return false; - }); - const [linkingMethod, setlinkingMethod] = React.useState(() => { - return "url"; + const [formData, setFormData] = useState({ + href: "", + rel: "", + type: "", }); + const [errorMessage, setErrorMessage] = useState(""); + const [display, setDisplay] = useState(false); + const [linkingMethod, setLinkingMethod] = useState("url"); const [currentLinkedTd, setCurrentLinkedTd] = React.useState< Record >(() => { @@ -80,9 +84,8 @@ const AddLinkTdDialog = forwardRef( }; const addLinksToTd = (link: Link): void => { - // clone instead of mutating original TD const updatedTd = structuredClone(context.parsedTD); - // initialize links array if missing + if (!Array.isArray(updatedTd.links)) { updatedTd.links = []; } @@ -91,7 +94,7 @@ const AddLinkTdDialog = forwardRef( }; const linkingMethodChange = (linkingOption: string): void => { - setlinkingMethod(linkingOption); + setLinkingMethod(linkingOption); if (currentLinkedTd && linkingOption === "url") { setCurrentLinkedTd({}); } @@ -101,8 +104,11 @@ const AddLinkTdDialog = forwardRef( try { const res = await fileTdService.readFromFile(); - (document.getElementById("link-href") as HTMLInputElement).value = - `./${res.fileName}`; + setFormData((prev) => ({ + ...prev, + href: `./${res.fileName}`, + })); + setErrorMessage(""); setCurrentLinkedTd( res.fileHandle ? res.fileHandle : JSON.parse(res.td) ); @@ -113,25 +119,30 @@ const AddLinkTdDialog = forwardRef( } }, []); - const RelationType = (): JSX.Element[] => { - const relations = [ - "icon", - "service-doc", - "alternate", - "type", - "tm:extends", - "proxy-to", - "collection", - "item", - "predecessor-version", - "controlledBy", - ]; - let index = 0; - const relationsHtml = relations.map((currentRelation) => { - index++; - return