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
className
prop with a value of"add-property"
to outer<div>
of the<AddProperty />
component. If using aFragment
, replace it with a<div>
and remove the unusedFragment
import. -
Create a new
add-property.css
file and import it into your component. -
Style your
.add-property
CSS selector to give it some padding. -
Inside the
<div>
, add a<form>
tag. -
Inside the
<form>
tag, add a<button>
with atype
prop ofsubmit
. The<button>
should have text "Add" inside. -
At the top of your component, create a
const
namedinitialState
with a keyfields
set to the following object: -
Initialise the component state by importing
useState
and 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 ownthis
property, which comes useful when working with older versions of React). More onthis
here. -
Add a single parameter of
event
tohandleAddProperty()
. Inside you should callevent.preventDefault()
to halt the default form submission behaviour, and then below log outfields
to the console. -
Give a prop of
onSubmit
to theform
and 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 anid
prop set totitle
, aname
prop 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 correspondinghtmlFor
prop set to theid
of 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.name
will give you the input name (in this case:title
) andevent.target.value
will give you the current value of the input. You want to update theevent.target.name
of thefields
state, updating its value. You will have to get down and dirty with the spread operator here. -
Add a
city
field to your initialfields
state 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 anid
prop ofcity
, aname
prop ofcity
, a value prop offields.city
and anonChange
prop ofhandleFieldChange
(see the similarities between this and the<input>
from before). Don't forget to give it a<label>
with the correspondinghtmlFor
prop. -
Give the
<select>
element some<option>
elements as children. Each should have avalue
prop. 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 thecity
select:
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 andplaceholders
to make it clearer to the user what each field is for (<label>
should inform what information should be provided, whenplaceholder
in 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!