In this practice you will continue building out your Contact Us
form by
incorporating <textarea>
and <select>
elements.
Click the Download Project
button at the bottom of this page to go to the
starter repo, then load the repo into CodeSandbox.
In a regular HTML form, the value for a <textarea>
element is defined by its
inner content:
<textarea>This is the value for the text area element.</textarea>
In React JSX, a <textarea>
element uses a value
attribute instead of its
inner content to define its value. This allows the <textarea>
element to be
handled in the same way as <input>
elements.
To see this in action, add a comments
state variable and add a "Comments"
field to the form:
<div>
<label htmlFor='comments'>Comments:</label>
<textarea
id='comments'
name='comments'
onChange={e => setComments(e.target.value)}
value={comments}
/>
</div>
To support this new form field, you'll also need to update the onSubmit
function:
// ./src/ContactUs.js
import { useState } from 'react';
function ContactUs() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [phone, setPhone] = useState('');
const [comments, setComments] = useState('');
const onSubmit = e => {
e.preventDefault();
const contactUsInformation = {
name,
email,
phone,
comments,
submittedOn: new Date()
};
console.log(contactUsInformation);
setName('');
setEmail('');
setPhone('');
setComments('');
};
return (
<div>
<h2>Contact Us</h2>
<form onSubmit={onSubmit}>
<div>
<label htmlFor='name'>Name:</label>
<input
id='name'
type='text'
onChange={e => setName(e.target.value)}
value={name}
/>
</div>
<div>
<label htmlFor='email'>Email:</label>
<input
id='email'
type='text'
onChange={e => setEmail(e.target.value)}
value={email}
/>
</div>
<div>
<label htmlFor='phone'>Phone:</label>
<input
id='phone'
type='text'
onChange={e => setPhone(e.target.value)}
value={phone}
/>
</div>
<div>
<label htmlFor='comments'>Comments:</label>
<textarea
id='comments'
name='comments'
onChange={e => setComments(e.target.value)}
value={comments}
/>
</div>
<button>Submit</button>
</form>
</div>
);
}
export default ContactUs;
To maintain symmetry across React form element types, the <select>
element
also uses a value
attribute to get and set the element's selected option. To
see this in action, add a <select>
element to the right of the <input>
element for the Phone
form field. The <select>
element should allow a user
to specify the type of phone number they're providing:
<div>
<label htmlFor='phone'>Phone:</label>
<input
id='phone'
name='phone'
type='text'
onChange={e => setPhone(e.target.value)}
value={phone}
/>
<select
name='phoneType'
onChange={e => setPhoneType(e.target.value)}
value={phoneType}
>
<option value='' disabled>
Select a phone type...
</option>
<option>Home</option>
<option>Work</option>
<option>Mobile</option>
</select>
</div>
Notice that you can leave the first Select a phone type...
<option>
element
as an empty value element before rendering the other <option>
elements.
To complete this new field, update the state variables and onSubmit
function
just like you did when adding the Comments
form field:
// ./src/ContactUs.js
import { useState } from 'react';
function ContactUs(props) {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [phone, setPhone] = useState('');
const [comments, setComments] = useState('');
const [phoneType, setPhoneType] = useState('');
const onSubmit = e => {
e.preventDefault();
const contactUsInformation = {
name,
email,
phone,
comments,
phoneType,
submittedOn: new Date()
};
console.log(contactUsInformation);
setName('');
setEmail('');
setPhone('');
setComments('');
setPhoneType('');
};
return (
<div>
<h2>Contact Us</h2>
<form onSubmit={onSubmit}>
<div>
<label htmlFor='name'>Name:</label>
<input
id='name'
type='text'
onChange={e => setName(e.target.value)}
value={name}
/>
</div>
<div>
<label htmlFor='email'>Email:</label>
<input
id='email'
type='text'
onChange={e => setEmail(e.target.value)}
value={email}
/>
</div>
<div>
<label htmlFor='phone'>Phone:</label>
<input
id='phone'
type='text'
onChange={e => setPhone(e.target.value)}
value={phone}
/>
<select
name='phoneType'
onChange={e => setPhoneType(e.target.value)}
value={phoneType}
>
<option value='' disabled>
Select a phone type...
</option>
<option value='Home'>Home</option>
<option value='Work'>Work</option>
<option value='Mobile'>Mobile</option>
</select>
</div>
<div>
<label htmlFor='comments'>Comments:</label>
<textarea
id='comments'
name='comments'
onChange={e => setComments(e.target.value)}
value={comments}
/>
</div>
<button>Submit</button>
</form>
</div>
);
}
export default ContactUs;
Congratulations! In this practice you have learned how to
- Create
<textarea>
and<select>
fields in a React form