Skip to content

fix: phone input validation error should display red borders like email input #13275

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

Merged
merged 3 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type MultiItemBaseInputProps = Omit<HTMLInputProps, 'onChange'> & {
onChange: (value: string) => void;
autoFocus: HTMLInputProps['autoFocus'];
placeholder: HTMLInputProps['placeholder'];
hasError?: boolean;
}) => React.ReactNode;
error?: string | null;
hasError?: boolean;
Expand Down Expand Up @@ -134,6 +135,7 @@ export const MultiItemBaseInput = forwardRef<
onChange,
autoFocus,
placeholder,
hasError,
})
) : (
<StyledInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { usePhonesField } from '@/object-record/record-field/meta-types/hooks/usePhonesField';
import { PhonesFieldMenuItem } from '@/object-record/record-field/meta-types/input/components/PhonesFieldMenuItem';
import { recordFieldInputIsFieldInErrorComponentState } from '@/object-record/record-field/states/recordFieldInputIsFieldInErrorComponentState';
import { phoneSchema } from '@/object-record/record-field/validation-schemas/phoneSchema';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import styled from '@emotion/styled';
import { E164Number, parsePhoneNumber } from 'libphonenumber-js';
import ReactPhoneNumberInput from 'react-phone-number-input';
Expand All @@ -19,6 +22,7 @@ export const DEFAULT_PHONE_CALLING_CODE = '1';

const StyledCustomPhoneInputContainer = styled.div<{
hasItem: boolean;
hasError?: boolean;
}>`
${({ hasItem, theme }) =>
hasItem &&
Expand All @@ -28,6 +32,13 @@ const StyledCustomPhoneInputContainer = styled.div<{
border: 1px solid ${theme.border.color.medium};
height: 30px;
`}

${({ hasError, hasItem, theme }) =>
hasError &&
hasItem &&
css`
border: 1px solid ${theme.border.color.danger};
`}
`;

const StyledCustomPhoneInput = styled(ReactPhoneNumberInput)`
Expand Down Expand Up @@ -93,10 +104,23 @@ export const PhonesFieldInput = ({
});
};

const validateInput = (input: string) => ({
isValid: phoneSchema.safeParse(input).success,
errorMessage: '',
});

const getShowPrimaryIcon = (index: number) =>
index === 0 && phones.length > 1;
const getShowSetAsPrimaryButton = (index: number) => index > 0;

const setIsFieldInError = useSetRecoilComponentStateV2(
recordFieldInputIsFieldInErrorComponentState,
);

const handleError = (hasError: boolean, values: any[]) => {
setIsFieldInError(hasError && values.length === 0);
};

return (
<MultiItemFieldInput
items={phones}
Expand All @@ -105,6 +129,7 @@ export const PhonesFieldInput = ({
onCancel={onCancel}
placeholder="Phone"
fieldMetadataType={FieldMetadataType.PHONES}
validateInput={validateInput}
formatInput={(input) => {
const phone = parsePhoneNumber(input);
if (phone !== undefined) {
Expand Down Expand Up @@ -138,9 +163,12 @@ export const PhonesFieldInput = ({
onDelete={handleDelete}
/>
)}
renderInput={({ value, onChange, autoFocus, placeholder }) => {
renderInput={({ value, onChange, autoFocus, placeholder, hasError }) => {
return (
<StyledCustomPhoneInputContainer hasItem={!!phones.length}>
<StyledCustomPhoneInputContainer
hasItem={!!phones.length}
hasError={hasError}
>
<StyledCustomPhoneInput
autoFocus={autoFocus}
placeholder={placeholder}
Expand All @@ -154,6 +182,7 @@ export const PhonesFieldInput = ({
</StyledCustomPhoneInputContainer>
);
}}
onError={handleError}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { parsePhoneNumberWithError } from 'libphonenumber-js';
import { z } from 'zod';

export const phoneSchema = z.string().refine((value) => {
if (!value || value.trim() === '') return false;
try {
const phone = parsePhoneNumberWithError(value);
return phone.isValid();
} catch {
return false;
}
});