Merged in chore/SW-3145-move-country (pull request #2545)

chore: SW-3145 Moved country into design system

* chore: SW-3145 Moved country into design system


Approved-by: Anton Gunnarsson
This commit is contained in:
Hrishikesh Vaipurkar
2025-07-10 12:20:49 +00:00
parent 5f9af2701e
commit 2f72a0437b
27 changed files with 229 additions and 136 deletions

View File

@@ -18,7 +18,7 @@ import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
import Modal from "@/components/Modal" import Modal from "@/components/Modal"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import { getErrorMessage } from "@/components/TempDesignSystem/Form/Input/errors" import { getErrorMessage } from "@/utils/getErrorMessage"
import { Input as BookingWidgetInput } from "../Input" import { Input as BookingWidgetInput } from "../Input"
import { isMultiRoomError } from "../utils" import { isMultiRoomError } from "../utils"

View File

@@ -13,7 +13,7 @@ import { Typography } from "@scandic-hotels/design-system/Typography"
import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking" import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
import Modal from "@/components/Modal" import Modal from "@/components/Modal"
import { getErrorMessage } from "@/components/TempDesignSystem/Form/Input/errors" import { getErrorMessage } from "@/utils/getErrorMessage"
import { RemoveExtraRooms } from "../BookingCode" import { RemoveExtraRooms } from "../BookingCode"
import { isMultiRoomError } from "../utils" import { isMultiRoomError } from "../utils"

View File

@@ -3,17 +3,21 @@
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import { Divider } from "@scandic-hotels/design-system/Divider" import { Divider } from "@scandic-hotels/design-system/Divider"
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
import { Select } from "@scandic-hotels/design-system/Select" import { Select } from "@scandic-hotels/design-system/Select"
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from "@scandic-hotels/design-system/Typography"
import { getLocalizedLanguageOptions } from "@/constants/languages" import { getLocalizedLanguageOptions } from "@/constants/languages"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import DateSelect from "@/components/TempDesignSystem/Form/Date" import DateSelect from "@/components/TempDesignSystem/Form/Date"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import PasswordInput from "@/components/TempDesignSystem/Form/PasswordInput" import PasswordInput from "@/components/TempDesignSystem/Form/PasswordInput"
import Phone from "@/components/TempDesignSystem/Form/Phone" import Phone from "@/components/TempDesignSystem/Form/Phone"
import useLang from "@/hooks/useLang" import useLang from "@/hooks/useLang"
import { getFormattedCountryList } from "@/utils/countries"
import { getErrorMessage } from "@/utils/getErrorMessage"
import { editProfileErrors } from "../schema"
import styles from "./formContent.module.css" import styles from "./formContent.module.css"
@@ -59,9 +63,15 @@ export default function FormContent() {
registerOptions={{ required: true }} registerOptions={{ required: true }}
/> />
<CountrySelect <CountrySelect
countries={getFormattedCountryList(intl)}
errorMessage={getErrorMessage(
intl,
editProfileErrors.COUNTRY_REQUIRED
)}
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Country", defaultMessage: "Country",
})} })}
lang={lang}
name="address.countryCode" name="address.countryCode"
registerOptions={{ required: true }} registerOptions={{ required: true }}
/> />

View File

