Skip to content

Commit

Permalink
LA-7 Center screen on text input when creating a new node
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucano Vera committed Dec 12, 2024
1 parent faef9cb commit 8b98018
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node, NodeProps } from "@xyflow/react";
import { Node, NodeProps, useReactFlow } from "@xyflow/react";
import { AntInput, InputRef } from "fidesui";
import { useEffect, useRef, useState } from "react";

Expand All @@ -13,26 +13,42 @@ export type TextInputNodeType = Node<
"textInputNode"
>;

const TaxonomyTextInputNode = ({ data }: NodeProps<TextInputNodeType>) => {
const TaxonomyTextInputNode = ({
data,
positionAbsoluteX,
positionAbsoluteY,
}: NodeProps<TextInputNodeType>) => {
const { onCancel, onSubmit, parentKey } = data;
const inputRef = useRef<InputRef>(null);
const [value, setValue] = useState("");
const inputWidth = 200;

// Reset state and autofocus when the node is mounted
const { setCenter, getZoom } = useReactFlow();

// Reset state and autofocus / center screen around the node when it is mounted
// or when the parent key changes (it's being added to somewhere else in the tree)
useEffect(() => {
setValue("");
const focusOnInput = () => {
const focusOnInput = () =>
inputRef.current!.focus({
cursor: "start",
preventScroll: true,
});
const centerScreenOnNode = () =>
setCenter(positionAbsoluteX + inputWidth / 2, positionAbsoluteY, {
duration: 500,
zoom: getZoom(),
});
const centerAndFocus = async () => {
await centerScreenOnNode();
focusOnInput();
};
setTimeout(focusOnInput, 200);
}, [parentKey]);

centerAndFocus();
}, [parentKey, getZoom, positionAbsoluteX, positionAbsoluteY, setCenter]);

return (
<div className=" w-[200px]">
<div style={{ width: inputWidth }}>
<AntInput
placeholder="Type label name..."
ref={inputRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const TaxonomyTreeNode = ({ data }: NodeProps<TaxonomyTreeNodeType>) => {

return (
<div
className="group relative"
className="group relative transition-transform"
onMouseEnter={() => onMouseEnter(taxonomyItem?.fides_key!)}
onMouseLeave={() => onMouseLeave(taxonomyItem?.fides_key!)}
data-testid={`taxonomy-node-${taxonomyItem?.fides_key}`}
Expand Down

0 comments on commit 8b98018

Please sign in to comment.