diff --git a/src/app/(client-components)/(HeroSearchFormSmall)/GuestsInput.tsx b/src/app/(client-components)/(HeroSearchFormSmall)/GuestsInput.tsx new file mode 100644 index 0000000..dfc9b3b --- /dev/null +++ b/src/app/(client-components)/(HeroSearchFormSmall)/GuestsInput.tsx @@ -0,0 +1,137 @@ +"use client"; + +import React, { useEffect, useState } from "react"; +import { FC } from "react"; +import ClearDataButton from "./ClearDataButton"; +import ButtonSubmit from "./ButtonSubmit"; +import useOutsideAlerter from "@/hooks/useOutsideAlerter"; +import { PathName } from "@/routers/types"; +import NcInputNumber from "@/components/NcInputNumber"; +import { GuestsObject } from "../type"; + +export interface GuestsInputProps { + className?: string; + fieldClassName?: string; + autoFocus?: boolean; + submitLink: PathName; +} + +const GuestsInput: FC = ({ + fieldClassName = "[ nc-hero-field-padding--small ]", + className = "", + autoFocus = false, + submitLink, +}) => { + const refContainer = React.createRef(); + const [isOpen, setIsOpen] = useState(false); + useOutsideAlerter(refContainer, () => setIsOpen(false)); + + const [guestAdultsInputValue, setGuestAdultsInputValue] = useState(2); + const [guestChildrenInputValue, setGuestChildrenInputValue] = useState(1); + const [guestInfantsInputValue, setGuestInfantsInputValue] = useState(1); + // + + useEffect(() => { + setIsOpen(autoFocus); + }, [autoFocus]); + + 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 ( +
+
+
{ + setIsOpen(!isOpen); + }} + > +
+ + {totalGuests || ""} Guests + + + {totalGuests ? "Guests" : "Add guests"} + +
+
+
+ {!!totalGuests && isOpen && ( + { + setGuestAdultsInputValue(0); + setGuestChildrenInputValue(0); + setGuestInfantsInputValue(0); + }} + /> + )} +
+
+ +
+
+ + {isOpen && ( +
+ )} + + {isOpen && ( +
+ 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;