@@ -9,6 +9,7 @@ import { useIntl } from "react-intl"
import { logger } from "@scandic-hotels/common/logger" import { logger } from "@scandic-hotels/common/logger"
import { Button } from "@scandic-hotels/design-system/Button" import { Button } from "@scandic-hotels/design-system/Button"
import Checkbox from "@scandic-hotels/design-system/Form/Checkbox" import Checkbox from "@scandic-hotels/design-system/Form/Checkbox"
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from "@scandic-hotels/design-system/Typography"
import { trpc } from "@scandic-hotels/trpc/client" import { trpc } from "@scandic-hotels/trpc/client"
import { import {
@@ -23,7 +24,6 @@ import {
privacyPolicy, privacyPolicy,
} from "@/constants/webHrefs" } from "@/constants/webHrefs"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import DateSelect from "@/components/TempDesignSystem/Form/Date" import DateSelect from "@/components/TempDesignSystem/Form/Date"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import PasswordInput from "@/components/TempDesignSystem/Form/PasswordInput" import PasswordInput from "@/components/TempDesignSystem/Form/PasswordInput"
@@ -32,6 +32,8 @@ import Link from "@/components/TempDesignSystem/Link"
import { toast } from "@/components/TempDesignSystem/Toasts" import { toast } from "@/components/TempDesignSystem/Toasts"
import { useFormTracking } from "@/components/TrackingSDK/hooks" import { useFormTracking } from "@/components/TrackingSDK/hooks"
import useLang from "@/hooks/useLang" import useLang from "@/hooks/useLang"
import { getFormattedCountryList } from "@/utils/countries"
import { getErrorMessage } from "@/utils/getErrorMessage"
import { formatPhoneNumber } from "@/utils/phone" import { formatPhoneNumber } from "@/utils/phone"
// import { type SignUpSchema, signUpSchema } from "./schema" // import { type SignUpSchema, signUpSchema } from "./schema"
@@ -169,9 +171,15 @@ export default function SignupForm({ title }: SignUpFormProps) {
registerOptions={{ required: true }} registerOptions={{ required: true }}
/> />
<CountrySelect <CountrySelect
countries={getFormattedCountryList(intl)}
errorMessage={getErrorMessage(
intl,
signupErrors.COUNTRY_REQUIRED
)}
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Country", defaultMessage: "Country",
})} })}
lang={lang}
name="address.countryCode" name="address.countryCode"
registerOptions={{ required: true }} registerOptions={{ required: true }}
/> />

View File

