fix: remove filtering from date and country select for better mobile experience
This commit is contained in:
@@ -0,0 +1,168 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { type SyntheticEvent, useMemo, useState } from "react"
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
ComboBox,
|
||||||
|
Input,
|
||||||
|
Label,
|
||||||
|
ListBox,
|
||||||
|
ListBoxItem,
|
||||||
|
Popover,
|
||||||
|
useFilter,
|
||||||
|
} from "react-aria-components"
|
||||||
|
import { useController } from "react-hook-form"
|
||||||
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
|
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
|
||||||
|
import { Typography } from "@scandic-hotels/design-system/Typography"
|
||||||
|
|
||||||
|
import { countries } from "@/constants/countries"
|
||||||
|
|
||||||
|
import useLang from "@/hooks/useLang"
|
||||||
|
|
||||||
|
import ErrorMessage from "../ErrorMessage"
|
||||||
|
|
||||||
|
import styles from "./country.module.css"
|
||||||
|
|
||||||
|
import type { CountryProps } from "./country"
|
||||||
|
|
||||||
|
const prioCountryCode = ["DE", "DK", "FI", "NO", "SE"]
|
||||||
|
|
||||||
|
export default function CountrySelect({
|
||||||
|
// hack used since chrome does not respect autocomplete="off"
|
||||||
|
autoComplete = "nope",
|
||||||
|
className = "",
|
||||||
|
label,
|
||||||
|
name = "country",
|
||||||
|
readOnly = false,
|
||||||
|
registerOptions = {},
|
||||||
|
}: CountryProps) {
|
||||||
|
const lang = useLang()
|
||||||
|
const intl = useIntl()
|
||||||
|
|
||||||
|
const { startsWith } = useFilter({ sensitivity: "base" })
|
||||||
|
const [filterValue, setFilterValue] = useState("")
|
||||||
|
const { field, formState, fieldState } = useController({
|
||||||
|
name,
|
||||||
|
rules: registerOptions,
|
||||||
|
})
|
||||||
|
|
||||||
|
const items = useMemo(() => {
|
||||||
|
function mapCountry(country: (typeof countries)[number]) {
|
||||||
|
return {
|
||||||
|
value: country.code,
|
||||||
|
label:
|
||||||
|
intl.formatDisplayName(country.code, { type: "region" }) ||
|
||||||
|
country.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const collator = new Intl.Collator(lang)
|
||||||
|
const prioCountries = countries
|
||||||
|
.filter((c) => prioCountryCode.includes(c.code))
|
||||||
|
.map(mapCountry)
|
||||||
|
.filter((item) => startsWith(item.label, filterValue))
|
||||||
|
.sort((a, b) => collator.compare(a.label, b.label))
|
||||||
|
|
||||||
|
const restCountries = countries
|
||||||
|
.filter((c) => !prioCountryCode.includes(c.code))
|
||||||
|
.map(mapCountry)
|
||||||
|
.filter((item) => startsWith(item.label, filterValue))
|
||||||
|
.sort((a, b) => collator.compare(a.label, b.label))
|
||||||
|
|
||||||
|
return [...prioCountries, ...restCountries]
|
||||||
|
}, [filterValue, intl, lang, startsWith])
|
||||||
|
|
||||||
|
function handleOnInput(evt: SyntheticEvent<HTMLInputElement>) {
|
||||||
|
setFilterValue(evt.currentTarget.value)
|
||||||
|
const isAutoCompleteEvent = !("inputType" in evt.nativeEvent)
|
||||||
|
if (isAutoCompleteEvent) {
|
||||||
|
const { value } = evt.currentTarget
|
||||||
|
const cc = countries.find((c) => c.name === value || c.code === value)
|
||||||
|
if (cc) {
|
||||||
|
field.onChange(cc.code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={className}>
|
||||||
|
<ComboBox
|
||||||
|
aria-label={label}
|
||||||
|
className={styles.select}
|
||||||
|
data-testid={name}
|
||||||
|
isReadOnly={readOnly}
|
||||||
|
isRequired={Boolean(registerOptions?.required)}
|
||||||
|
isInvalid={fieldState.invalid}
|
||||||
|
name={name}
|
||||||
|
onBlur={field.onBlur}
|
||||||
|
onSelectionChange={(c) => field.onChange(c ?? "")}
|
||||||
|
selectedKey={field.value}
|
||||||
|
menuTrigger="focus"
|
||||||
|
>
|
||||||
|
<Label className={styles.inner}>
|
||||||
|
<span className={styles.displayText}>
|
||||||
|
<Typography variant="Label/xsRegular">
|
||||||
|
<span className={`${styles.label} ${styles.labelValue}`}>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="Body/Paragraph/mdRegular">
|
||||||
|
<span className={`${styles.label} ${styles.labelEmpty}`}>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="Body/Paragraph/mdRegular">
|
||||||
|
<Input
|
||||||
|
autoComplete={autoComplete}
|
||||||
|
className={styles.input}
|
||||||
|
onInput={handleOnInput}
|
||||||
|
ref={field.ref}
|
||||||
|
/>
|
||||||
|
</Typography>
|
||||||
|
</span>
|
||||||
|
<Button className={styles.button}>
|
||||||
|
<MaterialIcon
|
||||||
|
aria-hidden="true"
|
||||||
|
className={styles.chevron}
|
||||||
|
color="Icon/Default"
|
||||||
|
icon="chevron_right"
|
||||||
|
size={24}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
</Label>
|
||||||
|
<Popover
|
||||||
|
className={styles.popover}
|
||||||
|
crossOffset={-8}
|
||||||
|
offset={22}
|
||||||
|
shouldFlip={false}
|
||||||
|
>
|
||||||
|
<ListBox className={styles.listBox}>
|
||||||
|
{items.map((item) => (
|
||||||
|
<ListBoxItem
|
||||||
|
className={styles.listBoxItem}
|
||||||
|
key={item.value}
|
||||||
|
id={item.value}
|
||||||
|
textValue={item.label}
|
||||||
|
>
|
||||||
|
{({ isSelected }) => (
|
||||||
|
<Typography
|
||||||
|
variant={
|
||||||
|
isSelected
|
||||||
|
? "Body/Paragraph/mdBold"
|
||||||
|
: "Body/Paragraph/mdRegular"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<span>{item.label}</span>
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</ListBoxItem>
|
||||||
|
))}
|
||||||
|
</ListBox>
|
||||||
|
</Popover>
|
||||||
|
</ComboBox>
|
||||||
|
<ErrorMessage errors={formState.errors} name={name} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,21 +1,10 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { type SyntheticEvent, useMemo, useState } from "react"
|
import { useMemo } from "react"
|
||||||
import {
|
|
||||||
Button,
|
|
||||||
ComboBox,
|
|
||||||
Input,
|
|
||||||
Label,
|
|
||||||
ListBox,
|
|
||||||
ListBoxItem,
|
|
||||||
Popover,
|
|
||||||
useFilter,
|
|
||||||
} from "react-aria-components"
|
|
||||||
import { useController } from "react-hook-form"
|
import { useController } from "react-hook-form"
|
||||||
import { useIntl } from "react-intl"
|
import { useIntl } from "react-intl"
|
||||||
|
|
||||||
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
|
import { Select } from "@scandic-hotels/design-system/Select"
|
||||||
import { Typography } from "@scandic-hotels/design-system/Typography"
|
|
||||||
|
|
||||||
import { countries } from "@/constants/countries"
|
import { countries } from "@/constants/countries"
|
||||||
|
|
||||||
@@ -23,15 +12,11 @@ import useLang from "@/hooks/useLang"
|
|||||||
|
|
||||||
import ErrorMessage from "../ErrorMessage"
|
import ErrorMessage from "../ErrorMessage"
|
||||||
|
|
||||||
import styles from "./country.module.css"
|
|
||||||
|
|
||||||
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({
|
||||||
// hack used since chrome does not respect autocomplete="off"
|
|
||||||
autoComplete = "nope",
|
|
||||||
className = "",
|
className = "",
|
||||||
label,
|
label,
|
||||||
name = "country",
|
name = "country",
|
||||||
@@ -41,8 +26,6 @@ export default function CountrySelect({
|
|||||||
const lang = useLang()
|
const lang = useLang()
|
||||||
const intl = useIntl()
|
const intl = useIntl()
|
||||||
|
|
||||||
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,
|
||||||
@@ -62,36 +45,21 @@ export default function CountrySelect({
|
|||||||
const prioCountries = countries
|
const prioCountries = countries
|
||||||
.filter((c) => prioCountryCode.includes(c.code))
|
.filter((c) => prioCountryCode.includes(c.code))
|
||||||
.map(mapCountry)
|
.map(mapCountry)
|
||||||
.filter((item) => startsWith(item.label, filterValue))
|
|
||||||
.sort((a, b) => collator.compare(a.label, b.label))
|
.sort((a, b) => collator.compare(a.label, b.label))
|
||||||
|
|
||||||
const restCountries = countries
|
const restCountries = countries
|
||||||
.filter((c) => !prioCountryCode.includes(c.code))
|
.filter((c) => !prioCountryCode.includes(c.code))
|
||||||
.map(mapCountry)
|
.map(mapCountry)
|
||||||
.filter((item) => startsWith(item.label, filterValue))
|
|
||||||
.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])
|
}, [intl, lang])
|
||||||
|
|
||||||
function handleOnInput(evt: SyntheticEvent<HTMLInputElement>) {
|
|
||||||
setFilterValue(evt.currentTarget.value)
|
|
||||||
const isAutoCompleteEvent = !("inputType" in evt.nativeEvent)
|
|
||||||
if (isAutoCompleteEvent) {
|
|
||||||
const { value } = evt.currentTarget
|
|
||||||
const cc = countries.find((c) => c.name === value || c.code === value)
|
|
||||||
if (cc) {
|
|
||||||
field.onChange(cc.code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={className}>
|
<div className={className}>
|
||||||
<ComboBox
|
<Select
|
||||||
aria-label={label}
|
items={items}
|
||||||
className={styles.select}
|
label={label}
|
||||||
data-testid={name}
|
|
||||||
isReadOnly={readOnly}
|
isReadOnly={readOnly}
|
||||||
isRequired={Boolean(registerOptions?.required)}
|
isRequired={Boolean(registerOptions?.required)}
|
||||||
isInvalid={fieldState.invalid}
|
isInvalid={fieldState.invalid}
|
||||||
@@ -99,69 +67,8 @@ export default function CountrySelect({
|
|||||||
onBlur={field.onBlur}
|
onBlur={field.onBlur}
|
||||||
onSelectionChange={(c) => field.onChange(c ?? "")}
|
onSelectionChange={(c) => field.onChange(c ?? "")}
|
||||||
selectedKey={field.value}
|
selectedKey={field.value}
|
||||||
menuTrigger="focus"
|
data-testid={name}
|
||||||
>
|
/>
|
||||||
<Label className={styles.inner}>
|
|
||||||
<span className={styles.displayText}>
|
|
||||||
<Typography variant="Label/xsRegular">
|
|
||||||
<span className={`${styles.label} ${styles.labelValue}`}>
|
|
||||||
{label}
|
|
||||||
</span>
|
|
||||||
</Typography>
|
|
||||||
<Typography variant="Body/Paragraph/mdRegular">
|
|
||||||
<span className={`${styles.label} ${styles.labelEmpty}`}>
|
|
||||||
{label}
|
|
||||||
</span>
|
|
||||||
</Typography>
|
|
||||||
<Typography variant="Body/Paragraph/mdRegular">
|
|
||||||
<Input
|
|
||||||
autoComplete={autoComplete}
|
|
||||||
className={styles.input}
|
|
||||||
onInput={handleOnInput}
|
|
||||||
ref={field.ref}
|
|
||||||
/>
|
|
||||||
</Typography>
|
|
||||||
</span>
|
|
||||||
<Button className={styles.button}>
|
|
||||||
<MaterialIcon
|
|
||||||
aria-hidden="true"
|
|
||||||
className={styles.chevron}
|
|
||||||
color="Icon/Default"
|
|
||||||
icon="chevron_right"
|
|
||||||
size={24}
|
|
||||||
/>
|
|
||||||
</Button>
|
|
||||||
</Label>
|
|
||||||
<Popover
|
|
||||||
className={styles.popover}
|
|
||||||
crossOffset={-8}
|
|
||||||
offset={22}
|
|
||||||
shouldFlip={false}
|
|
||||||
>
|
|
||||||
<ListBox className={styles.listBox}>
|
|
||||||
{items.map((item) => (
|
|
||||||
<ListBoxItem
|
|
||||||
className={styles.listBoxItem}
|
|
||||||
key={item.value}
|
|
||||||
id={item.value}
|
|
||||||
textValue={item.label}
|
|
||||||
>
|
|
||||||
{({ isSelected }) => (
|
|
||||||
<Typography
|
|
||||||
variant={
|
|
||||||
isSelected
|
|
||||||
? "Body/Paragraph/mdBold"
|
|
||||||
: "Body/Paragraph/mdRegular"
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<span>{item.label}</span>
|
|
||||||
</Typography>
|
|
||||||
)}
|
|
||||||
</ListBoxItem>
|
|
||||||
))}
|
|
||||||
</ListBox>
|
|
||||||
</Popover>
|
|
||||||
</ComboBox>
|
|
||||||
<ErrorMessage errors={formState.errors} name={name} />
|
<ErrorMessage errors={formState.errors} name={name} />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -126,7 +126,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
|||||||
name={DateName.day}
|
name={DateName.day}
|
||||||
onSelectionChange={(key) => setValue(DateName.day, Number(key))}
|
onSelectionChange={(key) => setValue(DateName.day, Number(key))}
|
||||||
isRequired
|
isRequired
|
||||||
enableFiltering
|
|
||||||
isInvalid={fieldState.invalid}
|
isInvalid={fieldState.invalid}
|
||||||
onBlur={field.onBlur}
|
onBlur={field.onBlur}
|
||||||
defaultSelectedKey={dateValue?.day}
|
defaultSelectedKey={dateValue?.day}
|
||||||
@@ -142,7 +141,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
|||||||
name={DateName.month}
|
name={DateName.month}
|
||||||
onSelectionChange={(key) => setValue(DateName.month, Number(key))}
|
onSelectionChange={(key) => setValue(DateName.month, Number(key))}
|
||||||
isRequired
|
isRequired
|
||||||
enableFiltering
|
|
||||||
isInvalid={fieldState.invalid}
|
isInvalid={fieldState.invalid}
|
||||||
onBlur={field.onBlur}
|
onBlur={field.onBlur}
|
||||||
defaultSelectedKey={dateValue?.month}
|
defaultSelectedKey={dateValue?.month}
|
||||||
@@ -158,7 +156,6 @@ export default function DateSelect({ name, registerOptions = {} }: DateProps) {
|
|||||||
name={DateName.year}
|
name={DateName.year}
|
||||||
onSelectionChange={(key) => setValue(DateName.year, Number(key))}
|
onSelectionChange={(key) => setValue(DateName.year, Number(key))}
|
||||||
isRequired
|
isRequired
|
||||||
enableFiltering
|
|
||||||
isInvalid={fieldState.invalid}
|
isInvalid={fieldState.invalid}
|
||||||
onBlur={field.onBlur}
|
onBlur={field.onBlur}
|
||||||
defaultSelectedKey={dateValue?.year}
|
defaultSelectedKey={dateValue?.year}
|
||||||
|
|||||||
Reference in New Issue
Block a user