Merged in feat/SW-3076 (pull request #2416)

feat(SW-3076: change phone country code on country select)

* feat(SW-3076: change phone country code on country select)


Approved-by: Simon.Emanuelsson
This commit is contained in:
Arvid Norlin
2025-06-24 09:47:18 +00:00
parent 1cbf525bf7
commit 438de66a1f
3 changed files with 34 additions and 2 deletions

View File

@@ -85,6 +85,8 @@ export default function Details() {
control, control,
subscribe, subscribe,
formState: { isValid }, formState: { isValid },
setValue,
watch,
} = methods } = methods
useFormTracking("checkout", subscribe, control, ` - room ${roomNr}`) useFormTracking("checkout", subscribe, control, ` - room ${roomNr}`)
@@ -119,6 +121,14 @@ export default function Details() {
} }
}, [crossValidationData, methods]) }, [crossValidationData, methods])
const countryCode = watch("countryCode")
useEffect(() => {
if (countryCode) {
setValue("phoneNumberCC", countryCode.toLowerCase())
}
}, [countryCode, setValue])
const guestIsGoingToJoin = methods.watch("join") const guestIsGoingToJoin = methods.watch("join")
const guestIsMember = methods.watch("membershipNo") const guestIsMember = methods.watch("membershipNo")

View File

@@ -42,6 +42,7 @@ export default function Details({ user }: DetailsProps) {
roomNr, roomNr,
idx, idx,
} = useRoomContext() } = useRoomContext()
const initialData = room.guest const initialData = room.guest
const memberRate = "member" in room.roomRate ? room.roomRate.member : null const memberRate = "member" in room.roomRate ? room.roomRate.member : null
@@ -74,7 +75,15 @@ export default function Details({ user }: DetailsProps) {
reValidateMode: "onChange", reValidateMode: "onChange",
}) })
const { formState, handleSubmit, trigger, control, subscribe } = methods const {
formState,
handleSubmit,
trigger,
control,
subscribe,
setValue,
watch,
} = methods
useFormTracking( useFormTracking(
"checkout", "checkout",
@@ -103,7 +112,13 @@ export default function Details({ user }: DetailsProps) {
}, [handleSubmit, formState.isValid, onSubmit, setIncomplete]) }, [handleSubmit, formState.isValid, onSubmit, setIncomplete])
useEffect(updateDetailsStore, [updateDetailsStore]) useEffect(updateDetailsStore, [updateDetailsStore])
const countryCode = watch("countryCode")
useEffect(() => {
if (countryCode) {
setValue("phoneNumberCC", countryCode.toLowerCase())
}
}, [countryCode, setValue])
return ( return (
<FormProvider {...methods}> <FormProvider {...methods}>
<form <form

View File

@@ -1,6 +1,7 @@
"use client" "use client"
import "react-international-phone/style.css" import "react-international-phone/style.css"
import { useEffect } from "react"
import { TextField } from "react-aria-components" import { TextField } from "react-aria-components"
import { useFormContext, useWatch } from "react-hook-form" import { useFormContext, useWatch } from "react-hook-form"
import { import {
@@ -42,6 +43,7 @@ export default function Phone({
const lang = useLang() const lang = useLang()
const { formState, getFieldState, register, setValue } = useFormContext() const { formState, getFieldState, register, setValue } = useFormContext()
const fieldState = getFieldState(name) const fieldState = getFieldState(name)
const ccFieldState = getFieldState(countrySelectorName)
const [phoneNumber, phoneNumberCC]: [string, string] = useWatch({ const [phoneNumber, phoneNumberCC]: [string, string] = useWatch({
name: [name, countrySelectorName], name: [name, countrySelectorName],
}) })
@@ -56,13 +58,18 @@ export default function Phone({
setCountry(value.iso2) setCountry(value.iso2)
setValue(countrySelectorName, value.iso2, { setValue(countrySelectorName, value.iso2, {
shouldDirty: true, shouldDirty: true,
shouldTouch: true,
shouldValidate: true, shouldValidate: true,
}) })
if (registerOptions.onBlur) { if (registerOptions.onBlur) {
registerOptions.onBlur(value) registerOptions.onBlur(value)
} }
} }
useEffect(() => {
if (!ccFieldState.isTouched) {
setCountry(phoneNumberCC)
}
}, [phoneNumberCC, setCountry, ccFieldState])
return ( return (
<div className={`${styles.phone} ${className}`}> <div className={`${styles.phone} ${className}`}>
<CountrySelector <CountrySelector