@@ -5,26 +5,30 @@ import { FormProvider, useForm } from "react-hook-form"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import Footnote from "@scandic-hotels/design-system/Footnote" import Footnote from "@scandic-hotels/design-system/Footnote"
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
import { useEnterDetailsStore } from "@/stores/enter-details" import { useEnterDetailsStore } from "@/stores/enter-details"
import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests" import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import Phone from "@/components/TempDesignSystem/Form/Phone" import Phone from "@/components/TempDesignSystem/Form/Phone"
import { useFormTracking } from "@/components/TrackingSDK/hooks" import { useFormTracking } from "@/components/TrackingSDK/hooks"
import { useRoomContext } from "@/contexts/Details/Room" import { useRoomContext } from "@/contexts/Details/Room"
import useLang from "@/hooks/useLang"
import usePhoneNumberParsing from "@/hooks/usePhoneNumberParsing" import usePhoneNumberParsing from "@/hooks/usePhoneNumberParsing"
import { getFormattedCountryList } from "@/utils/countries"
import { getErrorMessage } from "@/utils/getErrorMessage"
import MemberPriceModal from "../MemberPriceModal" import MemberPriceModal from "../MemberPriceModal"
import JoinScandicFriendsCard from "./JoinScandicFriendsCard" import JoinScandicFriendsCard from "./JoinScandicFriendsCard"
import { getMultiroomDetailsSchema } from "./schema" import { getMultiroomDetailsSchema, multiroomErrors } from "./schema"
import styles from "./details.module.css" import styles from "./details.module.css"
const formID = "enter-details" const formID = "enter-details"
export default function Details() { export default function Details() {
const intl = useIntl() const intl = useIntl()
const lang = useLang()
const { addPreSubmitCallback, rooms } = useEnterDetailsStore((state) => ({ const { addPreSubmitCallback, rooms } = useEnterDetailsStore((state) => ({
addPreSubmitCallback: state.actions.addPreSubmitCallback, addPreSubmitCallback: state.actions.addPreSubmitCallback,
@@ -187,9 +191,15 @@ export default function Details() {
/> />
<CountrySelect <CountrySelect
className={styles.fullWidth} className={styles.fullWidth}
countries={getFormattedCountryList(intl)}
errorMessage={getErrorMessage(
intl,
multiroomErrors.COUNTRY_REQUIRED
)}
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Country", defaultMessage: "Country",
})} })}
lang={lang}
name="countryCode" name="countryCode"
registerOptions={{ required: true, onBlur: updateDetailsStore }} registerOptions={{ required: true, onBlur: updateDetailsStore }}
/> />

View File

@@ -5,20 +5,27 @@ import { FormProvider, useForm } from "react-hook-form"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import Footnote from "@scandic-hotels/design-system/Footnote" import Footnote from "@scandic-hotels/design-system/Footnote"
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
import { useEnterDetailsStore } from "@/stores/enter-details" import { useEnterDetailsStore } from "@/stores/enter-details"
import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests" import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import Phone from "@/components/TempDesignSystem/Form/Phone" import Phone from "@/components/TempDesignSystem/Form/Phone"
import { useFormTracking } from "@/components/TrackingSDK/hooks" import { useFormTracking } from "@/components/TrackingSDK/hooks"
import { useRoomContext } from "@/contexts/Details/Room" import { useRoomContext } from "@/contexts/Details/Room"
import useLang from "@/hooks/useLang"
import usePhoneNumberParsing from "@/hooks/usePhoneNumberParsing" import usePhoneNumberParsing from "@/hooks/usePhoneNumberParsing"
import { getFormattedCountryList } from "@/utils/countries"
import { getErrorMessage } from "@/utils/getErrorMessage"
import MemberPriceModal from "../MemberPriceModal" import MemberPriceModal from "../MemberPriceModal"
import JoinScandicFriendsCard from "./JoinScandicFriendsCard" import JoinScandicFriendsCard from "./JoinScandicFriendsCard"
import { guestDetailsSchema, signedInDetailsSchema } from "./schema" import {
guestDetailsSchema,
roomOneErrors,
signedInDetailsSchema,
} from "./schema"
import Signup from "./Signup" import Signup from "./Signup"
import styles from "./details.module.css" import styles from "./details.module.css"
@@ -31,6 +38,7 @@ import type {
const formID = "enter-details" const formID = "enter-details"
export default function Details({ user }: DetailsProps) { export default function Details({ user }: DetailsProps) {
const intl = useIntl() const intl = useIntl()
const lang = useLang()
const { lastRoom, addPreSubmitCallback } = useEnterDetailsStore((state) => ({ const { lastRoom, addPreSubmitCallback } = useEnterDetailsStore((state) => ({
lastRoom: state.lastRoom, lastRoom: state.lastRoom,
@@ -168,6 +176,9 @@ export default function Details({ user }: DetailsProps) {
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Country", defaultMessage: "Country",
})} })}
lang={lang}
countries={getFormattedCountryList(intl)}
errorMessage={getErrorMessage(intl, roomOneErrors.COUNTRY_REQUIRED)}
name="countryCode" name="countryCode"
readOnly={!!user} readOnly={!!user}
registerOptions={{ required: true, onBlur: updateDetailsStore }} registerOptions={{ required: true, onBlur: updateDetailsStore }}

View File

@@ -2,6 +2,7 @@ import { useFormContext } from "react-hook-form"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import Body from "@scandic-hotels/design-system/Body" import Body from "@scandic-hotels/design-system/Body"
import { ErrorMessage } from "@scandic-hotels/design-system/Form/ErrorMessage"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from "@scandic-hotels/design-system/Typography"
import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alertType" import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alertType"
@@ -9,8 +10,8 @@ import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alertType"
import { useAddAncillaryStore } from "@/stores/my-stay/add-ancillary-flow" import { useAddAncillaryStore } from "@/stores/my-stay/add-ancillary-flow"
import Alert from "@/components/TempDesignSystem/Alert" import Alert from "@/components/TempDesignSystem/Alert"
import ErrorMessage from "@/components/TempDesignSystem/Form/ErrorMessage"
import Select from "@/components/TempDesignSystem/Form/Select" import Select from "@/components/TempDesignSystem/Form/Select"
import { getErrorMessage } from "@/utils/getErrorMessage"
import styles from "./selectQuantityStep.module.css" import styles from "./selectQuantityStep.module.css"
@@ -116,7 +117,14 @@ export default function SelectQuantityStep({ user }: SelectQuantityStepProps) {
items={cardQuantityOptions} items={cardQuantityOptions}
isNestedInModal isNestedInModal
/> />
<ErrorMessage errors={errors} name="quantityWithCard" /> <ErrorMessage
errors={errors}
name="quantityWithCard"
messageLabel={getErrorMessage(
intl,
errors["quantityWithCard"]?.message?.toString()
)}
/>
</div> </div>
</div> </div>
) )

View File

@@ -4,10 +4,14 @@ import { useFormContext } from "react-hook-form"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import Body from "@scandic-hotels/design-system/Body" import Body from "@scandic-hotels/design-system/Body"
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
import { signupErrors } from "@scandic-hotels/trpc/routers/user/schemas"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import Input from "@/components/TempDesignSystem/Form/Input" import Input from "@/components/TempDesignSystem/Form/Input"
import Phone from "@/components/TempDesignSystem/Form/Phone" import Phone from "@/components/TempDesignSystem/Form/Phone"
import useLang from "@/hooks/useLang"
import { getFormattedCountryList } from "@/utils/countries"
import { getErrorMessage } from "@/utils/getErrorMessage"
import styles from "./modifyContact.module.css" import styles from "./modifyContact.module.css"
@@ -23,6 +27,7 @@ export default function ModifyContact({
isFirstStep, isFirstStep,
}: ModifyContactProps) { }: ModifyContactProps) {
const intl = useIntl() const intl = useIntl()
const lang = useLang()
const { getValues, setValue } = useFormContext() const { getValues, setValue } = useFormContext()
useEffect(() => { useEffect(() => {
@@ -57,9 +62,15 @@ export default function ModifyContact({
</div> </div>
<div className={styles.row}> <div className={styles.row}>
<CountrySelect <CountrySelect
countries={getFormattedCountryList(intl)}
errorMessage={getErrorMessage(
intl,
signupErrors.COUNTRY_REQUIRED
)}
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Country", defaultMessage: "Country",
})} })}
lang={lang}
name="countryCode" name="countryCode"
/> />
</div> </div>

View File

@@ -1,20 +0,0 @@
"use client"
import { useMediaQuery } from "usehooks-ts"
import CountryCombobox from "./CountryCombobox"
import CountrySelect from "./CountrySelect"
import type { CountryProps } from "./country"
export default function Country(props: CountryProps) {
const isDesktop = useMediaQuery("(min-width: 768px)", {
initializeWithValue: false,
})
return isDesktop ? (
<CountryCombobox {...props} />
) : (
<CountrySelect {...props} />
)
}

View File

@@ -7,13 +7,14 @@ import { useMediaQuery } from "usehooks-ts"
import { dt } from "@scandic-hotels/common/dt" import { dt } from "@scandic-hotels/common/dt"
import { logger } from "@scandic-hotels/common/logger" import { logger } from "@scandic-hotels/common/logger"
import { ErrorMessage } from "@scandic-hotels/design-system/Form/ErrorMessage"
import { Select } from "@scandic-hotels/design-system/Select" import { Select } from "@scandic-hotels/design-system/Select"
import useLang from "@/hooks/useLang" import useLang from "@/hooks/useLang"
import { getLocalizedMonthName } from "@/utils/dateFormatting" import { getLocalizedMonthName } from "@/utils/dateFormatting"
import { getErrorMessage } from "@/utils/getErrorMessage"
import { rangeArray } from "@/utils/rangeArray" import { rangeArray } from "@/utils/rangeArray"
import ErrorMessage from "../ErrorMessage"
import { DateName, type DateProps } from "./date" import { DateName, type DateProps } from "./date"
import styles from "./date.module.css" import styles from "./date.module.css"
@@ -172,7 +173,14 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
/> />
</div> </div>
</div> </div>
<ErrorMessage errors={formState.errors} name={field.name} /> <ErrorMessage
errors={formState.errors}
name={field.name}
messageLabel={getErrorMessage(
intl,
formState.errors[field.name]?.message?.toString()
)}
/>
</> </>
) )
} }

View File

@@ -1,13 +0,0 @@
import Caption from "@scandic-hotels/design-system/Caption"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import styles from "./error.module.css"
export default function Error({ children }: React.PropsWithChildren) {
return (
<Caption className={styles.message} fontOnly>
<MaterialIcon icon="info" color="Icon/Feedback/Error" />
{children}
</Caption>
)
}

View File

@@ -1,21 +0,0 @@
import { ErrorMessage as RHFErrorMessage } from "@hookform/error-message"
import { useIntl } from "react-intl"
import { getErrorMessage } from "../Input/errors"
import Error from "./Error"
import type { ErrorMessageProps } from "./errorMessage"
export default function ErrorMessage<T>({
errors,
name,
}: ErrorMessageProps<T>) {
const intl = useIntl()
return (
<RHFErrorMessage
errors={errors}
name={name}
render={({ message }) => <Error>{getErrorMessage(intl, message)}</Error>}
/>
)
}

View File

@@ -9,7 +9,7 @@ import Caption from "@scandic-hotels/design-system/Caption"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Input as InputWithLabel } from "@scandic-hotels/design-system/Input" import { Input as InputWithLabel } from "@scandic-hotels/design-system/Input"
import { getErrorMessage } from "./errors" import { getErrorMessage } from "@/utils/getErrorMessage"
import styles from "./input.module.css" import styles from "./input.module.css"

View File

@@ -11,7 +11,7 @@ import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Input } from "@scandic-hotels/design-system/Input" import { Input } from "@scandic-hotels/design-system/Input"
import { OldDSButton as Button } from "@scandic-hotels/design-system/OldDSButton" import { OldDSButton as Button } from "@scandic-hotels/design-system/OldDSButton"
import { getErrorMessage } from "../Input/errors" import { getErrorMessage } from "@/utils/getErrorMessage"
import styles from "./passwordInput.module.css" import styles from "./passwordInput.module.css"

