From e323e104e03569b7131dc1aac1e15c548ebe8485 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 16 Sep 2024 21:50:22 +0800 Subject: [PATCH] refactor: add new editable text component which allow to change group title --- src/client/components/EditableText.tsx | 34 ++++++---- .../monitor/StatusPage/ServiceList.tsx | 68 ++++++++++++------- src/client/routes/playground.tsx | 6 ++ 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/client/components/EditableText.tsx b/src/client/components/EditableText.tsx index 7fe3b9d..816c7d3 100644 --- a/src/client/components/EditableText.tsx +++ b/src/client/components/EditableText.tsx @@ -1,38 +1,46 @@ -import { Input } from 'antd'; -import clsx from 'clsx'; import React, { useState } from 'react'; import { useWatch } from '../hooks/useWatch'; +import { Input } from './ui/input'; +import { useEvent } from '@/hooks/useEvent'; +import { cn } from '@/utils/style'; interface EditableTextProps { className?: string; - enable?: boolean; defaultValue: string; onSave: (text: string) => void; } export const EditableText: React.FC = React.memo((props) => { const [text, setText] = useState(props.defaultValue); - const enable = props.enable ?? true; + const [editing, setEditing] = useState(false); + const inputRef = React.useRef(null); useWatch([props.defaultValue], () => { setText(props.defaultValue); }); + const handleClick = useEvent(() => { + setEditing(true); + }); + return ( - <> - {enable ? ( +
+ {editing ? ( setText(e.target.value)} - onBlur={(e) => props.onSave(e.target.value)} + onBlur={() => { + setEditing(false); + props.onSave(text); + }} /> ) : ( - {text} + {text} )} - +
); }); EditableText.displayName = 'EditableText'; diff --git a/src/client/components/monitor/StatusPage/ServiceList.tsx b/src/client/components/monitor/StatusPage/ServiceList.tsx index 4ab4eb3..6aa6c12 100644 --- a/src/client/components/monitor/StatusPage/ServiceList.tsx +++ b/src/client/components/monitor/StatusPage/ServiceList.tsx @@ -12,6 +12,7 @@ import { Separator } from '@/components/ui/separator'; import { MonitorPicker } from '../MonitorPicker'; import { Switch } from '@/components/ui/switch'; import { set } from 'lodash-es'; +import { EditableText } from '@/components/EditableText'; export const leafItemSchema = z.object({ key: z.string(), @@ -90,6 +91,24 @@ export const MonitorStatusPageServiceList: React.FC { + const index = props.value.findIndex((item) => item.key === groupKey); + if (index === -1) { + return; + } + + const newList = [...props.value]; + if (!('children' in newList[index])) { + return; + } + + newList[index].title = title; + + props.onChange(newList); + } + ); + const handleDeleteItem = useEvent((groupKey: string, itemKey: string) => { const newList = [...props.value]; const groupIndex = newList.findIndex((item) => item.key === groupKey); @@ -145,31 +164,34 @@ export const MonitorStatusPageServiceList: React.FC (
-
- {group.title} + {level > 0 && ( +
+ handleChangeGroupTitle(group.key, text)} + /> - {level > 0 && ( - <> -
+ )} -
0 && 'border-l-4 border-gray-600 p-2')} > diff --git a/src/client/routes/playground.tsx b/src/client/routes/playground.tsx index 6edf629..86470a9 100644 --- a/src/client/routes/playground.tsx +++ b/src/client/routes/playground.tsx @@ -5,6 +5,7 @@ import { MonitorStatusPageServiceList, } from '@/components/monitor/StatusPage/ServiceList'; import { useState } from 'react'; +import { EditableText } from '@/components/EditableText'; export const Route = createFileRoute('/playground')({ beforeLoad: () => { @@ -45,6 +46,11 @@ function PageComponent() { return (
+ console.log('save')} + /> +
);