Guest User

Untitled

a guest
Nov 2nd, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. // callback для получения данных стран/региона/города
  2.  
  3. const loadOptions = (inputValue, callback) => {
  4. dummy.get(`country?CountrySearch[query]=${inputValue}`)
  5. .then((response) => {
  6. const options = []
  7. response.data.data.forEach((permission) => {
  8. options.push({
  9. label: permission.name,
  10. value: permission.id
  11. })
  12. })
  13. callback(options);
  14. })
  15. }
  16.  
  17. const loadOptions2 = (inputValue, callback) => {
  18. dummy.get(`region?RegionSearch[query]=${inputValue}`)
  19. .then((response) => {
  20. const options = []
  21. response.data.data.forEach((permission) => {
  22. options.push({
  23. label: permission.name,
  24. value: permission.country_id
  25. })
  26. })
  27. callback(options);
  28. })
  29. }
  30.  
  31. const loadOptions3 = (inputValue, callback) => {
  32. dummy.get(`city?CitySearch[query]=${inputValue}`)
  33. .then((response) => {
  34. const options = []
  35. response.data.data.forEach((permission) => {
  36. options.push({
  37. label: permission.name,
  38. value: permission.region_id
  39. })
  40. })
  41. callback(options);
  42. })
  43. }
  44.  
  45.  
  46. //Тут Селекты которые нужно связать
  47.  
  48. function SelectsDrop() {
  49. const [selectedCountry, setSelectedCountry] = useState('');
  50. const [selectedRegion, setSelectedRegion] = useState('');
  51. const [selectedCity, setSelectedCity] = useState('');
  52.  
  53.  
  54.  
  55. const handleCity = value =>{
  56. setSelectedCity(value)
  57. }
  58.  
  59. const handleRegion = value =>{
  60. setSelectedRegion(value)
  61. }
  62.  
  63. const handleCountry = value =>{
  64. setSelectedCountry(value)
  65. }
  66.  
  67. return (
  68. <div className="city__select">
  69. {JSON.stringify(selectedCountry.value)},
  70. {JSON.stringify(selectedRegion.value)},
  71. {JSON.stringify(selectedCity.value)}
  72. <AsyncSelect
  73. components={{DropdownIndicator}}
  74. placeholder={"Выбор страны"}
  75. cacheOptions
  76. defaultOptions
  77. styles={customStyles}
  78. value={selectedCountry}
  79. onChange={handleCountry}
  80. loadOptions={loadOptions}
  81. />
  82.  
  83. <AsyncSelect
  84. components={{DropdownIndicator}}
  85. placeholder={"Выбрать регион"}
  86. cacheOptions
  87. defaultOptions
  88. styles={customStyles}
  89. value={selectedRegion}
  90. onChange={handleRegion}
  91. loadOptions={loadOptions2}
  92. />
  93.  
  94. <AsyncSelect
  95. components={{DropdownIndicator}}
  96. placeholder={"Город"}
  97. cacheOptions
  98. defaultOptions
  99. styles={customStyles}
  100. value={selectedCity}
  101. onChange={handleCity}
  102. loadOptions={loadOptions3}
  103. />
  104. </div>
  105. )
  106.  
  107. }
  108. export default SelectsDrop;
Advertisement
Add Comment
Please, Sign In to add comment