mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-27 10:21:50 +08:00
feat: align list editors with name hover actions
This commit is contained in:
1 parent
bdf0e68644
commit
de7a668b32
7 files changed
+188
-77
No files matched your search
@@ -2,27 +2,43 @@
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import { PencilIcon } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const MAX_REMARK_CHARS = 2000;
|
||||
type EditorProps = {
|
||||
value?: string;
|
||||
label: string;
|
||||
onSave: (value: string) => Promise<void>;
|
||||
};
|
||||
|
||||
export function RemarkEditor({
|
||||
export function RemarkEditor(props: EditorProps) {
|
||||
return <InlineTextEditor {...props} multiline fieldName="备注" maxCharacters={2000} emptyLabel="添加备注" />;
|
||||
}
|
||||
|
||||
export function ScheduleNameEditor(props: EditorProps) {
|
||||
return <InlineTextEditor {...props} multiline={false} fieldName="名称" maxCharacters={200} emptyLabel="未命名" />;
|
||||
}
|
||||
|
||||
function InlineTextEditor({
|
||||
value = "",
|
||||
label,
|
||||
onSave,
|
||||
}: {
|
||||
value?: string;
|
||||
label: string;
|
||||
onSave: (remark: string) => Promise<void>;
|
||||
}) {
|
||||
multiline,
|
||||
fieldName,
|
||||
maxCharacters,
|
||||
emptyLabel,
|
||||
}: EditorProps & { multiline: boolean; fieldName: string; maxCharacters: number; emptyLabel: string }) {
|
||||
const [editing, setEditing] = React.useState(false);
|
||||
const [draft, setDraft] = React.useState(value);
|
||||
const [saving, setSaving] = React.useState(false);
|
||||
const [error, setError] = React.useState("");
|
||||
const inputRef = React.useRef<HTMLTextAreaElement>(null);
|
||||
const inputRef = React.useRef<HTMLInputElement>(null);
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
|
||||
const cancelOnBlurRef = React.useRef(false);
|
||||
const savingRef = React.useRef(false);
|
||||
const errorID = React.useId();
|
||||
@@ -31,6 +47,10 @@ export function RemarkEditor({
|
||||
if (!editing) setDraft(value);
|
||||
}, [editing, value]);
|
||||
|
||||
function refocus() {
|
||||
requestAnimationFrame(() => (multiline ? textareaRef.current : inputRef.current)?.focus());
|
||||
}
|
||||
|
||||
async function finishEditing() {
|
||||
if (savingRef.current) return;
|
||||
if (cancelOnBlurRef.current) {
|
||||
@@ -45,20 +65,25 @@ export function RemarkEditor({
|
||||
setEditing(false);
|
||||
return;
|
||||
}
|
||||
if (Array.from(next).length > MAX_REMARK_CHARS) {
|
||||
setError(`备注最多 ${MAX_REMARK_CHARS} 个字符`);
|
||||
requestAnimationFrame(() => inputRef.current?.focus());
|
||||
if (!multiline && next.length === 0) {
|
||||
setError("名称不能为空");
|
||||
refocus();
|
||||
return;
|
||||
}
|
||||
if (Array.from(next).length > maxCharacters) {
|
||||
setError(`${fieldName}最多 ${maxCharacters} 个字符`);
|
||||
refocus();
|
||||
return;
|
||||
}
|
||||
savingRef.current = true;
|
||||
setSaving(true);
|
||||
try {
|
||||
await onSave(next);
|
||||
toast.success("备注已保存");
|
||||
toast.success(`${fieldName}已保存`);
|
||||
setEditing(false);
|
||||
} catch (cause) {
|
||||
setError(`备注保存失败:${(cause as Error).message}`);
|
||||
requestAnimationFrame(() => inputRef.current?.focus());
|
||||
setError(`${fieldName}保存失败:${(cause as Error).message}`);
|
||||
refocus();
|
||||
} finally {
|
||||
savingRef.current = false;
|
||||
setSaving(false);
|
||||
@@ -66,38 +91,45 @@ export function RemarkEditor({
|
||||
}
|
||||
|
||||
if (editing) {
|
||||
const inputProps = {
|
||||
value: draft,
|
||||
autoFocus: true,
|
||||
disabled: saving,
|
||||
"aria-busy": saving,
|
||||
"aria-label": `${label}${fieldName}`,
|
||||
"aria-invalid": error.length > 0,
|
||||
"aria-describedby": error ? errorID : undefined,
|
||||
title: multiline ? "Enter 保存,Shift+Enter 换行,Esc 取消;留空可清除备注" : "Enter 或失焦保存,Esc 取消",
|
||||
onFocus: (event: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => event.currentTarget.select(),
|
||||
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
||||
setDraft(event.target.value);
|
||||
setError("");
|
||||
},
|
||||
onBlur: () => void finishEditing(),
|
||||
onKeyDown: (event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
||||
if (event.nativeEvent.isComposing) return;
|
||||
if (event.key === "Enter" && (!multiline || !event.shiftKey)) {
|
||||
event.preventDefault();
|
||||
event.currentTarget.blur();
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancelOnBlurRef.current = true;
|
||||
event.currentTarget.blur();
|
||||
}
|
||||
},
|
||||
};
|
||||
return (
|
||||
<div className="flex w-32 min-w-28 flex-col gap-1 sm:w-48">
|
||||
<Textarea
|
||||
ref={inputRef}
|
||||
value={draft}
|
||||
rows={1}
|
||||
autoFocus
|
||||
disabled={saving}
|
||||
aria-busy={saving}
|
||||
aria-label={`${label}备注`}
|
||||
aria-invalid={error.length > 0}
|
||||
aria-describedby={error ? errorID : undefined}
|
||||
className="max-h-40 min-h-7 resize-none px-2 py-1 [overflow-wrap:anywhere]"
|
||||
title="Enter 保存,Shift+Enter 换行,Esc 取消;留空可清除备注"
|
||||
onFocus={(event) => event.currentTarget.select()}
|
||||
onChange={(event) => {
|
||||
setDraft(event.target.value);
|
||||
setError("");
|
||||
}}
|
||||
onBlur={() => void finishEditing()}
|
||||
onKeyDown={(event) => {
|
||||
if (event.nativeEvent.isComposing) return;
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
event.currentTarget.blur();
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancelOnBlurRef.current = true;
|
||||
event.currentTarget.blur();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className={cn("flex min-w-0 flex-col gap-1 font-medium", multiline && "w-32 min-w-28 sm:w-48")}>
|
||||
{multiline ? (
|
||||
<Textarea
|
||||
{...inputProps}
|
||||
ref={textareaRef}
|
||||
rows={1}
|
||||
className="max-h-40 min-h-7 resize-none px-2 py-1 [overflow-wrap:anywhere]"
|
||||
/>
|
||||
) : (
|
||||
<Input {...inputProps} ref={inputRef} className="h-7 min-w-28 max-w-48 px-2 font-medium" />
|
||||
)}
|
||||
{error ? (
|
||||
<p id={errorID} role="alert" className="whitespace-normal text-destructive text-xs">
|
||||
{error}
|
||||
@@ -107,25 +139,42 @@ export function RemarkEditor({
|
||||
);
|
||||
}
|
||||
|
||||
const startEditing = () => {
|
||||
setDraft(value);
|
||||
setError("");
|
||||
setEditing(true);
|
||||
};
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="link"
|
||||
size="sm"
|
||||
className="h-7 w-32 justify-start p-0 sm:w-48"
|
||||
aria-label={`编辑${label}的备注`}
|
||||
title={value.length > 0 ? value : "点击编辑备注"}
|
||||
onClick={() => {
|
||||
setDraft(value);
|
||||
setError("");
|
||||
setEditing(true);
|
||||
}}
|
||||
>
|
||||
{value.length > 0 ? (
|
||||
<span className="truncate">{value}</span>
|
||||
) : (
|
||||
<span className="text-muted-foreground">添加备注</span>
|
||||
<div
|
||||
className={cn(
|
||||
"group/inline-editor flex min-w-0 items-center gap-1 font-medium",
|
||||
multiline ? "w-32 sm:w-48" : "max-w-xs",
|
||||
)}
|
||||
</Button>
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
className="min-w-0 truncate rounded-sm text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
||||
aria-label={`${label}${fieldName},点击编辑`}
|
||||
title={value.length > 0 ? value : `点击编辑${fieldName}`}
|
||||
onClick={startEditing}
|
||||
>
|
||||
{value.length > 0 ? (
|
||||
<span className="block truncate">{value}</span>
|
||||
) : (
|
||||
<span className="text-muted-foreground">{emptyLabel}</span>
|
||||
)}
|
||||
</button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon-xs"
|
||||
className="shrink-0 opacity-100 transition-opacity [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-focus-within:opacity-100 [@media(hover:hover)]:group-hover:opacity-100 [@media(hover:hover)]:group-focus-within/inline-editor:opacity-100 [@media(hover:hover)]:group-hover/inline-editor:opacity-100"
|
||||
aria-label={`编辑${label}的${fieldName}`}
|
||||
title={`编辑${fieldName}`}
|
||||
onClick={startEditing}
|
||||
>
|
||||
<PencilIcon />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user