From 31a196a76513df809ee91427129926370de551ba Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 14 Mar 2021 13:27:01 +0300 Subject: feat: add useOptions hook --- src/components/Select.tsx | 2 +- src/containers/Service/ServiceContext.tsx | 3 ++- src/containers/Service/ServiceForm.tsx | 5 ++--- src/containers/WaybillForm.tsx | 14 +++++++------- src/hooks/useOptions.ts | 28 ++++++++++++++++++++++++++++ src/services.js | 1 + 6 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 src/hooks/useOptions.ts (limited to 'src') diff --git a/src/components/Select.tsx b/src/components/Select.tsx index 18eb0ed..595208d 100644 --- a/src/components/Select.tsx +++ b/src/components/Select.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Field } from 'formik'; -interface Option { +export interface Option { key: string; label: string; } diff --git a/src/containers/Service/ServiceContext.tsx b/src/containers/Service/ServiceContext.tsx index 8f23d9d..6c1a760 100644 --- a/src/containers/Service/ServiceContext.tsx +++ b/src/containers/Service/ServiceContext.tsx @@ -1,11 +1,12 @@ import React from 'react'; +import { FormikProps } from 'formik'; export interface ServiceParams { route: string; name: string; nameSingular: string; tableFields: any[]; - Form: React.FC; + Form: React.FC; default: Record; } diff --git a/src/containers/Service/ServiceForm.tsx b/src/containers/Service/ServiceForm.tsx index 62e4521..e2dfef6 100644 --- a/src/containers/Service/ServiceForm.tsx +++ b/src/containers/Service/ServiceForm.tsx @@ -35,9 +35,8 @@ const ServiceForm: React.FC = () => { - - + children={service.Form} + /> )} ); diff --git a/src/containers/WaybillForm.tsx b/src/containers/WaybillForm.tsx index bb8d9ab..1e3276f 100644 --- a/src/containers/WaybillForm.tsx +++ b/src/containers/WaybillForm.tsx @@ -1,12 +1,12 @@ import React from 'react'; -import { Form, Field } from 'formik'; +import { Form, FormikProps } from 'formik'; import Input from '../components/Input'; import Select from '../components/Select'; -import hooks from '../hooks/useAPIClient'; +import useOptions from '../hooks/useOptions'; -const WaybillForm: React.FC = () => { - const { data: contractors } = hooks.contractors.useList(); - const { data: products } = hooks.products.useList(); +const WaybillForm: React.FC = ({ setFieldValue, values }) => { + const contractorOptions = useOptions('contractors', 'contractorId', values, setFieldValue); + const productOptions = useOptions('products', 'productId', values, setFieldValue); return (
@@ -14,12 +14,12 @@ const WaybillForm: React.FC = () => { ({ key: p._id, label: p.name }))} + options={productOptions} />