Files
AFFiNE-Mirror/packages/frontend/component/src/components/rename-modal/index.tsx
T
JimmFly 675a010dfc fix(core): prevent floating sidebar from disappearing unexpectedly (#8477)
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
2024-10-19 13:24:08 +00:00

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>
);
};