@@ -1223,4 +1223,108 @@ const EventComponents = () => (
12231223 < button onPaste = { } > btn</ button >
12241224 { /* and many more -> https://reactjs.org/docs/events.html */ }
12251225 </ >
1226- )
1226+ )
1227+
1228+
1229+ // FORMS (controlled, react manage the state of the form)
1230+ import { useState } from 'react' ;
1231+ const ControlledInput = ( ) => {
1232+ const [ input , setInput ] = useState ( '' ) ;
1233+ const [ textarea , setTextarea ] = useState ( '' ) ;
1234+ const [ select , setSelect ] = useState ( 1 ) ;
1235+ const [ checkbox , setCheckbox ] = useState ( false ) ;
1236+ const [ radio , setRadio ] = useState ( false ) ;
1237+ function handleChange ( e ) {
1238+ setInput ( ( ) => e . target . value ) ;
1239+ }
1240+ return (
1241+ < div >
1242+ < form >
1243+ < label htmlFor = "input" > Name:</ label >
1244+ < input id = "input" type = "text" name = "name" value = { input } onChange = { handleChange } />
1245+ { /* external OR inline onChange function */ }
1246+ < input value = { input } onChange = { ( e ) => setInput ( e . target . value ) } />
1247+ < textarea value = { textarea } onChange = { ( e ) => setTextarea ( e . target . value ) } />
1248+ < select value = { select } onChange = { ( e ) => setSelect ( e . target . value ) } >
1249+ < option value = { 1 } > 1</ option >
1250+ < option value = { 2 } > 2</ option >
1251+ </ select >
1252+ < input type = "checkbox" checked = { checkbox } onChange = { ( e ) => setCheckbox ( e . target . value ) } />
1253+ < input type = "radio" checked = { radio } onChange = { ( e ) => setRadio ( e . target . value ) } />
1254+ </ form >
1255+ < p > Input value: { input } </ p >
1256+ < p > Textarea value: { textarea } </ p >
1257+ < p > Select value: { select } </ p >
1258+ < p > Checkbox value: { checkbox . toString ( ) } </ p >
1259+ < p > Radio value: { radio . toString ( ) } </ p >
1260+ </ div >
1261+ )
1262+ } ;
1263+
1264+
1265+ // 'react-hook-form' to build forms faster (uncrontrolled forms & more performance)
1266+ import { useForm } from "react-hook-form" ;
1267+ function ReactHookForm ( ) {
1268+ const { register, handleSubmit, watch, formState : { errors } } = useForm ( ) ;
1269+ const onSubmit = data => console . log ( data ) ;
1270+ console . log ( watch ( "example" ) ) ; // watch input value by passing the name of it
1271+ return (
1272+ /* "handleSubmit" will validate your inputs before invoking "onSubmit" */
1273+ < form onSubmit = { handleSubmit ( onSubmit ) } >
1274+ { /* register your input into the hook by invoking the "register" function */ }
1275+ < input defaultValue = "test" { ...register ( "example" ) } />
1276+ { /* include validation with required or other standard HTML validation rules */ }
1277+ < input { ...register ( "exampleRequired" , { required : true } ) } />
1278+ { /* errors will return when field validation fails */ }
1279+ { errors . exampleRequired && < span > This field is required</ span > }
1280+ < input type = "submit" />
1281+ </ form >
1282+ ) ;
1283+ }
1284+
1285+
1286+ // 'formik' to build forms faster (controlled forms)
1287+ import { Formik , Field , Form } from "formik" ;
1288+ function FormikForm ( ) {
1289+ return (
1290+ < Formik
1291+ initialValues = { { name : "" , email : "" } }
1292+ onSubmit = { async ( values ) => {
1293+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
1294+ alert ( JSON . stringify ( values , null , 2 ) ) ;
1295+ } }
1296+ >
1297+ < Form >
1298+ < Field name = "name" type = "text" />
1299+ < Field name = "email" type = "email" />
1300+ < button type = "submit" > Submit</ button >
1301+ </ Form >
1302+ </ Formik >
1303+ ) ;
1304+ }
1305+
1306+
1307+ // 'yup' helps with validation
1308+ import * as yup from 'yup' ;
1309+ const schema = Yup . object ( ) . shape ( {
1310+ name : yup . string ( ) . min ( 2 , 'Too short' ) . required ( 'Required' ) ,
1311+ email : yup . string ( ) . email ( 'Invalid email' ) . required ( 'Required' )
1312+ } ) ;
1313+
1314+
1315+ // FORM (uncrontolled, DOM manage the form)
1316+ import React , { useRef , useState } from "react" ;
1317+ const UncontrolledInput = ( ) => {
1318+ const fileInput = useRef ( "" ) ;
1319+ const [ fileName , setFileName ] = useState ( "" ) ;
1320+ const func = ( ) => {
1321+ setFileName ( fileInput . current . value ) ;
1322+ } ;
1323+ return (
1324+ < >
1325+ < input type = "file" ref = { fileInput } />
1326+ < button onClick = { func } > Upload file</ button >
1327+ { fileName && < p > You uploaded { fileName } </ p > }
1328+ </ >
1329+ ) ;
1330+ } ;
0 commit comments