Files
liandong-crm/src/app/(dashboard)/customers/customer-form.tsx
T

169 lines
5.1 KiB
TypeScript
Raw Normal View History

"use client";
import { useActionState } from "react";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { Input, Textarea, Select } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { fmtDate } from "@/lib/utils";
import type { ActionState } from "./actions";
type CustomerValues = {
customerName?: string | null;
contacts?: string | null;
balanceUser?: string | null;
phone?: string | null;
detail?: string | null;
deposit?: string | null;
buy?: string | null;
birthdayType?: number | null;
birthdayOwner?: number | null;
birthday?: Date | null;
salesman?: string | null;
reportDate?: Date | null;
remark?: string | null;
};
function Field({
label,
name,
defaultValue,
required,
type = "text",
pattern,
inputMode,
title,
}: {
label: string;
name: string;
defaultValue?: string | null;
required?: boolean;
type?: string;
pattern?: string;
inputMode?: React.HTMLAttributes<HTMLInputElement>["inputMode"];
title?: string;
}) {
return (
<div className="space-y-1.5">
<Label htmlFor={name}>
{label}
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
</Label>
<Input
id={name}
name={name}
type={type}
defaultValue={defaultValue ?? ""}
required={required}
pattern={pattern}
inputMode={inputMode}
title={title}
/>
</div>
);
}
export function CustomerForm({
action,
defaultValues,
submitLabel = "保存",
requireAll = false,
}: {
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
defaultValues?: CustomerValues;
submitLabel?: string;
requireAll?: boolean;
}) {
const [state, formAction, pending] = useActionState(action, {});
const v = defaultValues ?? {};
return (
<form action={formAction} className="space-y-6">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
<Field
label="商户名称"
name="customerName"
defaultValue={v.customerName}
required
pattern={requireAll ? "\\S+" : undefined}
title={requireAll ? "商户名称不能包含空格" : undefined}
/>
<Field label="联系人" name="contacts" defaultValue={v.contacts} required />
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
<Field label="业务员" name="salesman" defaultValue={v.salesman} required />
<Field
label="联系电话"
name="phone"
defaultValue={v.phone}
required={requireAll}
pattern={requireAll ? "1\\d{10}" : undefined}
inputMode={requireAll ? "numeric" : undefined}
title={requireAll ? "请输入 11 位手机号" : undefined}
/>
<Field
label="报单日期"
name="reportDate"
type="date"
defaultValue={fmtDate(v.reportDate)}
required
/>
<Field label="押金" name="deposit" defaultValue={v.deposit} required={requireAll} />
<Field label="买断" name="buy" defaultValue={v.buy} required={requireAll} />
{/* 生日类型 + 生日归属,同行两个下拉框 */}
<div className="grid grid-cols-2 gap-3">
<div className="space-y-1.5">
<Label htmlFor="birthdayType">生日类型</Label>
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
<option value="1">阳历</option>
<option value="2">阴历</option>
</Select>
</div>
<div className="space-y-1.5">
<Label htmlFor="birthdayOwner">生日归属</Label>
<Select id="birthdayOwner" name="birthdayOwner" defaultValue={String(v.birthdayOwner ?? 2)}>
<option value="1">结算人</option>
<option value="2">联系人</option>
</Select>
</div>
</div>
<Field
label="生日"
name="birthday"
type="date"
defaultValue={fmtDate(v.birthday)}
required={requireAll}
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="detail">
产品明细<span className="ml-0.5 text-[var(--color-danger)]">*</span>
</Label>
<Textarea id="detail" name="detail" defaultValue={v.detail ?? ""} rows={3} />
</div>
<div className="space-y-1.5">
<Label htmlFor="remark">备注</Label>
<Textarea id="remark" name="remark" defaultValue={v.remark ?? ""} rows={2} />
</div>
{state.error && (
<p className="rounded-md bg-[var(--color-danger-soft)] px-3 py-2 text-sm text-[var(--color-danger)]">
{state.error}
</p>
)}
<div className="flex gap-2 border-t pt-4">
<Button type="submit" disabled={pending}>
{pending ? "保存中…" : submitLabel}
</Button>
<Link href="/customers">
<Button type="button" variant="outline">
取消
</Button>
</Link>
</div>
</form>
);
}