From b78aa7d3ecf9927c3454c4b403631f42a1bbe6b4 Mon Sep 17 00:00:00 2001 From: jlison Date: Sat, 31 Aug 2019 14:27:30 -0700 Subject: [PATCH] fix: expose react hook form in the input onClick, onBlur, onKeyUp --- src/Form.tsx | 16 ++++++++-------- src/components/Input.tsx | 28 ++++++++++++++++++---------- src/components/SingleSelect.tsx | 13 ++++++++++++- src/components/Toggle.tsx | 8 +++++++- src/interfaces/FormConfig.ts | 28 ++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/Form.tsx b/src/Form.tsx index c241e12..b532587 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -4,7 +4,7 @@ import useForm from 'react-hook-form'; import {useTranslation} from 'react-i18next'; import {Col, Form, Row} from 'reactstrap'; -import {FormConfig, FormHeader, FormHooks} from './interfaces/FormConfig'; +import {FormConfig, FormHeader, Hooks} from './interfaces/FormConfig'; import fetch from './utils/fetch'; import InputForm from './components/Input'; import SelectForm from './components/SingleSelect'; @@ -14,8 +14,8 @@ import ToggleForm from './components/Toggle'; interface Props extends FormProps { form: FormConfig[]; csrfUrl?: string; - getForm?: (formHooks: FormHooks) => any; - onSubmit: (...args: any) => Promise | T | void; + getForm?: (formHooks: Hooks) => any; + onSubmit: (data: any, formHooks: Hooks) => Promise | T | void; valid?: {[key: string]: any}; } @@ -44,10 +44,10 @@ export default ({ */ useEffect(() => { const setCsrfToken = async () => { - const {token} = await fetch(csrfUrl); + const {csrf} = await fetch(csrfUrl); - if (token && token.csrf) { - setCsrf(token.csrf); + if (csrf) { + setCsrf(csrf); } }; @@ -66,7 +66,7 @@ export default ({ key={`${dep.name}-${elementConfig.name}-${elementConfig.type}`} elementConfig={elementConfig} valid={valid} - {...formHooks} + formHooks={formHooks} /> ); }); @@ -173,7 +173,7 @@ export default ({ data.csrf = csrf; } - return onSubmit(data); + return onSubmit(data, formHooks); }; if (typeof getForm === 'function') { diff --git a/src/components/Input.tsx b/src/components/Input.tsx index 866abb4..7235fd3 100644 --- a/src/components/Input.tsx +++ b/src/components/Input.tsx @@ -14,16 +14,9 @@ import { import {DefaultInputProps, FormConfig} from '../interfaces/FormConfig'; /** Input field */ -export default ({ - elementConfig, - errors, - formState, - getValues, - register, - triggerValidation, - valid, -}: DefaultInputProps) => { +export default ({elementConfig, formHooks, valid}: DefaultInputProps) => { const {t} = useTranslation(); + const {errors, formState, getValues, register, triggerValidation} = formHooks; const values = getValues(); const {addon, className, name, placeholder} = elementConfig; @@ -73,7 +66,22 @@ export default ({ } invalid={!!errors[name]} placeholder={t(placeholder)} - onBlur={() => triggerValidation([{name}])} + onBlur={(e) => { + triggerValidation([{name}]); + if (elementConfig.onBlur) { + elementConfig.onBlur(e, formHooks); + } + }} + onKeyUp={(e) => { + if (elementConfig.onKeyUp) { + elementConfig.onKeyUp(e, formHooks); + } + }} + onFocus={(e) => { + if (elementConfig.onFocus) { + elementConfig.onFocus(e, formHooks); + } + }} />