Skip to content

Commit

Permalink
step3 - Changes the SimpleForm to use the bindField function
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Junior committed Mar 22, 2020
1 parent ce5f281 commit 2cf9a21
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions src/components/SimpleForm.jsx
Expand Up @@ -3,13 +3,13 @@ import React, { useState } from 'react';
import { useForm } from '../useForm';

const SimpleForm = () => {
const [name, setName] = useState('');
const handleSubmit = e => {
e.preventDefault();

const handleSubmit = e => e.preventDefault();
console.log('values', values);
};

// create a state variable for the name validation errors
const [nameError, setNameError] = useState('');
const { validateField } = useForm({
const { values, errors, bindField } = useForm({
validations: {
name: {
pattern: {
Expand All @@ -20,28 +20,12 @@ const SimpleForm = () => {
}
});

// handle change events in the name input
const handleNameChange = e => {
const { value } = e.target;

// set the name state with the field value
setName(value);
// validates the name field and sets the error state
setNameError(validateField('name', value));
};

return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="name">name</label>
<input
type="text"
name="name"
id="name"
value={name}
onChange={handleNameChange}
/>
{nameError && <p>{nameError}</p>}
<input type="text" name="name" id="name" {...bindField('name')} />
{errors.name && <p>{errors.name}</p>}
</div>
</form>
);
Expand Down

0 comments on commit 2cf9a21

Please sign in to comment.