Skip to content

Commit

Permalink
refactor fields
Browse files Browse the repository at this point in the history
  • Loading branch information
yurytut1993 committed May 10, 2024
1 parent 99c2bea commit adfe121
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface PasswordProps {

export const Password = ({ field, isValid, name, onChange }: PasswordProps) => {
const t = useTranslations('Account.Register.validationMessages');
const fieldName = FieldNameToFieldId[field.entityId];

return (
<Field className="relative space-y-2 pb-7" name={name}>
Expand All @@ -45,7 +46,7 @@ export const Password = ({ field, isValid, name, onChange }: PasswordProps) => {
{t('password')}
</FieldMessage>
)}
{FieldNameToFieldId[field.entityId] === 'confirmPassword' && (
{fieldName === 'confirmPassword' && (
<FieldMessage
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error-secondary"
match={() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useTranslations } from 'next-intl';

import { Field, FieldControl, FieldLabel, FieldMessage } from '~/components/ui/form';
import { Field, FieldControl, FieldLabel } from '~/components/ui/form';
import { Input } from '~/components/ui/input';
import { Select, SelectContent, SelectItem } from '~/components/ui/select';

import { AddressFields, FieldNameToFieldId } from '..';
import { AddressFields } from '..';

type PicklistOrTextType = Extract<
NonNullable<AddressFields>[number],
Expand All @@ -20,8 +18,6 @@ interface PicklistOrTextProps {
}

export const PicklistOrText = ({ defaultValue, field, name, options }: PicklistOrTextProps) => {
const t = useTranslations('Account.Register');

return (
<Field className="relative space-y-2 pb-7" name={name}>
<FieldLabel
Expand All @@ -43,26 +39,17 @@ export const PicklistOrText = ({ defaultValue, field, name, options }: PicklistO
required={field.isRequired}
>
<SelectContent position="item-aligned">
{field.entityId === FieldNameToFieldId.stateOrProvince &&
options.map(({ entityId, label }) => {
return (
<SelectItem key={entityId} value={entityId.toString()}>
{label}
</SelectItem>
);
})}
{options.map(({ entityId, label }) => {
return (
<SelectItem key={entityId} value={entityId.toString()}>
{label}
</SelectItem>
);
})}
</SelectContent>
</Select>
)}
</FieldControl>
{field.isRequired && options.length === 0 && (
<FieldMessage
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error-secondary"
match="valueMissing"
>
{t('emptyTextValidatoinMessage')}
</FieldMessage>
)}
</Field>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ export const Picklist = ({ defaultValue, field, name, onChange, options }: Pickl
required={field.isRequired}
>
<SelectContent position="item-aligned">
{field.entityId === FieldNameToFieldId.countryCode &&
options.map(({ label, entityId }) => (
<SelectItem key={entityId} value={entityId.toString()}>
{label}
</SelectItem>
))}
{options.map(({ entityId, label }) => (
<SelectItem key={entityId} value={entityId.toString()}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</FieldControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface TextProps {

export const Text = ({ field, isValid, name, onChange, type }: TextProps) => {
const t = useTranslations('Account.Register.validationMessages');
const fieldName = FieldNameToFieldId[field.entityId];

return (
<Field className="relative space-y-2 pb-7" name={name}>
Expand All @@ -43,10 +44,10 @@ export const Text = ({ field, isValid, name, onChange, type }: TextProps) => {
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error-secondary"
match="valueMissing"
>
{t(FieldNameToFieldId[field.entityId] ?? 'empty')}
{t(fieldName ?? 'empty')}
</FieldMessage>
)}
{FieldNameToFieldId[field.entityId] === 'email' && (
{fieldName === 'email' && (
<FieldMessage
className="absolute inset-x-0 bottom-0 inline-flex w-full text-xs font-normal text-error-secondary"
match="typeMismatch"
Expand Down

0 comments on commit adfe121

Please sign in to comment.