Lab 9 - Forms
Intro
In this lab you will be creating a form.
Pre-requisites
You should have a running "payments ui" application.
1. Create a search feature
We'll start by creating a simple form that contains a single text box into which the user can enter a search term to find only relevant transactions.
We already have a component called Search which contains the input.
-
Create a stateful variable called "searchTerm", with an initial value of
""(an empty string) -
Bind the searchTerm to the value of the input.
-
Create a function to be executed when the input's value changes, to update the search term (use the event's .target.value property to find out the data in the input)
-
Call the function from the onChange event of the input
-
For now, just to test, add a function to the onClick event of the button to print out the search term to the console.
Click to view sample solution
const Search = () => {
const [searchTerm, setSearchTerm] = useState("");
const doSearch = () => {
console.log(searchTerm)
}
const handleChange = (event) => {
setSearchTerm(event.target.value);
}
return (
<div className="searchBox">
<label htmlFor="orderId">Order Id:</label>
<input id="orderId" type="text" value={searchTerm} onChange={handleChange} />
<button type="submit" onClick={doSearch} >Search</button>
</div>
);
}
2. Use an HTML Form element
-
Change the JSX in the search component so that it is now constructed as an HTML form.
-
Change the button to be of type
submitand remove its onClick event. -
Bind the onSubmit event of the form to the
doSearchfunction. -
Add the code required to the doSearch function to prevent the form being submitted to the server.
-
Test the form is working as expected!
Click to view sample solution
const Search = () => {
const [searchTerm, setSearchTerm] = useState("");
const doSearch = (event) => {
event.preventDefault();
console.log(searchTerm)
}
const handleChange = (event) => {
setSearchTerm(event.target.value);
}
return (
<div className="searchBox">
<form onSubmit={doSearch}>
<label htmlFor="orderId">Order Id:</label>
<input id="orderId" type="text" value={searchTerm} onChange={handleChange} />
<button type="submit">Search</button>
</form>
</div>
);
}
3. Implement Form validation
We'll implement the following validation rules:
-
A search term is only valid if it contains 1 or more characters, excluding spaces
-
The button should only be clickable if the search term is valid
-
We should display a visible sign to the user if the search term is not valid.
-
Create a stateful variable called
validto store if the search term is valid or not, with an initial value offalse. -
Create a stateful variable called
touchedto store if the user has attempted to enter a search term or not, with an initial value offalse. -
In the handleChange event, set the touched variable to true, and the valid variable to true only if the length of the entry is > 0 (call .trim() first in case they have only entered spaces).
-
Create a css class called
searchBoxErrorin the css file which gives a solid red border. -
Assign the input a class of searchBoxError if the value of valid is false and the value of touched is true (Hint - use the ternary operator)
-
Set the disabled property of the button to true if valid is not true.
-
Test the form is working as expected!
Click to view sample solution
const Search = () => {
const [searchTerm, setSearchTerm] = useState("");
const [valid, setValid] = useState(false);
const [touched, setTouched] = useState(false);
const doSearch = (event) => {
event.preventDefault();
console.log(searchTerm);
}
const handleChange = (event) => {
setSearchTerm(event.target.value);
setTouched(true);
setValid (event.target.value.trim().length > 0);
}
return (
<div className="searchBox">
<form onSubmit={doSearch}>
<label htmlFor="orderId">Order Id:</label>
<input id="orderId" type="text" value={searchTerm} onChange={handleChange} className={touched && !valid ? 'searchBoxError' : ''} />
<button type="submit" disabled={!valid} >Search</button>
</form>
</div>
);
}