From 83a2742a19cef9fdf6a9d75327b1c674e1ddc303 Mon Sep 17 00:00:00 2001 From: UnderKoen Date: Tue, 17 Dec 2024 22:29:25 +0100 Subject: [PATCH] style: change account name field an input --- .../src/components/common/Link.tsx | 7 ++ .../src/components/sidebar/Account.tsx | 72 ++++++++----------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/packages/desktop-client/src/components/common/Link.tsx b/packages/desktop-client/src/components/common/Link.tsx index 5ecd098cb75..1c89a9c868d 100644 --- a/packages/desktop-client/src/components/common/Link.tsx +++ b/packages/desktop-client/src/components/common/Link.tsx @@ -35,6 +35,7 @@ type InternalLinkProps = { activeStyle?: CSSProperties; children?: ReactNode; report?: CustomReportEntity; + isDisabled?: boolean; }; const externalLinkColors = { @@ -121,6 +122,7 @@ const InternalLink = ({ activeStyle, children, report, + isDisabled, }: InternalLinkProps) => { const path = to ?? ''; const match = useMatch({ path }); @@ -130,6 +132,11 @@ const InternalLink = ({ to={path} state={report ? { report } : {}} className={css([styles.smallText, style, match ? activeStyle : null])} + onClick={e => { + if (isDisabled) { + e.preventDefault(); + } + }} > {children} diff --git a/packages/desktop-client/src/components/sidebar/Account.tsx b/packages/desktop-client/src/components/sidebar/Account.tsx index a913b497934..a3b25cc9bc9 100644 --- a/packages/desktop-client/src/components/sidebar/Account.tsx +++ b/packages/desktop-client/src/components/sidebar/Account.tsx @@ -16,6 +16,8 @@ import { useContextMenu } from '../../hooks/useContextMenu'; import { useNotes } from '../../hooks/useNotes'; import { styles, theme } from '../../style'; import { AlignedText } from '../common/AlignedText'; +import { InitialFocus } from '../common/InitialFocus'; +import { Input } from '../common/Input'; import { Link } from '../common/Link'; import { Menu } from '../common/Menu'; import { Popover } from '../common/Popover'; @@ -102,32 +104,7 @@ export function Account>({ const dispatch = useDispatch(); - const editingRef = useRef(null); const [isEditing, setIsEditing] = useState(false); - useEffect(() => { - if (!editingRef.current) return; - if (isEditing) { - editingRef.current.focus(); - window.getSelection().selectAllChildren(editingRef.current); - } else { - editingRef.current.textContent = name; - } - }, [name, isEditing]); - - const updateName = () => { - if (account && isEditing) { - setIsEditing(false); - const newName = editingRef.current.textContent; - if (newName !== account.name && newName.trim()) { - dispatch( - updateAccount({ - ...account, - name: newName, - }), - ); - } - } - }; const accountNote = useNotes(`account-${account?.id}`); const needsTooltip = !!account?.id; @@ -144,6 +121,7 @@ export function Account>({ >({ } } left={ - { - if (e.key === 'Enter') { - e.preventDefault(); - updateName(); - } else if (e.key === 'Escape') { - setIsEditing(false); - } - }} - > - {name} - + isEditing ? ( + + setIsEditing(false)} + onEnter={e => { + const inputEl = e.target as HTMLInputElement; + const newAccountName = inputEl.value; + if (newAccountName.trim() !== '') { + dispatch( + updateAccount({ + ...account, + name: newAccountName, + }), + ); + } + setIsEditing(false); + }} + onEscape={() => setIsEditing(false)} + defaultValue={name} + /> + + ) : ( + name + ) } right={} />