Adding properties
The <AddProperty /> component will be a form where we'll create a new property. We'll eventually tie it up to an API, but for now we're just concerned with the user interface (UI).
You should read the React Forms documentation before you go any further. There aren't many applications out there that don't use forms, so don't rush through this step.
The field names for a property are as follows:
title- the property taglinetype- what type of property it is...flat, terrace etc.bedrooms- number of bedroomsbathrooms- number of bathroomsprice- cost of propertycity- where property is basedemail- email of person to contact
To complete this challenge, you will need to:
-
Give a
classNameprop with a value of"add-property"to outer<div>of the<AddProperty />component. If using aFragment, replace it with a<div>and remove the unusedFragmentimport. -
Create a new
add-property.cssfile and import it into your component. -
Style your
.add-propertyCSS selector to give it some padding. -
Inside the
<div>, add a<form>tag. -
Inside the
<form>tag, add a<button>with atypeprop ofsubmit. The<button>should have text "Add" inside. -
At the top of your component, create a
constnamedinitialStatewith a keyfieldsset to the following object: -
Initialise the component state by importing
useStateand set it like so: -
Create the
handleAddProperty()function inside the Component wrapper function. You can write this as an arrow function or not (we prefer arrow functions as they don't have their ownthisproperty, which comes useful when working with older versions of React). More onthishere. -
Add a single parameter of
eventtohandleAddProperty(). Inside you should callevent.preventDefault()to halt the default form submission behaviour, and then below log outfieldsto the console. -
Give a prop of
onSubmitto theformand set the value tohandleAddProperty. -
If you view your page in the browser now and click the "Add" button, you should see
{ title: '' }logged out to the console. -
Inside the
<form>tag above your<button>, add an<input>. It should have anidprop set totitle, anameprop set totitle, a value prop set tofields.title(the state you set earlier), and an onChange handler set to ahandleFieldChange()function. It should also have a<label>with the correspondinghtmlForprop set to theidof the field referenced, in this case it will betitle. -
Create the
handleFieldChange()function. Make it an arrow function and should have a parameter ofevent. -
Inside the
handleFieldChange()function, set the state of the field being updated.event.target.namewill give you the input name (in this case:title) andevent.target.valuewill give you the current value of the input. You want to update theevent.target.nameof thefieldsstate, updating its value. You will have to get down and dirty with the spread operator here. -
Add a
cityfield to your initialfieldsstate with a value of"Manchester". It will be the default option we wish to show in our city dropdown. -
Add a
<select>element. It should have anidprop ofcity, anameprop ofcity, a value prop offields.cityand anonChangeprop ofhandleFieldChange(see the similarities between this and the<input>from before). Don't forget to give it a<label>with the correspondinghtmlForprop. -
Give the
<select>element some<option>elements as children. Each should have avalueprop. The text for each select<option>will be passed as a child to the<option>element. Here are the values and text you will need for thecityselect:
| Value | Text |
|---|---|
| Manchester | Manchester |
| Leeds | Leeds |
| Sheffield | Sheffield |
| Liverpool | Liverpool |
- Add the other fields (repeating the above steps: add field state + add
<input>/<select>). All are<input>s apart from "Type" which is another<select>dropdown. Think about the appropriate<input>types to use. The options for the "Type" field are:
| Value | Text |
|---|---|
| Flat | Flat |
| Detached | Detached |
| Semi-Detached | Semi-Detached |
| Terraced | Terraced |
| End of Terrace | End of Terrace |
| Cottage | Cottage |
| Bungalow | Bungalow |
- Fill out your form and click the "Add" button. If you've done everything correctly then you should see an object logged out to the console which has key/value pairs matching your
<input>/<select>names and values. - Give your inputs
<label>s andplaceholdersto make it clearer to the user what each field is for (<label>should inform what information should be provided, whenplaceholderin which format, including capitalisation or usage of special characters, like "john.smith@email.co.uk"). Use CSS to style your form fields. Don't be afraid to wrap them in<div>elements to use margins and padding to space everything out a bit!