From 3d8d66ccb392da32e45a776ff259fcdfb8b72cab Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 13 Sep 2023 19:35:56 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Implemented=20guests=20input=20c?= =?UTF-8?q?omponent=20=F0=9F=91=A8=E2=80=8D=F0=9F=92=BB=20Added=20function?= =?UTF-8?q?ality=20to=20handle=20guest=20counts=20=F0=9F=8E=A8=20Styled=20?= =?UTF-8?q?the=20guests=20input=20component=20=E2=9C=A8=20Enhanced=20user?= =?UTF-8?q?=20experience=20for=20guest=20selection=20=F0=9F=93=9D=20Added?= =?UTF-8?q?=20comments=20for=20better=20code=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../(HeroSearchForm)/GuestsInput.tsx | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/app/(client-components)/(HeroSearchForm)/GuestsInput.tsx diff --git a/src/app/(client-components)/(HeroSearchForm)/GuestsInput.tsx b/src/app/(client-components)/(HeroSearchForm)/GuestsInput.tsx new file mode 100644 index 0000000..25ba1ca --- /dev/null +++ b/src/app/(client-components)/(HeroSearchForm)/GuestsInput.tsx @@ -0,0 +1,143 @@ +"use client"; + +import React, { Fragment, useEffect, useState } from "react"; +import { Popover, Transition } from "@headlessui/react"; +import NcInputNumber from "@/components/NcInputNumber"; +import { FC } from "react"; +import ClearDataButton from "./ClearDataButton"; +import ButtonSubmit from "./ButtonSubmit"; +import { PathName } from "@/routers/types"; +import { UserPlusIcon } from "@heroicons/react/24/outline"; +import { GuestsObject } from "../type"; + +export interface GuestsInputProps { + fieldClassName?: string; + className?: string; + buttonSubmitHref?: PathName; + hasButtonSubmit?: boolean; +} + +const GuestsInput: FC = ({ + fieldClassName = "[ nc-hero-field-padding ]", + className = "[ nc-flex-1 ]", + buttonSubmitHref = "/listing-stay-map", + hasButtonSubmit = true, +}) => { + const [guestAdultsInputValue, setGuestAdultsInputValue] = useState(2); + const [guestChildrenInputValue, setGuestChildrenInputValue] = useState(1); + const [guestInfantsInputValue, setGuestInfantsInputValue] = useState(1); + + const handleChangeData = (value: number, type: keyof GuestsObject) => { + let newValue = { + guestAdults: guestAdultsInputValue, + guestChildren: guestChildrenInputValue, + guestInfants: guestInfantsInputValue, + }; + if (type === "guestAdults") { + setGuestAdultsInputValue(value); + newValue.guestAdults = value; + } + if (type === "guestChildren") { + setGuestChildrenInputValue(value); + newValue.guestChildren = value; + } + if (type === "guestInfants") { + setGuestInfantsInputValue(value); + newValue.guestInfants = value; + } + }; + + const totalGuests = + guestChildrenInputValue + guestAdultsInputValue + guestInfantsInputValue; + + return ( + + {({ open }) => ( + <> +
+ +
+ +
+
+ + {totalGuests || ""} Guests + + + {totalGuests ? "Guests" : "Add guests"} + +
+ + {!!totalGuests && open && ( + { + setGuestAdultsInputValue(0); + setGuestChildrenInputValue(0); + setGuestInfantsInputValue(0); + }} + /> + )} +
+ + {/* BUTTON SUBMIT OF FORM */} + {hasButtonSubmit && ( +
+ +
+ )} +
+ + {open && ( +
+ )} + + + handleChangeData(value, "guestAdults")} + max={10} + min={1} + label="Adults" + desc="Ages 13 or above" + /> + handleChangeData(value, "guestChildren")} + max={4} + label="Children" + desc="Ages 2–12" + /> + + handleChangeData(value, "guestInfants")} + max={4} + label="Infants" + desc="Ages 0–2" + /> + + + + )} +
+ ); +}; + +export default GuestsInput;