Press "Enter" to skip to content

Learn React & Material UI – #8 Forms (Part 1)


right so as you saw in the last video
deleting exercise is really not a big
deal
now what about editing existing
exercises what I’m gonna do next is I’m
actually in a clone this element over
here I’m just can’t paste it in I’m
going to delete the Delete icon then I’m
going to go back to material io / icons
that’s the website for Google’s material
icons what I’m gonna do is I’m gonna
search for word the Edit icon if I
scroll down you’re actually going to
find this beautiful pen that we can use
so the name of this icon is edit to go
back we could just simply use it with
the camel case as always and the Edit
icon needs to be imported from
material.you icons as well so now we
have this beautiful icon and of course
the onclick handler is also going to
change so we’re gonna switch from on
delete to let’s call it on select edit
we’re gonna have to pass the ID to that
method as well and in fact that method
doesn’t exist even yet so I’m gonna add
to the list of properties and finally
we’re gonna need to pass it to the
component from within the app root
component so what I’m gonna do is I’m
gonna call this handle exercise let’s do
select edit okay so we need to add that
method as well let’s add it at the
bottom we’ll have an ID and then finally
we’re gonna do is well put set state and
then for the set state call we can
actually do is we can actually add a new
property let’s call it edit mode and be
set to true now the idea here is that
when you click on the edit mode icon
it’s going to open up the form basically
the same form as you saw in the previous
videos the one for creating a new
exercise it’s going to display that form
on the right pane so that we can
actually modify the title description as
well as the muscles for a given exercise
so we’re going to set the value of that
property on the state to true but we
also need to
update the selected exercise right
because when you click on this button
you’re going to need to select an
exercise pretty much the same way as we
already do when you click on the list
item itself we’re also going to do the
same thing that this property does in
the end so in OP GS it refers to handle
exercise select to this method so what
we need to do is but basically in the
end need to do pretty much the same
thing by updating the selected exercise
in fact what I’m gonna do is I’m going
to place the backhand but we need to
update the edit mode property to true
and to be clear you could also set the
edit mode to false as the initial value
on the state so you could set it to
false but if you don’t set it to false
it’s actually going to be undefined and
in the end it’s also going to be falsies
so you can keep it out from the state
just for the sake of brevity so I’m
going to scroll back here and in fact
because we just have a single set state
call we don’t actually need the curly
braces we’re not actually returning
anything from the method I’m gonna do
the same thing for all of the other ones
I’m going to kill all of those
unnecessary curly braces let’s do the
same thing over here and take a few
seconds okay that’s better so we
simplified the code base slightly now of
course in this case if I click on edit
it’s going to well it’s not going to
display anything because we did not
update the logic or here for the right
thing so the one thing we could do is
inside of the paper component what we
can say is basically the following so
let’s say if we do have the edit mode
property enabled so if edit mode is true
then what we’re going to do is we’re
actually going to display something
right
and in this case would like to display a
form and this form would contain the
title description and muscle fields just
like the form that we have when you
click on the plus icon to create a new
exercise it’s going to be the exact same
form and otherwise if we just simply
display a fragment so an empty wrapper
which could also be a div of course but
I’ll use a fragment and otherwise we’re
basically just display the to typography
elements inside of it so I’ll just base
them inside the fragment okay and then
the problem here of course is that edit
mode is not defined so I’m going to
expect the edit mode property to be
passed with the props let’s say that and
then the edit mode of course well that
needs to come from the app component
let’s reference edit mode and the edit
mode is going to come from the state so
again the edit mode property basically
just indicates whether we are in the
edit mode and what I mean by added mode
is a mode in which the form is being
displayed and you’re basically able to
modify the fields of the selected
exercise so that’s the whole idea so I’m
gonna save app GS and of course this is
not going to work still because we’re
referencing a form which is not really
an existing element so what I’d like to
do is I’d like to do a bit of reflection
here back in the dialog create a s file
we already have the form nested inside
of the modal over here and the one thing
that actually mentioned in the
description of one of my videos is that
it’s kind of weird that we have the
exercise model nested inside of the
state on the dialog because the dialog
should not really be concerned with the
exercise so this property is sort of
like off on the state you’ll be better
to extract it to a separate component
which will in the end be responsible for
handling it so what I’d like to do is
I’d like to actually create a new file
let’s call it from GS and this from GS
will basically of course as always it’s
gonna import the act from
we act this will be a component and
we’re basically going to export the
fault a class the type of pattern that I
like to follow for this file is known as
a control component so a control
component is basically a component that
contains its own state and it’s
responsible for maintaining its own
state in this example the component that
I’d like to create for the form but
basically be the form that we have
inside of the dialog of course
understand minimize this and I’m going
to grab the form and paste it in here
and in fact we’re going to need to
create the errata method first so let’s
return the form I’m going to paste it in
and then we’re going to need to also
extract all these other guys the imports
from a truly wide just so that we’re
able to reference those elements inside
of the form component and then back and
create GS what we can actually do is we
can basically go back to this place and
put the form inside of it and of course
we’re going to need to pass some of the
properties as well so what the form
actually expects to have is it expects
to have me categories which if you
remember our basically muscles in fact
we can actually extract this line from
create GS because we no longer need to
manipulate the muscles or the categories
and I pasted in here this is going to be
a constant for basically extracting the
classes and the muscles of the props and
in fact the classes will that refers to
these styles that we created in the last
videos my base time in here so let’s
have these styles and we’re going to
need to remove the with a styled call as
well let’s paste it in here I’m going to
close off the class and then with styles
well that needs to be included inside of
form GS as always so there were five
things a bit boring of course but you
stick with it it’s
yet more enticing in just a few minutes
so what else are we missing here well
the exercise itself that’s going to be
gone because the form is going to be
responsible for maintaining the state on
the exercise so I’m just gonna cut that
state let’s create a new state property
looks like I made a typo of course this
Const extraction or the structuring
family props that needs to be inside of
the render method so we’re gonna have
our initial state over here and then
back in here we’re going to need to cut
all these methods you handle unchanged
handle submit they’re no longer relevant
to the dialog so I’m gonna base them in
here okay and of course we’re gonna need
to change a few things so we’re no
longer updating the exercise on the
state we’re actually updating the
property directly so I’m going to delete
that but the method here is gonna do
well it’s very simple it’s just
basically going to update the property
that has a name of name coming off this
variable and then it’s going to update
the state for that property so I’m just
remove the curly braces which we don’t
actually need in this case and it looks
like something is breaking in create GS
let’s see we have a fragment with a
button with a dialogue and form I don’t
think we ever included the form so let’s
imports form from form in fact that’s
actually yeah in this case we need to go
up to get the form what I’m going to do
is I’m actually going to move create two
exercises and in this case I’m going to
delete the dialogues folder and I’m
going to remove create to just simply
dialogue GS
that this is going to break the
importing Heather so what I’ll do is
I’ll change it to dialogue and this is
gonna come from exercises dialogue like
that in fact we can keep it as create
dialogue just for clarity the name of
the default export is basically up to
you so we’ll keep it as create dialogue
and then this is coming from exercises
dialogue so it should be good let’s
refresh this when I go back to dialogue
and we have something on line 58 so I
forgot the closing parenthesis there
component is not defined this one is
coming up from here from the former
extending the component but we forgot to
report it so include the component as
well as the fragment which I think we’re
gonna need at some point probably not do
need it on include what else we have
this constant over here that’s coming
from the state that’s no longer
applicable we only are going to have the
open property and what else text field
is not defined well these two are coming
from maturely why as well I’m gonna cut
them I’m gonna base them back in here in
fact where you need to create another
import for the text field for the select
but also the button if you remember we
used to have the button at the end of
the form in fact we included it in
dialogue options now the problem with
that is that the button always needs to
be present on the form whether you’re
creating the exercise or you were
editing an existing one but the dialogue
is only applicable when you create a new
exercise but not when you add it so the
button like I said always needs to be
there and just go
I’m gonna paste it at the end over here
after a break and the button like I said
would need to be imported as well
from material you I like that without
next just material UI and then here we
can just get a bit of dialogue actions
like that
and then the dialogue actions can also
be killed from there okay and then
lastly in form G s what we need to do is
we also need to extract the title
description as well as the muscles off
the state so that’s what I’m gonna do
and then we can also keep the other ones
that we extract from the props like that
and then to map through the categories
well of course we’re going to need to
pass them down to exercises so what I’ll
do is all pass the muscles from app GS
like that the muscles if you remember
they basically refer to the muscles at
the very top coming directly from the
store we’re gonna pass them down to
exercises and then the exercises
component is going to need to receive
them through props we’re gonna do
muscles so these muscles over here they
need to be passed to the forum as well
so what I’ll do is I’ll pass the muscles
down to the forum component like that
and then the last thing we’re going to
need to do is we’re also going to need
to include the submit handler so in this
case what we can do is the handle submit
function is going to call the oncreate
prop but because we want to keep it
generate for both of the cases when you
create an exercise but also when you
edit it let’s change it to on submit
like that the on submit is going to need
to come from index GS from this file so
we’ll do
submit we’re gonna call on edit which is
a new property that we have to create so
I’ll add it to the list of props in
there on edit and it needs to be also
added
incidentally to app GS of course so
let’s do it on edit
let’s creating a method we’re gonna call
it handle exercise edit then we need to
create the method of course so at the
end of the list I’m going to add a new
one this one’s going to accept the
exercise as well and then we’re going to
set the state most likely and when the
exercise is edited well we need to do is
we need to pass a callback as we always
do and this callback is going to accept
the previous state from which we’re
going to extract the exercises exercises
like that the idea here is that when the
exercise changes so when we received a
new updated to exercise object we’re
gonna do is we’re gonna get the
exercises and we’re going to filter them
down to the exercises that don’t contain
the current exercise that’s been edited
so we’ll do when the ID does not equal
to exercise ID and then once we get that
so when I cut it actually what I’ll do
is I’m actually going to extract all of
those elements to in your a and then
we’re just basically going to append the
new exercise to that array so what that
actually is going to do is it’s going to
find all the exercises that don’t have
the ID of this exercise that we accept
as the argument
it’s going to filter them down and then
it’s going to add the new exercise
object to the end of the new array and
then everything else is going to be
recalculated and then you’re gonna see
the updated exercise in the list on the
left now back in dialogue Diaz in this
scenario we’re actually going to need to
pass the muscles of course these muscles
actually come from header so what you do
here is let’s actually extract them from
the props so what I’ll do is and get
them get the muscles from the props like
that or pass the muscles as the muscles
property down to the foreign component
and the form component is also going to
need to accept the onsubmit property
which would refer to oncreate property
coming from the header so let’s also
structure that from the props I’ll bring
back the indentation to what it was
before let’s save it let’s not forget to
include the form and XJS as well so form
from basically from floor it exists in
the same directory so now when you click
on the edit icon or the pen icon that’s
actually going to open up the form but
because we’ve set the width to 500 it’s
actually not being displayed correctly
so let’s change it to 300 perhaps that
looks better but of course as you can
see here it doesn’t actually populate
the form because we did not provide any
initial value into it so the one thing
we can do here is instead of setting the
state to the properties that you simply
refer to empty strings what we can do
here is let’s actually create a method
get initial State
and this method is going to do the
following it’s going to check it’s going
to try to be structure and exercise from
the props okay unless props and on the
same note we’re actually going to pass
that exercise and in that GS to the
phone so what I’ll do is I’m going to
pass the exercise object down to the
form so it’s do exactly that exercise
that in this exercise refers to the
currently selected exercise that
basically exists on the state and it
gets updated whenever the exercise is
being selected so when you click on the
list item for example the exercise is
being selected but when you click on the
Edit icon the exercise is also being
selected because that’s exactly what we
do in setup set state we’re finding the
exercise on the exercises array and then
we updating the currently selected
exercise to that exercise that we’re
able to find based on the ID that’s
being provided and the ID in case you
forgot it’s coming from index GS over
here when I call the on select edit
method so back in form GS we can expect
the exercise object on the props and
once we do get it we can actually return
either the exercise if we do have it or
we can just simply return an object that
will contain all those properties like
that so when we’re setting the state the
initial state on the form we can
actually call get in its state method
like that and this would either return
the exercise to the properties of the
exercise the title description muscles
or just an empty object if that exercise
is not available which is the case when
you open up a create exercise model and
in this case we need to fix it to the
get initial state let’s try that again
that’s what the form is going to look
like we don’t have any initial values
that’s why we get this empty object over
here but if I click on the Edit icon
let’s say for benchpress no we get an
error the exercise is not defined and
that’s because the exercise let’s see
whenever extracted from the props so
let’s actually D structure it as well
and I’ll keep this one in there for now
as well let’s save it let’s click on the
Edit icon again so you can see here it
pre-populates the form with the values
so in this case I clicked on benchpress
we’ve got the title key muscles and we’ve got the description as well

Please follow and like us: