From f642d61d68f4380d3fe0b0f76d31e53c97d0b488 Mon Sep 17 00:00:00 2001 From: dmisamohin Date: Fri, 6 Jun 2025 18:16:12 +0300 Subject: [PATCH] [fixed] --- src/app/contractors/create/page.tsx | 349 +++++----------------------- src/components/Input/index.tsx | 2 +- src/components/Table/index.tsx | 22 +- 3 files changed, 63 insertions(+), 310 deletions(-) diff --git a/src/app/contractors/create/page.tsx b/src/app/contractors/create/page.tsx index b1ec9bf..3ac707e 100644 --- a/src/app/contractors/create/page.tsx +++ b/src/app/contractors/create/page.tsx @@ -1,7 +1,8 @@ 'use client' import { Breadcrumbs, IBreadcrumbProps } from '@/components/Breadcrumbs'; +import { Input } from '@/components/Input'; import { PageLayout } from '@/components/PageLayout'; -import { useState } from 'react'; +import { SubmitHandler, useForm } from 'react-hook-form'; const BREADCRUMBS: IBreadcrumbProps[] = [ { @@ -33,104 +34,55 @@ const BREADCRUMBS: IBreadcrumbProps[] = [ } ]; +type ICreateContractorFormProps = { + name: string; + type: string; + inn: string; + kpp: string; + ogrn: string; + address: string; + phone: string; + email: string; + contactPerson: string; + account: string; + bank: string; + bik: string; + correspondentAccount: string; +}; + +const CREATE_CONTRACTOR_FORM_INIT: ICreateContractorFormProps = { + name: "", + type: "legal", + inn: "", + kpp: "", + ogrn: "", + address: "", + phone: "", + email: "", + contactPerson: "", + account: "", + bank: "", + bik: "", + correspondentAccount: "", +}; + export default function CreateContractor() { - const [formData, setFormData] = useState({ - name: '', - type: 'legal', // или 'individual' - inn: '', - kpp: '', - ogrn: '', - address: '', - phone: '', - email: '', - contactPerson: '', - bankDetails: { - account: '', - bank: '', - bik: '', - correspondentAccount: '' - } - }); - const [isLoading, setIsLoading] = useState(false); - const [error, setError] = useState(''); - - const handleChange = (e: React.ChangeEvent) => { - const { name, value } = e.target; - - if (name.includes('.')) { - const [parent, child] = name.split('.'); - // setFormData(prev => ({ - // ...prev, - // [parent]: { - // ...prev[parent as keyof typeof formData], - // [child]: value - // } - // })); - } else { - setFormData(prev => ({ - ...prev, - [name]: value - })); - } - }; - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - setIsLoading(true); - setError(''); - - try { - // Здесь будет запрос к вашему API - const response = await fetch('/api/contractors', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(formData), - }); - - if (!response.ok) { - throw new Error('Ошибка при создании контрагента'); - } - - const data = await response.json(); - // router.push(`/contractors/${data.id}`); - } catch (err) { - setError(err instanceof Error ? err.message : 'Неизвестная ошибка'); - } finally { - setIsLoading(false); - } + const { register, handleSubmit } = useForm({ defaultValues: CREATE_CONTRACTOR_FORM_INIT }) + + const onSubmit: SubmitHandler = (form) => { + console.log("form", form) }; return ( - +
- {error &&
{error}
} - -
+
- {/* Основная информация */}

Основная информация

-
- - -
+
-
- - -
+ + - {formData.type === "legal" && ( -
- - -
- )} - -
- - -
+

Контактная информация

-
- - -
- -
- - -
- -
- - -
- -
- - -
+ + + +
- {/* Банковские реквизиты */}

Банковские реквизиты

-
- - -
- -
- - -
- -
- - -
- -
- - -
+ + + +
- -
diff --git a/src/components/Input/index.tsx b/src/components/Input/index.tsx index ae4d5e5..92c0247 100644 --- a/src/components/Input/index.tsx +++ b/src/components/Input/index.tsx @@ -10,7 +10,7 @@ type IInputProps = { export const Input = ({ label, isError, errorText, className, id, ...props }: IInputProps) => { const inputClsx = twMerge( - classNames("w-full px-3 py-2 border rounded-md shadow-sm focus:outline-none focus:ring-1", + classNames("mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 border p-2", { "border-red-300 text-red-900 placeholder-red-300 focus:ring-red-500 focus:border-red-500": isError, "border-gray-300 focus:ring-blue-500 focus:border-blue-500": !isError, diff --git a/src/components/Table/index.tsx b/src/components/Table/index.tsx index 6a4844c..f99d8ec 100644 --- a/src/components/Table/index.tsx +++ b/src/components/Table/index.tsx @@ -14,6 +14,7 @@ import { flexRender, RowData, } from '@tanstack/react-table' +import { Input } from '../Input'; type TableProps = { tableData: T[]; @@ -162,16 +163,8 @@ export function Table({ tableData, columns }: TableProps) ) } -function Filter({ - column, - table, -}: { - column: Column; - table: TanStackTable; -}) { - const firstValue = table - .getPreFilteredRowModel() - .flatRows[0]?.getValue(column.id); +function Filter({ column, table }: { column: Column, table: TanStackTable }) { + const firstValue = table.getPreFilteredRowModel().flatRows[0]?.getValue(column.id); const columnFilterValue = column.getFilterValue(); @@ -192,18 +185,13 @@ function Filter({ - column.setFilterValue((old: [number, number]) => [ - old?.[0], - e.target.value, - ]) - } + onChange={(e) => column.setFilterValue((old: [number, number]) => [old?.[0], e.target.value])} placeholder={`Max`} className="w-24 border shadow rounded" />
) : ( - column.setFilterValue(e.target.value)}