hossein-zare / react-native-dropdown-picker

A single / multiple, categorizable, customizable, localizable and searchable item picker (drop-down) component for react native which supports both Android & iOS.
https://hossein-zare.github.io/react-native-dropdown-picker-website/
MIT License
971 stars 295 forks source link

Can you do a cascading / dependent dropdown with this dropdown picker? #563

Closed ahmadaba14 closed 2 years ago

ahmadaba14 commented 2 years ago

Hi, basically as shown in the gif, this is what I'm trying to do but on react native, but can't think of any solution to it. Its a cascading dropdown where the dropdown is dependent to the data before that. cascading I've seen some codes of React JS but nothing specific for React Native. So if anyone can help me on this, that would be great. Thanks.

hossein-zare commented 2 years ago

Hi,


const [country, setCountry] = useState(null);
const [countries, setCountries] = useState([
  {label: 'USA', value: 'usa'},
]);

const [state, setState] = useState(null);
const [states, setStates] = useState([]);

const [city, setCity] = useState(null);
const [cities, setCities] = useState([]);

const countryChanged = (value) => {
  setState(null);
  setStates([]); // Add states based on the value (country)

  setCity(null);
  setCities([]);
};

const stateChanged = (value) => {
  setCity(null);
  setCities([]);  // Add cities based on the value (state)
};

<DropDownPicker
  value={country}
  setValue={setCountry}
  items={countries}
  setItems={setCountries}
  onChangeValue={countryChanged}
  ...
/>

<DropDownPicker
  value={state}
  setValue={setState}
  items={states}
  setItems={setStates}
  onChangeValue={stateChanged}
  ...
/>

<DropDownPicker
  value={city}
  setValue={setCity}
  items={cities}
  setItems={setCities}
  ...
/>