View File

@@ -16,14 +16,15 @@ import {
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import Body from "@scandic-hotels/design-system/Body" import Body from "@scandic-hotels/design-system/Body"
import { ErrorMessage } from "@scandic-hotels/design-system/Form/ErrorMessage"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Input } from "@scandic-hotels/design-system/Input" import { Input } from "@scandic-hotels/design-system/Input"
import { Label } from "@scandic-hotels/design-system/Label" import { Label } from "@scandic-hotels/design-system/Label"
import { getDefaultCountryFromLang } from "@/constants/languages" import { getDefaultCountryFromLang } from "@/constants/languages"
import ErrorMessage from "@/components/TempDesignSystem/Form/ErrorMessage"
import useLang from "@/hooks/useLang" import useLang from "@/hooks/useLang"
import { getErrorMessage } from "@/utils/getErrorMessage"
import styles from "./phone.module.css" import styles from "./phone.module.css"
@@ -149,7 +150,14 @@ export default function Phone({
readOnly={readOnly} readOnly={readOnly}
type="tel" type="tel"
/> />
<ErrorMessage errors={formState.errors} name={name} /> <ErrorMessage
errors={formState.errors}
name={name}
messageLabel={getErrorMessage(
intl,
formState.errors[name]?.message?.toString()
)}
/>
</TextField> </TextField>
</div> </div>
) )

