-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added country selector component #59
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Work.
Just a few changes to add more controll and changability to the component to match the needs of the design.
Also address the linter errors
label: string; | ||
}; | ||
|
||
export const CountrySelector: React.FC = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have this taking in the p[ace holder text as a prop since place holder text changes depending on design
}; | ||
|
||
export const CountrySelector: React.FC = () => { | ||
const [value, setValue] = useState<CountryOption | null>(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state should be controled outside the component
i.e. we pass in the value and the set function this way we know the values when we use the component in the form
Something similar to this
import Select from "react-select";
import countryList from "react-select-country-list";
const CountrySelector = ({ value, onChange }) => {
const options = useMemo(() => countryList().getData(), []);
return (
<Select
options={options}
value={options.find((option) => option.value === value)}
onChange={(selected) => onChange(selected?.value)} // Pass value to parent
isSearchable
/>
);
};
export default CountrySelector;```
If you want to learn more its controlled vs uncontrolled components https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components
|
||
return ( | ||
<div> | ||
<Select |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right now the element size changes depending on the content. We need to be able to control its width so that we can match it to the design
Two option:
- pass in a width prop which is then used to style the comopnent
- Find a way so it takes the full width of the parent component i.e.
<div> <CountrySelector/> </div>
so that the element size will change depending on the size of div.
} | ||
}, []); | ||
|
||
const changeHandler = (selectedOption: SingleValue<CountryOption>) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap this in a use Callback
I did something like this and it went away since every rerender its creating this function which we also don't really want to do that we
const changeHandler = useCallback( (selectedOption: SingleValue<CountryOption>) => { setValue(selectedOption ?? null); }, [setValue] );
Tracking Info
Resolves #<45>
Changes
I added a country selector component using the react-select and react-select-country-list libraries.
Testing
I put the country selector on the page.tsx and tested to see if it showed up.
Confirmation of Change