From e250f5aab24799b61ae94233b2acf90cc812b409 Mon Sep 17 00:00:00 2001 From: twwu Date: Thu, 27 Mar 2025 14:39:25 +0800 Subject: [PATCH] feat: implement AutoWidthInput component for dynamic input sizing in visual editor --- .../visual-editor/card.tsx | 4 +- .../edit-card/auto-width-input.tsx | 81 +++++++++++++++++++ .../visual-editor/edit-card/index.tsx | 11 ++- web/app/dev-preview/page.tsx | 3 - 4 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/auto-width-input.tsx diff --git a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/card.tsx b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/card.tsx index bdfc2a53fc..4f53f6b163 100644 --- a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/card.tsx +++ b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/card.tsx @@ -18,8 +18,8 @@ const Card: FC = ({ return (
-
-
+
+
{name}
diff --git a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/auto-width-input.tsx b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/auto-width-input.tsx new file mode 100644 index 0000000000..af4a82c772 --- /dev/null +++ b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/auto-width-input.tsx @@ -0,0 +1,81 @@ +import React, { useEffect, useState } from 'react' +import type { FC } from 'react' +import cn from '@/utils/classnames' + +type AutoWidthInputProps = { + value: string + placeholder: string + onChange: (event: React.ChangeEvent) => void + onBlur: () => void + minWidth?: number + maxWidth?: number +} & Omit, 'onChange'> + +const AutoWidthInput: FC = ({ + value, + placeholder, + onChange, + onBlur, + minWidth = 60, + maxWidth = 300, + className, + ...props +}) => { + const [width, setWidth] = useState(minWidth) + const textRef = React.useRef(null) + + useEffect(() => { + if (textRef.current) { + textRef.current.textContent = value || placeholder + const textWidth = textRef.current.offsetWidth + const newWidth = Math.max(minWidth, Math.min(textWidth + 16, maxWidth)) + if (width !== newWidth) + setWidth(newWidth) + } + }, [value, placeholder, minWidth, maxWidth, width]) + + // Handle Enter key + const handleKeyUp = (e: React.KeyboardEvent) => { + if (e.key === 'Enter' && e.currentTarget.blur) + e.currentTarget.blur() + if (props.onKeyUp) + props.onKeyUp(e) + } + + return ( +
+ {/* Hidden measurement span */} + + + {/* Actual input element */} + +
+ ) +} + +export default React.memo(AutoWidthInput) diff --git a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/index.tsx b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/index.tsx index e162ca0c6c..7425facb79 100644 --- a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/index.tsx +++ b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/visual-editor/edit-card/index.tsx @@ -14,6 +14,7 @@ import { useVisualEditorStore } from '../store' import { useMittContext } from '../context' import { useUnmount } from 'ahooks' import { JSON_SCHEMA_MAX_DEPTH } from '@/config' +import AutoWidthInput from './auto-width-input' export type EditData = { name: string @@ -192,17 +193,15 @@ const EditCard: FC = ({ return (
-
+
- e.key === 'Enter' && e.currentTarget.blur()} />