Andi, youcanseehereittellsyoutogotoSRCat J s editthecodeandsaveanditwillalltoreload.
Solet's dothatnowtostartoffwith.
We'rejustgonnaflythroughbuilding a reallysimplecomponentwithreactjustas a tasterafterthatwillmoveontotheautocompletetaxbooks, whichismoreusefulbutalsomorecomplex.
Yougive a constructorandtheconstructortakesanargumentcausedcalledpropsthesadjustpropertiesforthecomponentsandtheycouldbepassedinlikeattributesarepasttwoinHTMLtags.
Let's create a newmethodontheclasscalledToggleAceHidden.
I didn't takeanyargumentsonthisisgoingtouse a specialmethodwhichisinheritedfromthereactstockcomponentclassonthisiscalledsetstakedforreasons I won't gointonowbecauseit's beyondthescopeofthevideo.
Thecurrentishiddensocurrentstatethoughtishiddenandagainforreasons I won't gointonowbecauseit's toocomplicatedforthemoment, youmustalwaysyougetthecurrentvalueofanystateobjectwhenyou'redoing a setstatefromcurrentstate, notfromthisthoughtstate, you'llhavetowatchmyentirereactcoursetofindoutwhy.
Sowhat's happeninghereiswhenweinitiallyrenderthebuttonishiddenisfalseOndsothetextishittingandwhenwecollectthebottomtoggleishiddeniscalledsetstateiscalledanditsetsishiddentotrueEachtimeweupdatethestateofreactcomponentthecomponentrearendersSotherenderfunctioniscalledagainonSowhenweclickforthefirsttimeinhishiddenissettotrueThisfunctionisthencalledagainThistimeishiddenistrueonDSOthetextisn't shownNowwe'regonnamoveontobuilding a moreusefulrielworldcomponent.
It's oneofthosetextboxeswetypeandget a listofpossiblelightensyoucanselectwhichfillinthetaxbox a bitliketheautosuggestionsearchboxesonmostsearchengines.
What I reallylikeaboutthisisthatithasreally a worlduses.
It's a verysimplecomponent, butbycreatingit, you'lllearnaboutreusablereactcomponents, props, componentstateandusingCSSwithreact.
You'llfirstneedtosetup a newreactsupbyinstalling, create, rackedup, greetingyououtwithiton, thenrunitbygoingtothedirectorintheterminalanddoingMPMstart.
Theycouldbeusedtorepresentanythinginyouryoureyefromsmallthingslike a standaloneyouinwidgetsuchasthisautocompletetaxboxwe'regoingtocreaterightupuntilyousomethinglarge, likeanentireWebpage.
Itneedstoextendthereactdotcomponentclassinordertomakeit a componentoutSexboxwillneedthelistofallpossiblelightensthatitcanorder, completewithinthefuturevideo.
I'llshowyouhowtomakethatconfigurable, butfornowwe'rejustgonnahardcode a listofnamestodothis.
First, create a constructorreactcomponentConstructorsmusttakeanargumentcalledpropsandcalledthesuperclassconstructorpassingthepropsargumenttoit.
Thisjustensuresthecomponentissetupproperly.
Let's add a membervariablecalleditems, soit's thisstoreitemsandit's goingtobeanarray.
Wecreate a membervariablesowecanaccessitfromanywhereintheclasslater.
Sincethefunctionisanexpression, wewrapitincurlybracketsonallthefunctionsgoingtodoistaketheeventthathasbeengeneratedonconsoledotlogeventdottargetdotvalueor e dottargetvalue.
Thenewversionofthecomponenttotheuserreactivecomponentgivesus a functioniscalledSetState.
Thatdoesjustthis.
Soit's thisdotsetstate, andnowthistakes a functionwhichwillreturnanobjectwithanystatechangesinit.
Soanypropertieswewanttochangeinthestategoingthatobject, thereasonittakes a functionthatreturnsanobjectinsteadofjustanobjectisbeyondthescopeofthisvideo.
I do, however, talkaboutthisinmyyoutomecourse, sowewanttosettheirsuggestionstoemptyrate, sothefunctionjustreturns.
Theelsecaseisbeforethetextisn't emptyForthiscase, we'regoingtocreate a caseinsensitivereggaeaxetotestformatchesintheitemslistthatstartwiththetexttheuserentered.
Next.
We'regoingtodefinethelistofMaxmatchingsuggestionsandputitin a constanton.
Thisisgoingtobedefinedastakingthecontentsofftheitemsarray, sortingitalphabeticallyandfilteringitforitemsthatmatchtherecord X wejustcreated.
Wedon't needtoupdatethestatewiththefilteredlistofsuggestions, andactually, I'vejustthoughtof a betterwayofwritingthis.
Now, whenwetypeinthetaxboxthevalueupdatestoselecttheitem, weneed a functiontosay a suggestionhasbeenselected, sowe'regonnagoheadonat a functionsuggestionselected.
Sincethisis a reusablecomponent, wewant a flexiblewith.
I thinkthebestwaytoachievethisistomakeeverything 100% with.
Thenwhensomeoneusesthecomponent, theycanputin a fixedwithcontainerinordertodictatethefinalwithoffthecomponent.
Nowlet's lookattheborder.
Sowehave a graveorderwith a dropshadow.
Myfirstthoughtwastoput a borderaroundboththetextinputontheULthatappearswithsuggestions.
Themainproblemofthisisthatifweput a borderanddropshadowonbothelements, theywouldoverlapatthebottomofthetextinputonthetopoftheyou.
Well, thesimplestsolutionisjustthatthecontainerDavehas a borderand a dropshadow, whiletheULandtexthavenonethisway, whenjustthetextbookshasshowntheborderwrapsthatwhenthesuggestionlistisalsoshown, itwrapsbothcomponents.
Solet's makethosechanges.
Nowthewidthofourcontainerisalready 100%.
Solet's add a newselectorforthetextinputandgivethat a widthof 100% two.
Wethenadd a graybordertoourcontainer, andthenwewanttodropshadowonit, too.
Sowehadsomecodefromthat.
Now I justgeneratedthiswithanonlinetool.
YoucanGoogleCSSbookShadowGeneratorandyou'llfind a similartoolon.
Wetakeofftheborderfromtheinput.
Sowe'regoingtogointoourAPPcomponentnowonDhe.
We'regoingtowrapthetaxboxin a deathwiththeclassnameofAPhyphencomponents.
ThenwegobacktoappdotCSSandwedo a selectorforAPhyphencomponents.
Finally, there's also a smallYouprobablycan't seeit, butthereis a smallgapbetweenthetopoftheULonthebottomofthetextinputthatthrowsthingsoffjust a littlebit.
So I'llget a listofallthecountriesintheworld I Googleforitsandthere's actually a textfarforitonGetHub, whichisperfect.
So I click.
FewruleAndi.
I copyallofthem.
Youwantthecountriestobein a right, so I create a newmodulecountriesdotJsInSRCOndawesimplypasteintheentirelistandwe'regoingtoexportitfromthemoduleasthedefaultexport.