@balby/booking-search 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@balby/booking-search",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Componente React agnostico per ricerca booking, ispirato a Booking.com",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "type": "module",
12
+ "files": [
13
+ "dist",
14
+ "src",
15
+ "README.md"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/AlbertoBarrago/booking-search.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/AlbertoBarrago/booking-search/issues"
23
+ },
24
+ "homepage": "https://github.com/AlbertoBarrago/booking-search#readme",
25
+ "keywords": [
26
+ "react",
27
+ "booking",
28
+ "search",
29
+ "widget",
30
+ "typescript",
31
+ "shadcn",
32
+ "tailwind",
33
+ "react-component",
34
+ "booking-search",
35
+ "date-picker",
36
+ "location-search"
37
+ ],
38
+ "author": "Alberto Barrago",
39
+ "license": "MIT",
40
+ "engines": {
41
+ "node": ">=18.0.0"
42
+ },
43
+ "scripts": {
44
+ "dev": "bun run build:css && bun --watch index.ts",
45
+ "dev:css": "bun run build:css && bunx concurrently \"bun --watch index.ts\" \"bun run watch:css\"",
46
+ "build": "bun run build:css && bun build src/index.ts --outdir=dist --target=browser --format=esm",
47
+ "build:css": "tailwindcss -i ./src/styles/globals.css -o ./src/styles/output.css --minify",
48
+ "watch:css": "tailwindcss -i ./src/styles/globals.css -o ./src/styles/output.css --watch",
49
+ "type-check": "tsc --noEmit",
50
+ "test": "bun test src/components/booking-search/tests/",
51
+ "test:coverage": "bun test --coverage src/components/booking-search/tests/",
52
+ "prepublishOnly": "bun run type-check && bun run test && bun run build",
53
+ "ts:check": "tsc --noEmit"
54
+ },
55
+ "devDependencies": {
56
+ "@happy-dom/global-registrator": "^20.0.11",
57
+ "@testing-library/react": "^16.3.1",
58
+ "@types/bun": "latest",
59
+ "@types/react": "19.2.7",
60
+ "@types/react-dom": "19.2.3",
61
+ "autoprefixer": "^10.4.23",
62
+ "happy-dom": "^20.0.11",
63
+ "postcss": "^8.5.6",
64
+ "tailwindcss": "3.4.17",
65
+ "typescript": "^5.9.3"
66
+ },
67
+ "peerDependencies": {
68
+ "react": "19.2.3",
69
+ "react-dom": "19.2.3",
70
+ "tailwindcss": ">=3.0.0"
71
+ },
72
+ "dependencies": {
73
+ "@radix-ui/react-dialog": "^1.1.15",
74
+ "@radix-ui/react-popover": "^1.1.15",
75
+ "@radix-ui/react-slot": "^1.2.4",
76
+ "class-variance-authority": "^0.7.1",
77
+ "clsx": "^2.1.1",
78
+ "cmdk": "^1.1.1",
79
+ "date-fns": "^4.1.0",
80
+ "lucide-react": "^0.562.0",
81
+ "react-day-picker": "8.10.1",
82
+ "tailwind-merge": "^3.4.0"
83
+ }
84
+ }
@@ -0,0 +1,240 @@
1
+ import * as React from "react"
2
+ import {DayPicker} from "react-day-picker"
3
+ import {format, parseISO, differenceInDays} from "date-fns"
4
+ import {enUS} from "date-fns/locale"
5
+ import {Calendar} from "lucide-react"
6
+ import {cn} from "../../lib/utils"
7
+ import type {DateRangePickerProps, AvailabilityDay} from "../../types/booking"
8
+ import {Popover, PopoverContent, PopoverTrigger} from "./ui/popover"
9
+
10
+ export function DateRangePicker({
11
+ availability,
12
+ value,
13
+ onChange,
14
+ disabled = false,
15
+ className,
16
+ minNights,
17
+ tabIndex
18
+ }: DateRangePickerProps) {
19
+ const [open, setOpen] = React.useState(false)
20
+ const [localValue, setLocalValue] = React.useState(value)
21
+
22
+ React.useEffect(() => {
23
+ if (open) {
24
+ setLocalValue(value)
25
+ }
26
+ }, [open, value])
27
+
28
+ const availabilityMap = React.useMemo(() => {
29
+ const map = new Map<string, AvailabilityDay>()
30
+ availability.forEach((day) => {
31
+ map.set(day.date, day)
32
+ })
33
+ return map
34
+ }, [availability])
35
+
36
+ const disabledDays = React.useMemo(() => {
37
+ const disabled: Date[] = []
38
+ availability.forEach((day) => {
39
+ if (!day.isAvailable) {
40
+ disabled.push(parseISO(day.date))
41
+ }
42
+ })
43
+
44
+ if (minNights && localValue.from && !localValue.to) {
45
+ const minCheckoutDate = new Date(localValue.from)
46
+ minCheckoutDate.setDate(minCheckoutDate.getDate() + minNights)
47
+
48
+ availability.forEach((day) => {
49
+ const dayDate = parseISO(day.date)
50
+ if (localValue && localValue.from && dayDate > localValue.from && dayDate < minCheckoutDate) {
51
+ disabled.push(dayDate)
52
+ }
53
+ })
54
+ }
55
+
56
+ return disabled
57
+ }, [availability, minNights, localValue.from, localValue.to])
58
+
59
+ const handleDayClick = (day: Date) => {
60
+ if (!localValue.from || (localValue.from && localValue.to)) {
61
+ setLocalValue({from: day, to: null})
62
+ } else {
63
+ if (day < localValue.from) {
64
+ setLocalValue({from: day, to: localValue.from})
65
+ } else {
66
+ if (minNights) {
67
+ const nights = differenceInDays(day, localValue.from)
68
+ if (nights < minNights) {
69
+ return
70
+ }
71
+ }
72
+ setLocalValue({from: localValue.from, to: day})
73
+ }
74
+ }
75
+ }
76
+
77
+ const handleConfirm = () => {
78
+ onChange(localValue)
79
+ setOpen(false)
80
+ }
81
+
82
+ const handleCancel = () => {
83
+ setLocalValue(value)
84
+ setOpen(false)
85
+ }
86
+
87
+ const renderDay = (day: Date) => {
88
+ const dateStr = format(day, "yyyy-MM-dd")
89
+ const dayData = availabilityMap.get(dateStr)
90
+
91
+ return (
92
+ <div className="flex flex-col items-center justify-center">
93
+ <span>{format(day, "d")}</span>
94
+ {dayData && dayData.isAvailable && (
95
+ <span className="text-[10px] font-semibold text-slate-600 leading-none mt-0.5">
96
+ €{dayData.price}
97
+ </span>
98
+ )}
99
+ </div>
100
+ )
101
+ }
102
+
103
+ const formatDateRange = () => {
104
+ if (value.from && value.to) {
105
+ return `${format(value.from, "dd MMM", {locale: enUS})} - ${format(value.to, "dd MMM", {locale: enUS})}`
106
+ }
107
+ if (value.from) {
108
+ return format(value.from, "dd MMM yyyy", {locale: enUS})
109
+ }
110
+ return "Select Date Range"
111
+ }
112
+
113
+ const formatLocalDateRange = () => {
114
+ if (localValue.from && localValue.to) {
115
+ const nights = differenceInDays(localValue.to, localValue.from)
116
+ return `${nights} ${nights === 1 ? 'night' : 'nights'}`
117
+ }
118
+ if (localValue.from) {
119
+ if (minNights && minNights > 1) {
120
+ return `Select check-out (min. ${minNights} nights)`
121
+ }
122
+ return "Select check-out"
123
+ }
124
+ return "Select Date Range"
125
+ }
126
+
127
+ const modifiers: Record<string, Date | Date[]> = {
128
+ disabled: disabledDays,
129
+ }
130
+
131
+ if (localValue.from) {
132
+ modifiers.start = localValue.from
133
+ }
134
+
135
+ if (localValue.to) {
136
+ modifiers.end = localValue.to
137
+ }
138
+
139
+ const modifiersClassNames = {
140
+ start: "rdp-day_range_start",
141
+ end: "rdp-day_range_end",
142
+ disabled: "rdp-day_disabled",
143
+ }
144
+
145
+ return (
146
+ <Popover open={open} onOpenChange={setOpen}>
147
+ <PopoverTrigger asChild>
148
+ <button
149
+ type="button"
150
+ aria-label="Select Date Range"
151
+ disabled={disabled}
152
+ tabIndex={tabIndex}
153
+ className={cn(
154
+ "flex h-14 w-full items-center gap-3 rounded-lg border bg-white px-4 py-3 text-left text-sm transition-all hover:bg-slate-50 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 relative",
155
+ open
156
+ ? "border-blue-500 ring-2 ring-blue-500 shadow-lg z-10"
157
+ : "border-slate-300",
158
+ className
159
+ )}
160
+ >
161
+ <Calendar className="h-5 w-5 text-slate-500 flex-shrink-0" aria-hidden="true"/>
162
+ <div className="flex flex-col min-w-0 flex-1">
163
+ <span className="text-xs font-medium text-slate-500">
164
+ Check-in - Check-out
165
+ </span>
166
+ <span
167
+ className={cn(
168
+ "font-medium truncate",
169
+ value.from ? "text-slate-900" : "text-slate-400"
170
+ )}
171
+ >
172
+ {formatDateRange()}
173
+ </span>
174
+ </div>
175
+ </button>
176
+ </PopoverTrigger>
177
+ <PopoverContent
178
+ className="w-auto p-0 shadow-2xl"
179
+ align="start"
180
+ sideOffset={8}
181
+ onInteractOutside={(e) => {
182
+ e.preventDefault()
183
+ }}
184
+ >
185
+ <div className="p-4">
186
+ <DayPicker
187
+ mode="range"
188
+ defaultMonth={localValue.from || new Date()}
189
+ selected={{
190
+ from: localValue.from || undefined,
191
+ to: localValue.to || undefined,
192
+ }}
193
+ onDayClick={handleDayClick}
194
+ disabled={disabledDays}
195
+ modifiers={modifiers}
196
+ modifiersClassNames={modifiersClassNames}
197
+ locale={enUS}
198
+ numberOfMonths={2}
199
+ className="rdp-custom"
200
+ formatters={{
201
+ formatDay: renderDay,
202
+ }}
203
+ />
204
+
205
+ <div className="border-t border-slate-200 p-4 space-y-3">
206
+ <div className="text-center">
207
+ <p className="text-sm font-medium text-slate-700">
208
+ {formatLocalDateRange()}
209
+ </p>
210
+ </div>
211
+
212
+ <div className="flex gap-2">
213
+ <button
214
+ type="button"
215
+ onClick={handleCancel}
216
+ className="flex-1 rounded-lg border-2 border-slate-300 bg-white px-4 py-2.5 text-sm font-semibold text-slate-700 transition-colors hover:bg-slate-50 focus:outline-none focus:ring-2 focus:ring-slate-500 focus:ring-offset-2"
217
+ >
218
+ Cancel
219
+ </button>
220
+ <button
221
+ type="button"
222
+ onClick={handleConfirm}
223
+ disabled={
224
+ !localValue.from ||
225
+ !localValue.to ||
226
+ (minNights && localValue.from && localValue.to
227
+ ? differenceInDays(localValue.to, localValue.from) < minNights
228
+ : false)
229
+ }
230
+ className="flex-1 rounded-lg bg-blue-600 px-4 py-2.5 text-sm font-semibold text-white transition-colors hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:bg-slate-300 disabled:hover:bg-slate-300"
231
+ >
232
+ Confirm
233
+ </button>
234
+ </div>
235
+ </div>
236
+ </div>
237
+ </PopoverContent>
238
+ </Popover>
239
+ )
240
+ }
@@ -0,0 +1,181 @@
1
+ import * as React from "react"
2
+ import { Users, Minus, Plus } from "lucide-react"
3
+ import { cn } from "../../lib/utils"
4
+ import type { GuestSelectorProps, GuestStepperProps } from "../../types/booking"
5
+ import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"
6
+
7
+ function GuestStepper({
8
+ label,
9
+ description,
10
+ value,
11
+ onIncrement,
12
+ onDecrement,
13
+ min = 0,
14
+ max = 99,
15
+ }: GuestStepperProps) {
16
+ const canDecrement = value > min
17
+ const canIncrement = value < max
18
+
19
+ return (
20
+ <div className="flex items-center justify-between py-3">
21
+ <div className="flex flex-col">
22
+ <span className="text-sm font-medium text-slate-900">{label}</span>
23
+ {description && (
24
+ <span className="text-xs text-slate-500">{description}</span>
25
+ )}
26
+ </div>
27
+ <div className="flex items-center gap-3">
28
+ <button
29
+ type="button"
30
+ onClick={onDecrement}
31
+ disabled={!canDecrement}
32
+ aria-label={`Decrease ${label.toLowerCase()}`}
33
+ className={cn(
34
+ "flex h-8 w-8 items-center justify-center rounded-full border-2 border-blue-500 text-blue-500 transition-all hover:bg-blue-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:border-slate-300 disabled:text-slate-300 disabled:hover:bg-transparent"
35
+ )}
36
+ >
37
+ <Minus className="h-4 w-4" aria-hidden="true" />
38
+ </button>
39
+ <span
40
+ className="w-8 text-center text-sm font-medium text-slate-900"
41
+ aria-live="polite"
42
+ aria-atomic="true"
43
+ >
44
+ {value}
45
+ </span>
46
+ <button
47
+ type="button"
48
+ onClick={onIncrement}
49
+ disabled={!canIncrement}
50
+ aria-label={`Increase ${label.toLowerCase()}`}
51
+ className={cn(
52
+ "flex h-8 w-8 items-center justify-center rounded-full border-2 border-blue-500 bg-blue-500 text-white transition-all hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:border-slate-300 disabled:bg-slate-300 disabled:hover:bg-slate-300"
53
+ )}
54
+ >
55
+ <Plus className="h-4 w-4" aria-hidden="true" />
56
+ </button>
57
+ </div>
58
+ </div>
59
+ )
60
+ }
61
+
62
+ export function GuestSelector({
63
+ value,
64
+ onChange,
65
+ maxAdults = 30,
66
+ maxChildren = 10,
67
+ disabled = false,
68
+ className,
69
+ tabIndex
70
+ }: GuestSelectorProps) {
71
+ const [open, setOpen] = React.useState(false)
72
+ const [localValue, setLocalValue] = React.useState(value)
73
+
74
+ React.useEffect(() => {
75
+ if (open) {
76
+ setLocalValue(value)
77
+ }
78
+ }, [open, value])
79
+
80
+ const handleAdultsChange = (delta: number) => {
81
+ const newValue = Math.max(1, Math.min(maxAdults, localValue.adults + delta))
82
+ setLocalValue({ ...localValue, adults: newValue })
83
+ }
84
+
85
+ const handleChildrenChange = (delta: number) => {
86
+ const newValue = Math.max(0, Math.min(maxChildren, localValue.children + delta))
87
+ setLocalValue({ ...localValue, children: newValue })
88
+ }
89
+
90
+ const handleConfirm = () => {
91
+ onChange(localValue)
92
+ setOpen(false)
93
+ }
94
+
95
+ const formatGuestText = () => {
96
+ const parts: string[] = []
97
+
98
+ if (value.adults === 1) {
99
+ parts.push("1 adulto")
100
+ } else {
101
+ parts.push(`${value.adults} adulti`)
102
+ }
103
+
104
+ if (value.children > 0) {
105
+ if (value.children === 1) {
106
+ parts.push("1 bambino")
107
+ } else {
108
+ parts.push(`${value.children} bambini`)
109
+ }
110
+ }
111
+
112
+ return parts.join(", ")
113
+ }
114
+
115
+ return (
116
+ <Popover
117
+ open={open}
118
+ onOpenChange={(newOpen) => {
119
+ setOpen(newOpen)
120
+ }}
121
+ >
122
+ <PopoverTrigger asChild>
123
+ <button
124
+ type="button"
125
+ aria-label="Select guests"
126
+ disabled={disabled}
127
+ tabIndex={tabIndex}
128
+ className={cn(
129
+ "flex h-14 w-full items-center gap-3 rounded-lg border border-slate-300 bg-white px-4 py-3 text-left text-sm transition-colors hover:bg-slate-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
130
+ className
131
+ )}
132
+ >
133
+ <Users className="h-5 w-5 text-slate-500 flex-shrink-0" aria-hidden="true" />
134
+ <div className="flex flex-col min-w-0 flex-1">
135
+ <span className="text-xs font-medium text-slate-500">Guests</span>
136
+ <span className="font-medium text-slate-900 truncate">{formatGuestText()}</span>
137
+ </div>
138
+ </button>
139
+ </PopoverTrigger>
140
+ <PopoverContent
141
+ className="w-[320px]"
142
+ align="start"
143
+ onInteractOutside={(e) => {
144
+ e.preventDefault()
145
+ }}
146
+ >
147
+ <div className="space-y-1">
148
+ <GuestStepper
149
+ label="Adulti"
150
+ description="Età 18+"
151
+ value={localValue.adults}
152
+ onIncrement={() => handleAdultsChange(1)}
153
+ onDecrement={() => handleAdultsChange(-1)}
154
+ min={1}
155
+ max={maxAdults}
156
+ />
157
+ <div className="border-t border-slate-200" />
158
+ <GuestStepper
159
+ label="Bambini"
160
+ description="Età 0-17"
161
+ value={localValue.children}
162
+ onIncrement={() => handleChildrenChange(1)}
163
+ onDecrement={() => handleChildrenChange(-1)}
164
+ min={0}
165
+ max={maxChildren}
166
+ />
167
+ <div className="border-t border-slate-200 mt-3" />
168
+ <div className="pt-3">
169
+ <button
170
+ type="button"
171
+ onClick={handleConfirm}
172
+ className="w-full rounded-lg bg-blue-600 px-4 py-2.5 text-sm font-semibold text-white transition-colors hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
173
+ >
174
+ Conferma
175
+ </button>
176
+ </div>
177
+ </div>
178
+ </PopoverContent>
179
+ </Popover>
180
+ )
181
+ }