From 01188b3104fe3999e3def5e7d414430a02c6e7ae Mon Sep 17 00:00:00 2001 From: Kabilan Sakthivelu <88023922+kabilansakthivelu@users.noreply.github.com> Date: Mon, 18 Nov 2024 10:10:33 +0530 Subject: [PATCH] fix: geo location dropdown bugfixes in playground (#181) * fix: geo location dropdown bugfixes in playground * feat: added 'PlaceholderMenuItem' component to display in case of no records for a dropdown list --- .../src/components/countryDropdown/index.jsx | 34 +++++++++-------- .../components/placeholderMenuItem/index.js | 1 + .../placeholderMenuItem.jsx | 11 ++++++ .../src/components/stateDropdown/index.jsx | 34 +++++++++-------- .../src/constants/geo/index.js | 1 + .../src/pages/geo/getCities.jsx | 37 ++++++++++++------- .../src/pages/geo/getStates.jsx | 6 ++- .../src/pages/geo/getZipcodes.jsx | 37 ++++++++++++------- 8 files changed, 101 insertions(+), 60 deletions(-) create mode 100644 packages/i18nify-playground/src/components/placeholderMenuItem/index.js create mode 100644 packages/i18nify-playground/src/components/placeholderMenuItem/placeholderMenuItem.jsx diff --git a/packages/i18nify-playground/src/components/countryDropdown/index.jsx b/packages/i18nify-playground/src/components/countryDropdown/index.jsx index 1d06fc39..73a018c6 100644 --- a/packages/i18nify-playground/src/components/countryDropdown/index.jsx +++ b/packages/i18nify-playground/src/components/countryDropdown/index.jsx @@ -2,6 +2,8 @@ import { Box, MenuItem, Select, Typography } from '@mui/material'; import { getAllCountries } from '@razorpay/i18nify-js'; import React, { useEffect, useState } from 'react'; +import PlaceholderMenuItem from 'src/components/placeholderMenuItem'; + const CountryDropdown = ({ label = 'Select Country', value, @@ -38,21 +40,23 @@ const CountryDropdown = ({ ...selectStyle, }} > - {countryList.map((country) => ( - - - - {country.code} - {country.country_name} - - - - ))} + {countryList.length === 0 ? + : + countryList.map((country) => ( + + + + {country.code} - {country.country_name} + + + + ))} > ); diff --git a/packages/i18nify-playground/src/components/placeholderMenuItem/index.js b/packages/i18nify-playground/src/components/placeholderMenuItem/index.js new file mode 100644 index 00000000..8d698b61 --- /dev/null +++ b/packages/i18nify-playground/src/components/placeholderMenuItem/index.js @@ -0,0 +1 @@ +export { default } from './placeholderMenuItem'; diff --git a/packages/i18nify-playground/src/components/placeholderMenuItem/placeholderMenuItem.jsx b/packages/i18nify-playground/src/components/placeholderMenuItem/placeholderMenuItem.jsx new file mode 100644 index 00000000..c5b23911 --- /dev/null +++ b/packages/i18nify-playground/src/components/placeholderMenuItem/placeholderMenuItem.jsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { MenuItem } from '@mui/material'; + +import { MENU_ITEM_PLACEHOLDER_CONTENT } from 'src/constants/geo'; + +const PlaceholderMenuItem = (props) => { + const { content = MENU_ITEM_PLACEHOLDER_CONTENT } = props; + return {content}; +}; + +export default PlaceholderMenuItem; diff --git a/packages/i18nify-playground/src/components/stateDropdown/index.jsx b/packages/i18nify-playground/src/components/stateDropdown/index.jsx index 82ca68b9..bc42ffb5 100644 --- a/packages/i18nify-playground/src/components/stateDropdown/index.jsx +++ b/packages/i18nify-playground/src/components/stateDropdown/index.jsx @@ -1,6 +1,8 @@ import { Box, MenuItem, Select, Typography } from '@mui/material'; import React from 'react'; +import PlaceholderMenuItem from 'src/components/placeholderMenuItem'; + const StateDropdown = ({ label = 'Select State', list, onChange, value }) => { return ( <> @@ -16,21 +18,23 @@ const StateDropdown = ({ label = 'Select State', list, onChange, value }) => { mb: 4, }} > - {list.map((state) => ( - - - - {state.code} - {state.name} - - - - ))} + {list.length === 0 ? + : + list.map((state) => ( + + + + {state.code} - {state.name} + + + + ))} > ); diff --git a/packages/i18nify-playground/src/constants/geo/index.js b/packages/i18nify-playground/src/constants/geo/index.js index cb2acf6d..32c6fa8f 100644 --- a/packages/i18nify-playground/src/constants/geo/index.js +++ b/packages/i18nify-playground/src/constants/geo/index.js @@ -1 +1,2 @@ export const ALLOWED_COUNTRIES = ['IN', 'MY', 'FR', 'DE', 'US']; +export const MENU_ITEM_PLACEHOLDER_CONTENT = 'No data available'; diff --git a/packages/i18nify-playground/src/pages/geo/getCities.jsx b/packages/i18nify-playground/src/pages/geo/getCities.jsx index b26b2564..22597517 100644 --- a/packages/i18nify-playground/src/pages/geo/getCities.jsx +++ b/packages/i18nify-playground/src/pages/geo/getCities.jsx @@ -14,6 +14,7 @@ import { ALLOWED_COUNTRIES } from 'src/constants/geo'; import CodeEditor from 'src/components/codeEditor'; import CountryDropdown from 'src/components/countryDropdown'; import StateDropdown from 'src/components/stateDropdown'; +import PlaceholderMenuItem from 'src/components/placeholderMenuItem'; // ---------------------------------------------------------------------- @@ -38,6 +39,9 @@ export default function GetCities() { useEffect(() => { setStateInp(''); + setStateList([]); + setCityInp(''); + setCities([]); setCode(''); getStates(countryInp).then((res) => { const states = Object.entries(res).map(([_code, state]) => ({ @@ -50,6 +54,8 @@ export default function GetCities() { useEffect(() => { if (!stateInp) return; + setCityInp(''); + setCities([]); getCities(countryInp, stateInp).then((res) => { setCities(res); setCityInp(res[0].name); @@ -106,7 +112,7 @@ export default function GetCities() { /> setStateInp(e)} + onChange={(state) => setStateInp(state)} list={stateList} /> @@ -119,20 +125,23 @@ export default function GetCities() { marginRight: 1, width: '100%', }} + onChange={(e) => setCityInp(e.target.value || '')} > - {cities.map((city) => ( - - - {city.name} - - - ))} + {cities.length === 0 ? + : + cities.map((city) => ( + + + {city.name} + + + ))} {!isMobile && ( diff --git a/packages/i18nify-playground/src/pages/geo/getStates.jsx b/packages/i18nify-playground/src/pages/geo/getStates.jsx index a630ef37..bcd713b1 100644 --- a/packages/i18nify-playground/src/pages/geo/getStates.jsx +++ b/packages/i18nify-playground/src/pages/geo/getStates.jsx @@ -27,6 +27,8 @@ export default function GetStates() { }, []); useEffect(() => { + setStateVal(''); + setStateList([]); getStates(countryInp).then((res) => { setCode(JSON.stringify(res, null, 2)); const data = Object.keys(res).map((stateCode) => ({ @@ -80,13 +82,13 @@ export default function GetStates() { setCountryInp(e)} + onChange={(country) => setCountryInp(country)} /> setStateVal(e)} + onChange={(state) => setStateVal(state)} /> {!isMobile && ( diff --git a/packages/i18nify-playground/src/pages/geo/getZipcodes.jsx b/packages/i18nify-playground/src/pages/geo/getZipcodes.jsx index d7d93df5..20b80983 100644 --- a/packages/i18nify-playground/src/pages/geo/getZipcodes.jsx +++ b/packages/i18nify-playground/src/pages/geo/getZipcodes.jsx @@ -14,6 +14,7 @@ import { ALLOWED_COUNTRIES } from 'src/constants/geo'; import CodeEditor from 'src/components/codeEditor'; import CountryDropdown from 'src/components/countryDropdown'; import StateDropdown from 'src/components/stateDropdown'; +import PlaceholderMenuItem from 'src/components/placeholderMenuItem'; // ---------------------------------------------------------------------- @@ -38,6 +39,9 @@ export default function GetZipcodes() { useEffect(() => { setStateInp(''); + setStateList([]); + setZipcodeInp(''); + setZipcodes([]); setCode(''); getStates(countryInp).then((res) => { const states = Object.entries(res).map(([_code, state]) => ({ @@ -50,6 +54,8 @@ export default function GetZipcodes() { useEffect(() => { if (!stateInp) return; + setZipcodeInp(''); + setZipcodes([]); getZipcodes(countryInp, stateInp).then((res) => { setZipcodes(res); setZipcodeInp(res[0]); @@ -103,7 +109,7 @@ export default function GetZipcodes() { /> setStateInp(e)} + onChange={(state) => setStateInp(state)} list={stateList} /> @@ -116,20 +122,23 @@ export default function GetZipcodes() { marginRight: 1, width: '100%', }} + onChange={(e) => setZipcodeInp(e.target.value || '')} > - {zipcodes.map((zipcode) => ( - - - {zipcode} - - - ))} + {zipcodes.length === 0 ? + : + zipcodes.map((zipcode) => ( + + + {zipcode} + + + ))} {!isMobile && (