View File

@@ -1,7 +1,19 @@
import { countriesMap } from "@scandic-hotels/trpc/constants/countries" import {
countries,
countriesMap,
} from "@scandic-hotels/trpc/constants/countries"
import type { IntlShape } from "react-intl"
export function isValidCountry( export function isValidCountry(
country: string country: string
): country is keyof typeof countriesMap { ): country is keyof typeof countriesMap {
return country in countriesMap return country in countriesMap
} }
export function getFormattedCountryList(intl: IntlShape) {
return countries.map((country) => ({
...country,
displayName: intl.formatDisplayName(country.code, { type: "region" }),
}))
}

View File

@@ -1,6 +1,6 @@
"use client" 'use client'
import { type SyntheticEvent, useMemo, useState } from "react" import { type SyntheticEvent, useMemo, useState } from 'react'
import { import {
Button, Button,
ComboBox, ComboBox,
@@ -10,38 +10,33 @@ import {
ListBoxItem, ListBoxItem,
Popover, Popover,
useFilter, useFilter,
} from "react-aria-components" } from 'react-aria-components'
import { useController } from "react-hook-form" import { useController } from 'react-hook-form'
import { useIntl } from "react-intl"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon" import { MaterialIcon } from '../../Icons/MaterialIcon'
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from '../../Typography'
import { countries } from "@scandic-hotels/trpc/constants/countries" import { ErrorMessage } from '../ErrorMessage'
import useLang from "@/hooks/useLang" import styles from './country.module.css'
import ErrorMessage from "../ErrorMessage" import type { CountryProps } from './country'
import styles from "./country.module.css" const prioCountryCode = ['DE', 'DK', 'FI', 'NO', 'SE']
import type { CountryProps } from "./country"
const prioCountryCode = ["DE", "DK", "FI", "NO", "SE"]
export default function CountryCombobox({ export default function CountryCombobox({
// hack used since chrome does not respect autocomplete="off" // hack used since chrome does not respect autocomplete="off"
autoComplete = "nope", autoComplete = 'nope',
className = "", className = '',
errorMessage,
label, label,
name = "country", lang = 'en',
countries,
name = 'country',
readOnly = false, readOnly = false,
registerOptions = {}, registerOptions = {},
}: CountryProps) { }: CountryProps) {
const lang = useLang() const { startsWith } = useFilter({ sensitivity: 'base' })
const intl = useIntl() const [filterValue, setFilterValue] = useState('')
const { startsWith } = useFilter({ sensitivity: "base" })
const [filterValue, setFilterValue] = useState("")
const { field, formState, fieldState } = useController({ const { field, formState, fieldState } = useController({
name, name,
rules: registerOptions, rules: registerOptions,
@@ -51,9 +46,7 @@ export default function CountryCombobox({
function mapCountry(country: (typeof countries)[number]) { function mapCountry(country: (typeof countries)[number]) {
return { return {
value: country.code, value: country.code,
label: label: country.displayName || country.name,
intl.formatDisplayName(country.code, { type: "region" }) ||
country.name,
} }
} }
@@ -71,11 +64,11 @@ export default function CountryCombobox({
.sort((a, b) => collator.compare(a.label, b.label)) .sort((a, b) => collator.compare(a.label, b.label))
return [...prioCountries, ...restCountries] return [...prioCountries, ...restCountries]
}, [filterValue, intl, lang, startsWith]) }, [filterValue, lang, startsWith, countries])
function handleOnInput(evt: SyntheticEvent<HTMLInputElement>) { function handleOnInput(evt: SyntheticEvent<HTMLInputElement>) {
setFilterValue(evt.currentTarget.value) setFilterValue(evt.currentTarget.value)
const isAutoCompleteEvent = !("inputType" in evt.nativeEvent) const isAutoCompleteEvent = !('inputType' in evt.nativeEvent)
if (isAutoCompleteEvent) { if (isAutoCompleteEvent) {
const { value } = evt.currentTarget const { value } = evt.currentTarget
const cc = countries.find((c) => c.name === value || c.code === value) const cc = countries.find((c) => c.name === value || c.code === value)
@@ -96,7 +89,7 @@ export default function CountryCombobox({
isInvalid={fieldState.invalid} isInvalid={fieldState.invalid}
name={name} name={name}
onBlur={field.onBlur} onBlur={field.onBlur}
onSelectionChange={(c) => field.onChange(c ?? "")} onSelectionChange={(c) => field.onChange(c ?? '')}
selectedKey={field.value} selectedKey={field.value}
menuTrigger="focus" menuTrigger="focus"
> >
@@ -149,8 +142,8 @@ export default function CountryCombobox({
<Typography <Typography
variant={ variant={
isSelected isSelected
? "Body/Paragraph/mdBold" ? 'Body/Paragraph/mdBold'
: "Body/Paragraph/mdRegular" : 'Body/Paragraph/mdRegular'
} }
> >
<span>{item.label}</span> <span>{item.label}</span>
@@ -161,7 +154,11 @@ export default function CountryCombobox({
</ListBox> </ListBox>
</Popover> </Popover>
</ComboBox> </ComboBox>
<ErrorMessage errors={formState.errors} name={name} /> <ErrorMessage
errors={formState.errors}
name={name}
messageLabel={errorMessage}
/>
</div> </div>
) )
} }

View File

@@ -1,30 +1,26 @@
"use client" 'use client'
import { useMemo } from "react" import { useMemo } from 'react'
import { useController } from "react-hook-form" import { useController } from 'react-hook-form'
import { useIntl } from "react-intl"
import { Select } from "@scandic-hotels/design-system/Select" import { Select } from '../../Select'
import { countries } from "@scandic-hotels/trpc/constants/countries"
import useLang from "@/hooks/useLang" import { ErrorMessage } from '../ErrorMessage'
import ErrorMessage from "../ErrorMessage" import type { CountryProps } from './country'
import type { CountryProps } from "./country" const prioCountryCode = ['DE', 'DK', 'FI', 'NO', 'SE']
const prioCountryCode = ["DE", "DK", "FI", "NO", "SE"]
export default function CountrySelect({ export default function CountrySelect({
className = "", className = '',
errorMessage,
label, label,
name = "country", countries,
lang = 'en',
name = 'country',
readOnly = false, readOnly = false,
registerOptions = {}, registerOptions = {},
}: CountryProps) { }: CountryProps) {
const lang = useLang()
const intl = useIntl()
const { field, formState, fieldState } = useController({ const { field, formState, fieldState } = useController({
name, name,
rules: registerOptions, rules: registerOptions,
@@ -34,9 +30,7 @@ export default function CountrySelect({
function mapCountry(country: (typeof countries)[number]) { function mapCountry(country: (typeof countries)[number]) {
return { return {
value: country.code, value: country.code,
label: label: country.displayName || country.name,
intl.formatDisplayName(country.code, { type: "region" }) ||
country.name,
} }
} }
@@ -52,7 +46,7 @@ export default function CountrySelect({
.sort((a, b) => collator.compare(a.label, b.label)) .sort((a, b) => collator.compare(a.label, b.label))
return [...prioCountries, ...restCountries] return [...prioCountries, ...restCountries]
}, [intl, lang]) }, [lang, countries])
return ( return (
<div className={className}> <div className={className}>
@@ -64,11 +58,15 @@ export default function CountrySelect({
isInvalid={fieldState.invalid} isInvalid={fieldState.invalid}
name={name} name={name}
onBlur={field.onBlur} onBlur={field.onBlur}
onSelectionChange={(c) => field.onChange(c ?? "")} onSelectionChange={(c) => field.onChange(c ?? '')}
selectedKey={field.value} selectedKey={field.value}
data-testid={name} data-testid={name}
/> />
<ErrorMessage errors={formState.errors} name={name} /> <ErrorMessage
errors={formState.errors}
name={name}
messageLabel={errorMessage}
/>
</div> </div>
) )
} }

View File

@@ -6,7 +6,7 @@
height: 56px; height: 56px;
&[data-required] .label::after { &[data-required] .label::after {
content: " *"; content: ' *';
} }
&[data-open] { &[data-open] {
@@ -86,12 +86,12 @@
} }
} }
.inner:has(input:placeholder-shown, input[data-focused="true"], input:valid) .inner:has(input:placeholder-shown, input[data-focused='true'], input:valid)
.labelValue { .labelValue {
display: initial; display: initial;
} }
.inner:has(input[value=""]:not([data-focused="true"])) .labelEmpty { .inner:has(input[value='']:not([data-focused='true'])) .labelEmpty {
display: initial; display: initial;
} }
@@ -104,7 +104,7 @@
transition: min-height 150ms ease; transition: min-height 150ms ease;
width: 100%; width: 100%;
&[value]:not([value=""]) { &[value]:not([value='']) {
min-height: 18px; min-height: 18px;
} }
} }

View File

@@ -1,9 +1,16 @@
import type { RegisterOptions } from "react-hook-form" import type { RegisterOptions } from 'react-hook-form'
export type CountryProps = { export type CountryProps = {
autoComplete?: string autoComplete?: string
className?: string className?: string
errorMessage?: string
label: string label: string
countries: {
code: string
name: string
displayName?: string
}[]
lang?: string
name?: string name?: string
placeholder?: string placeholder?: string
readOnly?: boolean readOnly?: boolean

View File

@@ -0,0 +1,20 @@
'use client'
import { useMediaQuery } from 'usehooks-ts'
import CountryCombobox from './CountryCombobox'
import CountrySelect from './CountrySelect'
import type { CountryProps } from './country'
export default function Country(props: CountryProps) {
const isDesktop = useMediaQuery('(min-width: 768px)', {
initializeWithValue: false,
})
return isDesktop ? (
<CountryCombobox {...props} />
) : (
<CountrySelect {...props} />
)
}

View File

@@ -0,0 +1,18 @@
import { MaterialIcon } from '../../Icons/MaterialIcon'
import styles from './error.module.css'
import { Typography } from '../../Typography'
export function Error({ children }: React.PropsWithChildren) {
return (
<Typography
className={styles.message}
variant="Body/Supporting text (caption)/smRegular"
>
<span>
<MaterialIcon icon="info" color="Icon/Feedback/Error" />
{children}
</span>
</Typography>
)
}

View File

@@ -2,6 +2,6 @@
align-items: center; align-items: center;
color: var(--Text-Interactive-Error); color: var(--Text-Interactive-Error);
display: flex; display: flex;
gap: var(--Spacing-x-half); gap: var(--Space-x05);
margin: var(--Spacing-x1) 0 0; margin: var(--Space-x1) 0 0;
} }

View File

@@ -1,16 +1,16 @@
import type { FieldValuesFromFieldErrors } from "@hookform/error-message" import type { FieldValuesFromFieldErrors } from '@hookform/error-message'
import type { import type {
FieldErrors, FieldErrors,
FieldName, FieldName,
FieldValues, FieldValues,
Message, Message,
MultipleFieldErrors, MultipleFieldErrors,
} from "react-hook-form" } from 'react-hook-form'
export type ErrorMessageProps<TFieldErrors> = { export type ErrorMessageProps<TFieldErrors> = {
errors?: FieldErrors<FieldValues> errors?: FieldErrors<FieldValues>
name: FieldName<FieldValuesFromFieldErrors<TFieldErrors>> name: FieldName<FieldValuesFromFieldErrors<TFieldErrors>>
message?: Message messageLabel?: Message
render?: (data: { render?: (data: {
message: Message message: Message
messages?: MultipleFieldErrors messages?: MultipleFieldErrors

View File

@@ -0,0 +1,19 @@
import { ErrorMessage as RHFErrorMessage } from '@hookform/error-message'
import { Error } from './Error'
import type { ErrorMessageProps } from './errorMessage'
export function ErrorMessage<T>({
errors,
name,
messageLabel,
}: ErrorMessageProps<T>) {
return (
<RHFErrorMessage
errors={errors}
name={name}
render={({ message }) => <Error>{messageLabel ?? message}</Error>}
/>
)
}

View File

@@ -15,6 +15,8 @@
"./Divider": "./dist/components/Divider/index.js", "./Divider": "./dist/components/Divider/index.js",
"./Footnote": "./dist/components/Footnote/index.js", "./Footnote": "./dist/components/Footnote/index.js",
"./Form/Checkbox": "./dist/components/Form/Checkbox/index.js", "./Form/Checkbox": "./dist/components/Form/Checkbox/index.js",
"./Form/Country": "./dist/components/Form/Country/index.js",
"./Form/ErrorMessage": "./dist/components/Form/ErrorMessage/index.js",
"./Form/RadioCard": "./dist/components/Form/RadioCard/index.js", "./Form/RadioCard": "./dist/components/Form/RadioCard/index.js",
"./Input": "./dist/components/Input/index.js", "./Input": "./dist/components/Input/index.js",
"./Label": "./dist/components/Label/index.js", "./Label": "./dist/components/Label/index.js",