I want to share my 3 approaches of handling cascading form fields.
- First approach is general, using state variables.
- Second is to use ordinary variables and one boolean state variable to trigger the state effect (refresh page).
- Third is, dynamic form fields with ordinary variables.
This is 3rd approach and we are going to deal with dynamic form fields.
Note, it will be easier to understand this post if you take a look at the previous 2 approaches.
First approach, Cascade Form Basic
Second approach, Cascade form improved
Lets' begin,
Contents
- Base Form
- Form fields object
- Dynamic field rendering
- Sample Data
- OnPage load
- Load Country
- OnChange
- Load State
- Load rest (City, Village, Street)
- Validation
- Reset Form
Base Form
This is static form page with 5 dropdown fields.
import React, { useState, useEffect } from "react"; import { ScrollView, View, Text, StyleSheet, TouchableOpacity } from "react-native"; import { Dropdown } from "react-native-element-dropdown"; import { Snackbar } from "react-native-paper"; var snackMsg = ""; export default function App() { const [refreshPage, setRefreshPage] = useState(false); const [visible, setVisible] = useState(false); const onToggleSnackBar = () => setVisible(!visible); const onDismissSnackBar = () => setVisible(false); const resetForm = () => { }; return ( <ScrollView> <p>refreshPage state variable is used to refresh the page in all the situations. </p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173363402014343.jpg" class="lazy" alt="Cascading Form React Native Advanced" /></p> <p>Now, These fields are going to be converted as dynamic. </p> <h3> Form Field Object </h3> <p>Previously we had 3 separate objects for 3 different fields, but here all fields data placed under one form field object.<br> </p> <pre class="brush:php;toolbar:false"> . . . const formFields = { country: { fieldId: "country", label: "Country", labelField: "name", valueField: "countryId", parents: [], list: [], selectedItem: {}, selectedValue: null, onValueSelected: () => null, }, state: { fieldId: "state",label: "State",labelField: "name",valueField: "stateId", parents: ["country"],list: [],selectedItem: {},selectedValue: null, onValueSelected: () => null, }, city: { fieldId: "city",label: "City",labelField: "name",valueField: "cityId", parents: ["country", "state"],list: [],selectedItem: {}, selectedValue: null,onValueSelected: () => null, }, village: { fieldId: "village",label: "Village",labelField: "name", valueField: "villageId", parents: ["country", "state", "city"], list: [],selectedItem: {},selectedValue: null, onValueSelected: () => null, }, street: { fieldId: "street",label: "Street",labelField: "name", valueField: "streetId", parents: ["country", "state", "city", "village"], list: [],selectedItem: {},selectedValue: null,onValueSelected: () => null, }, }; . . . export default function App() { . . . }
All these properties of a field have benefits, will be useful to handle dynamic rendering.
- fieldId ID of the field
- label Diaplay name of the field
- labelField denotes the dropdown label field in a dropdown list array
- valueField denotes the dropdown value field
- parents array of parent fields, will be used for validations
- list dropdown list array
- selectedItem whole selected item object of dropdown
- selectedValue selected value
- onValueSelected it is a function property, will be used/called when dropdown value selected/changed. Initially assigned as an empty method.
Dynamic Field rendering
By iterating the form field object keys we render dropdown fields dynamically, all the required properties are available in the form field object.
export default function App() { . . . return ( <View> <p>handle focus / blur<br> </p> <pre class="brush:php;toolbar:false">var focusField = ""; export default function App() { . . . const changeFocusField = (fld = "") => { focusField = fld; setRefreshPage(!refreshPage); }; . . . }
<ZDropDown . . . isFocus={focusField === ele} onFocus={() => { changeFocusField(ele); }} onBlur={() => changeFocusField("")} onChange={(item) => {}} />
Sample Data
Sample data for fields country, state, city, village and street .
const listCountry = [ { countryId: "1", name: "india" }, { countryId: "2", name: "uk" }, { countryId: "3", name: "canada" }, { countryId: "4", name: "us" }, ]; const listSate = [ { stateId: "1", countryId: "1", name: "state1_india" }, { stateId: "4", countryId: "2", name: "state1_uk" }, { stateId: "7", countryId: "3", name: "state1_canada" }, { stateId: "10", countryId: "4", name: "state1_us" }, ]; const listCity = [ { cityId: "1", stateId: "1", countryId: "1", name: "city1_state1_country1" }, { cityId: "5", stateId: "2", countryId: "1", name: "city5_state2_country1" }, { cityId: "21",stateId: "7",countryId: "3",name:"city21_state7_country3", }, { cityId: "26",stateId: "9",countryId: "3",name: "city26_state9_country3", }, ]; const listVillage = [ { cityId: "1", villageId: "1", name: "village 1 city 1" }, { cityId: "2", villageId: "5", name: "village 5 city 2" }, { cityId: "3", villageId: "9", name: "village 9 city 3" }, { cityId: "4", villageId: "10", name: "village 10 city 4" }, ]; const listStreet = [ { villageId: "1", streetId: "1", name: "village 1 street 1" }, { villageId: "1", streetId: "109", name: "village 1 street 109" }, { villageId: "2", streetId: "2", name: "village 2 street 2" }, { villageId: "2", streetId: "110", name: "village 2 street 110" }, ]; . . . export default function App() { . . . } . . .
OnPage Load
Firstly in the functionality side we have to setup some important things. Remember we assigned an empty method to the property onValueSelected, now it's time to assign the actual method. So we need to create 5 methods and assign them to the respective form field.
export default function App() { . . . const allValuesSelected = () => { console.log("All fields value selected"); }; const loadStreet = async () => {}; const loadVillage = async () => {}; const loadCity = async () => {}; const loadState = async () => {}; const loadCountry = async () => {}; const loadPageData = () => { formFields.country.onValueSelected = loadState; formFields.state.onValueSelected = loadCity; formFields.city.onValueSelected = loadVillage; formFields.village.onValueSelected = loadStreet; formFields.street.onValueSelected = allValuesSelected; }; return (. . .); }
When Country value selected, the STATE list has to be loaded, that's why here loadState method assigned to Country's onValueSelected. And likewise other methods assigned.
useEffect(() => { loadPageData(); }, []); return (. . .);
Load Country
load country list from sample data and call it on initial page load.
const loadCountry = async () => { formFields.country.list = [...listCountry]; setRefreshPage(!refreshPage); }; const loadPageData = () => { formFields.country.onValueSelected = loadState; formFields.state.onValueSelected = loadCity; formFields.city.onValueSelected = loadVillage; formFields.village.onValueSelected = loadStreet; formFields.street.onValueSelected = allValuesSelected; loadCountry(); };
OnChange
When dropdown field value selected, we need to setup the respective form field value, remove focus and load the next dropdown list.
return ( . . . <ZDropDown // . . . onChange={(item) => { fld.selectedItem = item; fld.selectedValue = item[fld.valueField]; focusField = ""; fld.onValueSelected(); }} /> . . . );
onValueSelected is useful right ?
Load State
When the first dropdown(country) changed, rest of the fields will be changed. So need to clear the list and data of all other form fields. For this we write a method which can clear the values from the given field to the end field.
import React, { useState, useEffect } from "react"; import { ScrollView, View, Text, StyleSheet, TouchableOpacity } from "react-native"; import { Dropdown } from "react-native-element-dropdown"; import { Snackbar } from "react-native-paper"; var snackMsg = ""; export default function App() { const [refreshPage, setRefreshPage] = useState(false); const [visible, setVisible] = useState(false); const onToggleSnackBar = () => setVisible(!visible); const onDismissSnackBar = () => setVisible(false); const resetForm = () => { }; return ( <ScrollView> <p>refreshPage state variable is used to refresh the page in all the situations. </p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173363402014343.jpg" class="lazy" alt="Cascading Form React Native Advanced" /></p> <p>Now, These fields are going to be converted as dynamic. </p> <h3> Form Field Object </h3> <p>Previously we had 3 separate objects for 3 different fields, but here all fields data placed under one form field object.<br> </p> <pre class="brush:php;toolbar:false"> . . . const formFields = { country: { fieldId: "country", label: "Country", labelField: "name", valueField: "countryId", parents: [], list: [], selectedItem: {}, selectedValue: null, onValueSelected: () => null, }, state: { fieldId: "state",label: "State",labelField: "name",valueField: "stateId", parents: ["country"],list: [],selectedItem: {},selectedValue: null, onValueSelected: () => null, }, city: { fieldId: "city",label: "City",labelField: "name",valueField: "cityId", parents: ["country", "state"],list: [],selectedItem: {}, selectedValue: null,onValueSelected: () => null, }, village: { fieldId: "village",label: "Village",labelField: "name", valueField: "villageId", parents: ["country", "state", "city"], list: [],selectedItem: {},selectedValue: null, onValueSelected: () => null, }, street: { fieldId: "street",label: "Street",labelField: "name", valueField: "streetId", parents: ["country", "state", "city", "village"], list: [],selectedItem: {},selectedValue: null,onValueSelected: () => null, }, }; . . . export default function App() { . . . }
This method can be used for all other dropdown fields and page resetting purpose.
export default function App() { . . . return ( <View> <p>handle focus / blur<br> </p> <pre class="brush:php;toolbar:false">var focusField = ""; export default function App() { . . . const changeFocusField = (fld = "") => { focusField = fld; setRefreshPage(!refreshPage); }; . . . }
State dropdown list is now perfectly loaded.
Load the rest (City, Village, Street)
As like before we load data for rest of the fields.
<ZDropDown . . . isFocus={focusField === ele} onFocus={() => { changeFocusField(ele); }} onBlur={() => changeFocusField("")} onChange={(item) => {}} />
good, all the dropdowns populated with respective list.
Validation
Before showing the dropdown list we need to validate its parent field. So we are going to get the parent fields from the form field object. Then iterate them one by one, validate its value and show warning if necessary.
const listCountry = [ { countryId: "1", name: "india" }, { countryId: "2", name: "uk" }, { countryId: "3", name: "canada" }, { countryId: "4", name: "us" }, ]; const listSate = [ { stateId: "1", countryId: "1", name: "state1_india" }, { stateId: "4", countryId: "2", name: "state1_uk" }, { stateId: "7", countryId: "3", name: "state1_canada" }, { stateId: "10", countryId: "4", name: "state1_us" }, ]; const listCity = [ { cityId: "1", stateId: "1", countryId: "1", name: "city1_state1_country1" }, { cityId: "5", stateId: "2", countryId: "1", name: "city5_state2_country1" }, { cityId: "21",stateId: "7",countryId: "3",name:"city21_state7_country3", }, { cityId: "26",stateId: "9",countryId: "3",name: "city26_state9_country3", }, ]; const listVillage = [ { cityId: "1", villageId: "1", name: "village 1 city 1" }, { cityId: "2", villageId: "5", name: "village 5 city 2" }, { cityId: "3", villageId: "9", name: "village 9 city 3" }, { cityId: "4", villageId: "10", name: "village 10 city 4" }, ]; const listStreet = [ { villageId: "1", streetId: "1", name: "village 1 street 1" }, { villageId: "1", streetId: "109", name: "village 1 street 109" }, { villageId: "2", streetId: "2", name: "village 2 street 2" }, { villageId: "2", streetId: "110", name: "village 2 street 110" }, ]; . . . export default function App() { . . . } . . .
Reset Form
Finally, we provide option to reset the form fields.
export default function App() { . . . const allValuesSelected = () => { console.log("All fields value selected"); }; const loadStreet = async () => {}; const loadVillage = async () => {}; const loadCity = async () => {}; const loadState = async () => {}; const loadCountry = async () => {}; const loadPageData = () => { formFields.country.onValueSelected = loadState; formFields.state.onValueSelected = loadCity; formFields.city.onValueSelected = loadVillage; formFields.village.onValueSelected = loadStreet; formFields.street.onValueSelected = allValuesSelected; }; return (. . .); }
All Done. Now we saw how to deal with dynamic form fields, rendering fields, loading data and validating them.
These are the 3 approaches of mine to handle cascading form fields.
Hope this post/series has some useful stuffs which you like. Thank you.
Full code here
The above is the detailed content of Cascading Form React Native Advanced. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.
