-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathindex.tsx
117 lines (111 loc) · 4.32 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Listbox, Transition } from "@headlessui/react"
import { ChevronUpDown } from "@medusajs/icons"
import { clx } from "@medusajs/ui"
import { Fragment, useMemo } from "react"
import Radio from "@modules/common/components/radio"
import compareAddresses from "@lib/util/compare-addresses"
import { HttpTypes } from "@medusajs/types"
type AddressSelectProps = {
addresses: HttpTypes.StoreCustomerAddress[]
addressInput: HttpTypes.StoreCartAddress | null
onSelect: (
address: HttpTypes.StoreCartAddress | undefined,
email?: string
) => void
}
const AddressSelect = ({
addresses,
addressInput,
onSelect,
}: AddressSelectProps) => {
const handleSelect = (id: string) => {
const savedAddress = addresses.find((a) => a.id === id)
if (savedAddress) {
onSelect(savedAddress as HttpTypes.StoreCartAddress)
}
}
const selectedAddress = useMemo(() => {
if (!addressInput || !addresses?.length) return undefined
return addresses.find((a) => compareAddresses(a, addressInput))
}, [addresses, addressInput])
return (
<Listbox onChange={handleSelect} value={selectedAddress?.id || null}>
<div className="relative">
<Listbox.Button
className="relative w-full flex justify-between items-center px-4 py-[10px] text-left bg-white cursor-default focus:outline-none border rounded-rounded focus-visible:ring-2 focus-visible:ring-opacity-75 focus-visible:ring-white focus-visible:ring-offset-gray-300 focus-visible:ring-offset-2 focus-visible:border-gray-300 text-base-regular"
data-testid="shipping-address-select"
>
{({ open }) => (
<>
<span className="block truncate">
{selectedAddress
? selectedAddress.address_1
: "Choose an address"}
</span>
<ChevronUpDown
className={clx("transition-rotate duration-200", {
"transform rotate-180": open,
})}
/>
</>
)}
</Listbox.Button>
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Listbox.Options
className="absolute z-20 w-full overflow-auto text-small-regular bg-white border border-top-0 max-h-60 focus:outline-none sm:text-sm"
data-testid="shipping-address-options"
>
{addresses.map((address) => {
return (
<Listbox.Option
key={address.id}
value={address.id}
className="cursor-default select-none relative pl-6 pr-10 hover:bg-gray-50 py-4"
data-testid="shipping-address-option"
>
<div className="flex gap-x-4 items-start">
<Radio
checked={selectedAddress?.id === address.id}
data-testid="shipping-address-radio"
/>
<div className="flex flex-col">
<span className="text-left text-base-semi">
{address.first_name} {address.last_name}
</span>
{address.company && (
<span className="text-small-regular text-ui-fg-base">
{address.company}
</span>
)}
<div className="flex flex-col text-left text-base-regular mt-2">
<span>
{address.address_1}
{address.address_2 && (
<span>, {address.address_2}</span>
)}
</span>
<span>
{address.postal_code}, {address.city}
</span>
<span>
{address.province && `${address.province}, `}
{address.country_code?.toUpperCase()}
</span>
</div>
</div>
</div>
</Listbox.Option>
)
})}
</Listbox.Options>
</Transition>
</div>
</Listbox>
)
}
export default AddressSelect