refactor(qrcode): 优化布局逻辑,输入态隐藏预览区域

- 输入态:只显示输入框和生成按钮,隐藏二维码预览
- 预览态:上方显示文本预览(超长文本截断),下方显示二维码
- 点击编辑返回输入态,隐藏二维码预览
- 更新测试用例适配新布局
This commit is contained in:
雨霖铃
2026-05-30 20:29:12 +08:00
parent 6dca673a5a
commit 5b26287cff
2 changed files with 97 additions and 82 deletions
+64 -60
View File
@@ -7,6 +7,9 @@ import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { cn } from '@/lib/utils';
/** 文本预览的最大显示字符数 */
const TEXT_PREVIEW_MAX_LENGTH = 100;
export default function GeneratePanel() {
const { t } = useI18n('qrCode');
const {
@@ -21,60 +24,40 @@ export default function GeneratePanel() {
const isInputStep = generatorState.step === 'input';
const hasText = generatorState.textToEncode.trim().length > 0;
return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 w-full items-stretch select-none p-0.5">
{/* 左侧:输入态显示输入框,预览态显示文本预览 */}
<div
className={cn(
'border border-border rounded-xl bg-card text-card-foreground shadow-sm flex flex-col p-4',
isInputStep && 'focus-within:ring-1 focus-within:ring-ring focus-within:border-ring',
)}
>
<div className="flex flex-col space-y-2.5 h-full">
<div className="flex items-center justify-between">
/** 截断文本用于预览显示 */
const getTruncatedText = (text: string) => {
if (text.length <= TEXT_PREVIEW_MAX_LENGTH) return text;
return text.slice(0, TEXT_PREVIEW_MAX_LENGTH) + '...';
};
// 输入态:只显示输入框和生成按钮
if (isInputStep) {
return (
<div className="w-full select-none p-0.5">
<div
className={cn(
'border border-border rounded-xl bg-card text-card-foreground shadow-sm flex flex-col p-4',
'focus-within:ring-1 focus-within:ring-ring focus-within:border-ring',
)}
>
<div className="flex flex-col space-y-2.5">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5">
{isInputStep ? t('qrCode:urlInputLabel') : t('qrCode:textPreviewLabel')}
{t('qrCode:urlInputLabel')}
</Label>
{/* 预览态显示编辑按钮 */}
{!isInputStep && (
<Button variant="ghost" size="sm" onClick={backToEdit} className="h-7 px-2 text-xs">
<Pencil className="w-3 h-3 mr-1" />
{t('qrCode:editButton')}
</Button>
)}
</div>
<TextInputArea
value={generatorState.textToEncode}
onChange={setTextToEncode}
placeholder={t('qrCode:urlInputPlaceholder')}
showCount={true}
showClear={true}
allowCopy={false}
minRows={6}
maxRows={12}
externalError={generatorState.inputError || undefined}
onClear={() => setTextToEncode('')}
/>
<div className="flex-1 min-h-0">
{isInputStep ? (
/* 输入态:可编辑的输入框 */
<TextInputArea
value={generatorState.textToEncode}
onChange={setTextToEncode}
placeholder={t('qrCode:urlInputPlaceholder')}
showCount={true}
showClear={true}
allowCopy={false}
minRows={6}
maxRows={12}
externalError={generatorState.inputError || undefined}
onClear={() => setTextToEncode('')}
/>
) : (
/* 预览态:只读文本预览 */
<div
className="h-full min-h-[150px] p-3 rounded-md border border-input bg-muted/50 overflow-auto"
title={generatorState.savedText}
>
<p className="text-sm text-foreground whitespace-pre-wrap break-all">
{generatorState.savedText}
</p>
</div>
)}
</div>
{/* 输入态显示生成按钮 */}
{isInputStep && (
<Button
onClick={confirmGenerate}
disabled={!hasText || generatorState.generating}
@@ -92,19 +75,40 @@ export default function GeneratePanel() {
</>
)}
</Button>
)}
</div>
</div>
</div>
);
}
// 预览态:显示文本预览 + 二维码
return (
<div className="w-full select-none p-0.5 space-y-4">
{/* 文本预览区域 */}
<div className="border border-border rounded-xl bg-card text-card-foreground shadow-sm p-4">
<div className="flex items-center justify-between mb-2">
<Label className="text-[10px] font-bold text-muted-foreground/90 uppercase tracking-wider select-none pl-0.5">
{t('qrCode:textPreviewLabel')}
</Label>
<Button variant="ghost" size="sm" onClick={backToEdit} className="h-7 px-2 text-xs">
<Pencil className="w-3 h-3 mr-1" />
{t('qrCode:editButton')}
</Button>
</div>
<div
className="text-sm text-foreground bg-muted/50 rounded-md p-3 cursor-default"
title={generatorState.savedText}
>
{getTruncatedText(generatorState.savedText)}
</div>
</div>
{/* 右侧:二维码预览区域 */}
<div className="flex flex-col h-full">
<QrCodePreview
qrCodeDataUrl={generatorState.qrCodeDataUrl}
onDownload={downloadQrCode}
onCopy={copyQrCode}
placeholderText={isInputStep ? t('qrCode:generateFirstHint') : undefined}
/>
</div>
{/* 二维码预览区域 */}
<QrCodePreview
qrCodeDataUrl={generatorState.qrCodeDataUrl}
onDownload={downloadQrCode}
onCopy={copyQrCode}
/>
</div>
);
}