Spaces:
Sleeping
Sleeping
File size: 5,829 Bytes
4c2a557 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | "use client";
import { useState, useEffect } from "react";
import { Input } from "antd";
import { Button } from "@/components/ui/button";
import { CheckOutlined, InfoCircleOutlined } from "@ant-design/icons";
import { Tooltip } from "antd";
import { toast } from "sonner";
interface EditableCellProps {
value: number;
isEditing: boolean;
onEdit: () => void;
onSubmit: (value: number) => Promise<void>;
onCancel: () => void;
t: (key: string, options?: { max?: number }) => string;
disabled?: boolean;
tooltipText?: string;
placeholder?: string;
validateValue?: (value: number) => {
isValid: boolean;
errorMessage?: string;
maxValue?: number;
};
isPerMsgPrice?: boolean;
}
export function EditableCell({
value,
isEditing,
onEdit,
onSubmit,
onCancel,
t,
disabled = false,
tooltipText,
placeholder,
validateValue = (value) => ({ isValid: true }),
isPerMsgPrice = false,
}: EditableCellProps) {
const numericValue = typeof value === "number" ? value : Number(value);
const originalValue = numericValue >= 0 ? numericValue.toFixed(4) : "";
const [inputValue, setInputValue] = useState(originalValue);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
if (isEditing) {
setInputValue(originalValue);
}
}, [isEditing, originalValue]);
useEffect(() => {
if (isEditing) {
const handleClickOutside = (e: MouseEvent) => {
const target = e.target as HTMLElement;
if (!target.closest(".editable-cell-input")) {
onCancel();
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}
}, [isEditing, onCancel]);
const handleSubmit = async () => {
try {
setIsSaving(true);
const numValue = Number(inputValue);
const validation = validateValue(numValue);
if (!validation.isValid) {
toast.error(validation.errorMessage || t("error.invalidInput"));
return;
}
if (validation.maxValue !== undefined && numValue > validation.maxValue) {
toast.error(t("error.exceedsMaxValue", { max: validation.maxValue }));
return;
}
await onSubmit(numValue);
} catch (err) {
} finally {
setIsSaving(false);
}
};
return (
<div className={`relative ${disabled ? "opacity-50" : ""}`}>
{isEditing ? (
<div className="relative editable-cell-input flex items-center gap-1.5">
<Input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
className="
!w-[calc(100%-32px)]
!border
!border-slate-200
focus:!border-slate-300
!bg-white
!shadow-sm
hover:!shadow
focus:!shadow-md
!px-2
!py-1
!h-7
flex-1
!rounded-lg
!text-slate-600
!text-sm
!font-medium
placeholder:!text-slate-400/70
transition-all
duration-200
focus:!ring-2
focus:!ring-slate-200/50
focus:!ring-offset-0
"
placeholder={placeholder || t("common.enterValue")}
onPressEnter={handleSubmit}
autoFocus
disabled={isSaving}
/>
<Button
size="sm"
variant="ghost"
className={`
h-7 w-7
flex-shrink-0
bg-gradient-to-r from-slate-500/80 to-slate-600/80
hover:from-slate-600 hover:to-slate-700
text-white/90
shadow-sm
rounded-lg
transition-all
duration-200
hover:scale-105
active:scale-95
p-0
flex
items-center
justify-center
${isSaving ? "cursor-not-allowed opacity-70" : ""}
`}
onClick={(e) => {
e.stopPropagation();
handleSubmit();
}}
disabled={isSaving}
>
{isSaving ? (
<div className="w-3 h-3 rounded-full border-2 border-white/90 border-t-transparent animate-spin" />
) : (
<CheckOutlined className="text-xs" />
)}
</Button>
</div>
) : (
<div
onClick={disabled ? undefined : onEdit}
className={`
group
px-2
py-1
rounded-lg
transition-colors
duration-200
${
disabled
? "cursor-not-allowed line-through"
: "cursor-pointer hover:bg-primary/5"
}
`}
>
<span
className={`
font-medium
text-sm
transition-colors
duration-200
${
disabled
? "text-muted-foreground/60"
: "text-primary/80 group-hover:text-primary"
}
`}
>
{isPerMsgPrice && numericValue < 0 ? (
<span className="text-muted-foreground/60">
{t("common.notSet")}
</span>
) : (
<>
{numericValue.toFixed(4)}
{tooltipText && (
<Tooltip title={tooltipText}>
<InfoCircleOutlined className="ml-1 text-muted-foreground/60" />
</Tooltip>
)}
</>
)}
</span>
</div>
)}
</div>
);
}
|