Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

Refactor phone input component

neilk17be3fb1e68bb6
diff --git a/apps/carbon/app/components/Form/PhoneInput.tsx b/apps/carbon/app/components/Form/PhoneInput.tsx
index 8881501d73..a416bdd863 100644
--- a/apps/carbon/app/components/Form/PhoneInput.tsx
+++ b/apps/carbon/app/components/Form/PhoneInput.tsx
@@ -16,6 +16,7 @@ import {
CommandItem,
CommandList,
FormControl,
+ FormErrorMessage,
FormLabel,
Input,
Popover,
@@ -24,6 +25,7 @@ import {
cn,
} from "@carbon/react";
import { RxCaretSort, RxCheck } from "react-icons/rx";
+import { useField } from "remix-validated-form";
const PhoneInputComponent = ReactPhoneInput.default;
type PhoneInputProps = InputProps & {
@@ -40,42 +42,44 @@ const PhoneInput: React.ForwardRefExoticComponent<PhoneInputProps> =
React.forwardRef<
React.ElementRef<typeof ReactPhoneInput.default>,
PhoneInputProps
- >(({ name, label, isRequired, className, ...props }, ref) => (
- <FormControl isRequired={isRequired}>
- <FormLabel htmlFor={name}>{label}</FormLabel>
- <PhoneInputComponent
- ref={ref}
- className={cn("flex", className)}
- flagComponent={FlagComponent}
- countrySelectComponent={CountrySelect}
- inputComponent={InputComponent}
- /**
- * Handles the onChange event.
- *
- * react-phone-number-input might trigger the onChange event as undefined
- * when a valid phone number is not entered. To prevent this,
- * the value is coerced to an empty string.
- *
- * @param {E164Number | undefined} value - The entered value
- */
- onChange={(value) =>
- props.onChange ? props.onChange(value || "") : undefined
- }
- // onChange={(value) => onchange(value || "")}
- {...props}
- />
- </FormControl>
- ));
+ >(({ name, label, isRequired, className, ...props }, ref) => {
+ const { getInputProps, error } = useField(name);
+ return (
+ <FormControl isInvalid={!!error} isRequired={isRequired}>
+ {label && <FormLabel htmlFor={name}>{label}</FormLabel>}
+ <PhoneInputComponent
+ ref={ref}
+ className={cn("flex", className)}
+ flagComponent={FlagComponent}
+ countrySelectComponent={CountrySelect}
+ inputComponent={InputComponent}
+ value={getInputProps({ id: name, ...props }).defaultValue}
+ {...getInputProps({
+ id: name,
+ ...props,
+ })}
+ /**
+ * Handles the onChange event.
+ *
+ * react-phone-number-input might trigger the onChange event as undefined
+ * when a valid phone number is not entered. To prevent this,
+ * the value is coerced to an empty string.
+ *
+ * @param {E164Number | undefined} value - The entered value
+ */
+ onChange={(value) =>
+ props.onChange ? props.onChange(value || "") : undefined
+ }
+ {...props}
+ />
+ {error && <FormErrorMessage>{error}</FormErrorMessage>}
+ </FormControl>
+ );
+ });
PhoneInput.displayName = "PhoneInput";
const InputComponent = React.forwardRef<HTMLInputElement, InputProps>(
- ({ className, ...props }, ref) => (
- <Input
- className={cn("rounded-s-none rounded-e-lg", className)}
- {...props}
- ref={ref}
- />
- )
+ ({ className, ...props }, ref) => <Input {...props} ref={ref} />
);
InputComponent.displayName = "InputComponent";
@@ -107,7 +111,7 @@ const CountrySelect = ({
<Button
variant={"ghost"}
className={cn(
- "py-1 flex gap-1 h-full rounded-e-none rounded-s-lg pr-1 pl-3"
+ "py-1 border flex gap-1 h-full rounded-e-none rounded-s-lg pr-1 pl-3"
)}
disabled={disabled}
>
diff --git a/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx b/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
index 16e864d2c6..dfd97509f1 100644
--- a/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
+++ b/apps/carbon/app/modules/sales/ui/Customer/CustomerContactForm.tsx
@@ -15,7 +15,6 @@ import {
DatePicker,
Hidden,
Input,
- Phone,
PhoneInput,
Submit,
TextArea,
@@ -75,10 +74,9 @@ const CustomerContactForm = ({ initialValues }: CustomerContactFormProps) => {
<Input name="email" label="Email" />
<Input name="title" label="Title" />
<PhoneInput name="mobilePhone" label="Mobile Phone" />
- <Phone name="mobilePhone" label="Mobile Phone" />
- <Phone name="homePhone" label="Home Phone" />
- <Phone name="workPhone" label="Work Phone" />
- <Phone name="fax" label="Fax" />
+ <PhoneInput name="homePhone" label="Home Phone" />
+ <PhoneInput name="workPhone" label="Work Phone" />
+ <PhoneInput name="fax" label="Fax" />
<Input name="addressLine1" label="Address Line 1" />
<Input name="addressLine2" label="Address Line 2" />
<Input name="city" label="City" />