复制按钮文案改成状态映射表

xiaohongshu/CopyButton.tsx:用 COPY_STATE_TEXT 查表替掉嵌套三元。
This commit is contained in:
2026-10-06 21:06:08 +08:00
parent 62947a836f
commit 30c08272c6
@@ -4,6 +4,13 @@ import { useEffect, useState } from 'react';
type CopyState = 'idle' | 'copied' | 'failed';
/** 状态 → 按钮文案;加状态时只在这里补一条。 */
const COPY_STATE_TEXT: Record<CopyState, string> = {
idle: '复制',
copied: '已复制',
failed: '复制失败',
};
/**
* 复制按钮:成功后显示一小会儿「已复制」,失败也**必须**说一声。
*
@@ -21,8 +28,7 @@ export function CopyButton({ value, label }: { value: string; label: string }) {
return () => clearTimeout(timer);
}, [state]);
const text =
state === 'copied' ? '已复制' : state === 'failed' ? '复制失败' : '复制';
const text = COPY_STATE_TEXT[state];
return (
<button