در حال بارگذاری پیشنمایش
"use client"
import * as React from "react"
import PlateInput, { EMPTY_PLATE, isPlate, PLATE_LETTERS, stringifyPlate, type PlateValue } from "@/components/plate-input"
import { Button } from "@/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
} from "@/components/ui/field"
function toFa(value: string) {
return value.replace(/\d/g, (d) => "۰۱۲۳۴۵۶۷۸۹"[Number(d)]!)
}
export default function LicensePlateForm() {
const [plate, setPlate] = React.useState<PlateValue>(EMPTY_PLATE)
const [done, setDone] = React.useState(false)
const complete = isPlate(plate)
const letterLabel = PLATE_LETTERS.find((item) => item.letter === plate.letter)
?.label
if (done) {
return (
<Card>
<CardHeader className="text-center">
<CardTitle>پلاک ثبت شد</CardTitle>
<CardDescription>
<span dir="ltr" className="font-medium tabular-nums text-foreground">
{toFa(stringifyPlate(plate))}
</span>
{letterLabel ? ` · ${letterLabel}` : null}
</CardDescription>
</CardHeader>
<CardContent>
<Button
variant="outline"
className="w-full"
onClick={() => {
setDone(false)
setPlate(EMPTY_PLATE)
}}
>
ویرایش پلاک
</Button>
</CardContent>
</Card>
)
}
return (
<Card>
<CardHeader>
<CardTitle>ثبت پلاک خودرو</CardTitle>
<CardDescription>
شماره پلاک را مانند پلاک فلزی وارد کنید
</CardDescription>
</CardHeader>
<CardContent>
<form
onSubmit={(event) => {
event.preventDefault()
if (complete) setDone(true)
}}
>
<FieldGroup>
<Field>
<FieldLabel htmlFor="plate-01">شماره پلاک</FieldLabel>
<div className="flex justify-center py-1">
<PlateInput
id="plate-01"
name="plate"
value={plate}
onChange={(next) => setPlate(next)}
/>
</div>
</Field>
<Field>
<Button type="submit" className="w-full" disabled={!complete}>
ادامه
</Button>
<FieldDescription className="text-center">
حرف پلاک را از فهرست انتخاب کنید
</FieldDescription>
</Field>
</FieldGroup>
</form>
</CardContent>
</Card>
)
}