Skip to content

Commit

Permalink
fix: refactor CustomRegisterField.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski committed Jul 31, 2023
1 parent 6715cae commit f04af0e
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/components/CustomRegisterField/CustomRegisterField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,22 @@ export const CustomRegisterField: FC<Props> = ({ type, name, value = '', onChang
const { t } = useTranslation(type);

const optionsList = useMemo(() => {
const optionsObject = (() => {
switch (type) {
case REGISTER_FIELD_VARIANT.COUNTRY_SELECT:
return countriesCodes;
case REGISTER_FIELD_VARIANT.US_STATE_SELECT:
return usStatesCodes;
default:
return null;
switch (type) {
case REGISTER_FIELD_VARIANT.COUNTRY_SELECT:
case REGISTER_FIELD_VARIANT.US_STATE_SELECT: {
return (type === REGISTER_FIELD_VARIANT.COUNTRY_SELECT ? countriesCodes : usStatesCodes).map((code) => ({
value: code,
label: t(`${type}:${code}`),
}));
}
})();
default: {
if (props.options) {
return Object.entries(props.options).map(([value, label]) => ({ value, label }));
}

if (optionsObject) {
return optionsObject.map((countryCode) => ({
value: countryCode,
label: t(`${type}:${countryCode}`),
}));
}

if (!props.options) {
return [];
return [];
}
}

return Object.entries(props.options).map(([value, label]) => ({ value, label }));
}, [t, type, props.options]);

const changeHandler: ChangeEventHandler<HTMLInputElement> = useCallback(
Expand All @@ -80,9 +73,9 @@ export const CustomRegisterField: FC<Props> = ({ type, name, value = '', onChang
return <Dropdown {...commonProps} options={optionsList} value={value as string} defaultLabel={props.placeholder} fullWidth />;
case REGISTER_FIELD_VARIANT.DATE_PICKER:
return <DateField {...commonProps} value={value as string} onChange={(dateString: string) => onChange(name, dateString)} />;
default:
return null;
}

return null;
};

export default CustomRegisterField;

0 comments on commit f04af0e

Please sign in to comment.