mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-01 17:39:55 +08:00
675a010dfc
close AF-1475 Fixed an issue where you couldn't create collection or use the rename function in the floating sidebar. https://github.com/user-attachments/assets/41c2b6a8-8fc9-4f8b-ab51-bd7ce2a58738
73 lines
1.7 KiB
TypeScript
73 lines
1.7 KiB
TypeScript
import type { KeyboardEvent, ReactElement } from 'react';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
import Input from '../../ui/input';
|
|
import { Menu } from '../../ui/menu';
|
|
|
|
export const RenameModal = ({
|
|
onRename,
|
|
currentName,
|
|
open,
|
|
width = 220,
|
|
children,
|
|
onOpenChange,
|
|
}: {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
onRename: (newName: string) => void;
|
|
currentName: string;
|
|
width?: string | number;
|
|
children?: ReactElement;
|
|
}) => {
|
|
const [value, setValue] = useState(currentName);
|
|
|
|
const handleRename = useCallback(() => {
|
|
onRename(value);
|
|
onOpenChange(false);
|
|
}, [onOpenChange, onRename, value]);
|
|
|
|
const onKeyDown = useCallback(
|
|
(e: KeyboardEvent<HTMLInputElement>) => {
|
|
if (e.key !== 'Escape') return;
|
|
if (currentName !== value) setValue(currentName);
|
|
onOpenChange(false);
|
|
},
|
|
[currentName, value, onOpenChange]
|
|
);
|
|
|
|
// Synchronize when the title is changed in the page header or title.
|
|
useEffect(() => setValue(currentName), [currentName]);
|
|
|
|
return (
|
|
<Menu
|
|
rootOptions={{
|
|
modal: true,
|
|
open: open,
|
|
onOpenChange: onOpenChange,
|
|
}}
|
|
contentOptions={{
|
|
side: 'left',
|
|
onPointerDownOutside: handleRename,
|
|
sideOffset: -12,
|
|
onClick: e => e.stopPropagation(),
|
|
style: { borderRadius: 10, padding: 8 },
|
|
role: 'rename-modal',
|
|
}}
|
|
items={
|
|
<Input
|
|
autoFocus
|
|
autoSelect
|
|
value={value}
|
|
onChange={setValue}
|
|
onEnter={handleRename}
|
|
onKeyDown={onKeyDown}
|
|
data-testid="rename-modal-input"
|
|
style={{ width, height: 34, borderRadius: 4 }}
|
|
/>
|
|
}
|
|
>
|
|
{children ?? <div />}
|
|
</Menu>
|
|
);
|
|
};
|