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

218 lines
6.7 KiB
TypeScript

"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;
contactsPhone?: string | null;
balanceUser?: string | null;
balanceUserPhone?: 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>
);
}
// 表单分区:小标题 + 内容;块间靠外层 space-y-6 分隔,不加灰线(录入场景保持紧凑)
function FormSection({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
<section className="space-y-3">
<h4 className="text-xs font-semibold tracking-wide text-[var(--color-muted-foreground)]">
{title}
</h4>
{children}
</section>
);
}
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">
<FormSection title="基本">
<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="salesman" defaultValue={v.salesman} required />
<Field
label="报单日期"
name="reportDate"
type="date"
defaultValue={fmtDate(v.reportDate)}
required
/>
</div>
</FormSection>
<FormSection title="联系人信息">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
<Field label="姓名" name="contacts" defaultValue={v.contacts} required />
<Field
label="手机号"
name="contactsPhone"
defaultValue={v.contactsPhone}
required={requireAll}
pattern={requireAll ? "1\\d{10}" : undefined}
inputMode={requireAll ? "numeric" : undefined}
title={requireAll ? "请输入 11 位手机号" : undefined}
/>
</div>
</FormSection>
<FormSection title="结算人信息">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
<Field label="姓名" name="balanceUser" defaultValue={v.balanceUser} required />
<Field
label="手机号"
name="balanceUserPhone"
defaultValue={v.balanceUserPhone}
required={requireAll}
pattern={requireAll ? "1\\d{10}" : undefined}
inputMode={requireAll ? "numeric" : undefined}
title={requireAll ? "请输入 11 位手机号" : undefined}
/>
</div>
</FormSection>
<FormSection title="生日">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
<div className="space-y-1.5">
<Label htmlFor="birthdayOwner">
生日归属<span className="ml-0.5 text-[var(--color-danger)]">*</span>
</Label>
<Select id="birthdayOwner" name="birthdayOwner" defaultValue={String(v.birthdayOwner ?? 2)}>
<option value="1">结算人</option>
<option value="2">联系人</option>
</Select>
</div>
<div className="space-y-1.5">
<Label htmlFor="birthdayType">
生日类型<span className="ml-0.5 text-[var(--color-danger)]">*</span>
</Label>
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
<option value="1">阳历</option>
<option value="2">阴历</option>
</Select>
</div>
<Field
label="生日"
name="birthday"
type="date"
defaultValue={fmtDate(v.birthday)}
required={requireAll}
/>
</div>
</FormSection>
<FormSection title="其它">
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2">
<Field label="押金" name="deposit" defaultValue={v.deposit} required={requireAll} />
<Field label="买断" name="buy" defaultValue={v.buy} 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>
</FormSection>
{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>
);
}