From 6274200c0b88de4a076ae28df7c005a51d5ad0a0 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 19 Oct 2019 16:39:53 +0200 Subject: [PATCH] manually set form or error --- lib/index.ts | 2 +- test/test.tsx | 68 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 499c408..0b31487 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -93,5 +93,5 @@ export const useForm = = ({ error }) => (!error ? null :
{error}
) +const initial = { + username: '', + password: '', + type: 'formhero', + awesome: true, +} + const Index: React.FC = () => { - const { field, form, errors, isValid } = useForm( - { - username: '', - password: '', - type: 'formhero', - awesome: true, - }, - { - username: [ - /^test/, - { - validator: async () => { - return true - }, - message: 'Digits please', + const { field, form, errors, isValid, setForm, setErrors } = useForm(initial, { + username: [ + /^test/, + { + validator: async () => { + return true }, - ], - password: { - validator: /^.{3,}$/, - message: 'To short', + message: 'Digits please', }, - awesome: value => !!value, - } - ) + ], + password: { + validator: /^.{3,}$/, + message: 'To short', + }, + awesome: value => !!value, + }) const _submit = (e: React.FormEvent) => { e.preventDefault() console.log(form, errors, isValid) } + const reset = () => { + setForm(initial) + } + + const error = () => { + setErrors({ + username: 'nope', + }) + } + return (
@@ -81,12 +90,21 @@ const Index: React.FC = () => { + +
+
+ + +
+
+
) } ReactDOM.render(, document.getElementById('root')) - -// @ts-ignore -// if (module.hot) module.hot.accept()