-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[STK-33]: create stack frequency section (#31)
- Loading branch information
Showing
12 changed files
with
411 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
"use client"; | ||
|
||
import { format, isToday, isTomorrow } from "date-fns"; | ||
|
||
import { Button, Calendar, Icon } from "@/ui"; | ||
import { Popover } from "@headlessui/react"; | ||
|
||
import { Dispatch, Ref, SetStateAction, useEffect, useState } from "react"; | ||
import { usePopper } from "react-popper"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
interface DatePickerProps { | ||
dateTime: Date; | ||
setDateTime: Dispatch<SetStateAction<Date>>; | ||
className?: string; | ||
timeCaption: string; | ||
fromDate?: Date; | ||
} | ||
|
||
export function DatePicker({ | ||
dateTime, | ||
setDateTime, | ||
className, | ||
timeCaption, | ||
fromDate, | ||
}: DatePickerProps) { | ||
const [currentDate, setCurrentDate] = useState<Date>(new Date(dateTime)); | ||
const [hours, setHours] = useState(format(currentDate, "HH")); | ||
const [minutes, setMinutes] = useState(format(currentDate, "mm")); | ||
|
||
const [referenceElement, setReferenceElement] = useState< | ||
HTMLButtonElement | undefined | ||
>(); | ||
const [popperElement, setPopperElement] = useState< | ||
HTMLDivElement | undefined | ||
>(); | ||
|
||
const { styles, attributes } = usePopper(referenceElement, popperElement, { | ||
placement: "bottom", | ||
}); | ||
|
||
const formattedDate = () => { | ||
if (isToday(dateTime)) return format(dateTime, "'Today at' HH:mm"); | ||
if (isTomorrow(dateTime)) return format(dateTime, "'Tomorrow at' HH:mm"); | ||
|
||
return format(dateTime, "dd MMM Y 'at' HH:mm"); | ||
}; | ||
|
||
useEffect(() => { | ||
const newDate = new Date(dateTime); | ||
setCurrentDate(newDate); | ||
setHours(format(newDate, "HH")); | ||
setMinutes(format(newDate, "mm")); | ||
}, [dateTime]); | ||
|
||
return ( | ||
<Popover> | ||
{({ open }) => ( | ||
<> | ||
<Popover.Button | ||
ref={setReferenceElement as Ref<HTMLButtonElement>} | ||
className={twMerge( | ||
"flex justify-between items-center focus:border-0", | ||
className | ||
)} | ||
> | ||
<span>{formattedDate()}</span> | ||
<Icon name="caret-down" className="mr-2 h-4 w-4 text-black" /> | ||
</Popover.Button> | ||
{open && ( | ||
<> | ||
<div className="fixed inset-0 bg-black/30" aria-hidden="true" /> | ||
<Popover.Panel | ||
ref={setPopperElement as Ref<HTMLDivElement>} | ||
style={styles.popper} | ||
{...attributes.popper} | ||
className="w-fit h-fit m-auto max-md:!fixed max-md:!inset-0 max-md:!flex max-md:!items-center max-md:!justify-center max-md:!transform" /* This styles will make it hover on mobile */ | ||
> | ||
{({ close }) => ( | ||
<div className="flex flex-col space-y-2 bg-white divide-y divide-surface-50 rounded-2xl border border-surface-50"> | ||
<Calendar | ||
mode="single" | ||
selected={currentDate} | ||
defaultMonth={currentDate} | ||
onSelect={(newDate: Date | undefined) => { | ||
if (!newDate) return; | ||
newDate.setHours(currentDate.getHours()); | ||
newDate.setMinutes(currentDate.getMinutes()); | ||
setCurrentDate(newDate); | ||
}} | ||
initialFocus | ||
className="" | ||
fromDate={fromDate || new Date()} | ||
/> | ||
<div className="flex flex-col space-y-2 p-4"> | ||
<div className="flex justify-between items-center"> | ||
<span>{timeCaption}</span> | ||
<div className="flex space-x-2 items-center"> | ||
<div className="flex items-center border border-surface-75 rounded-xl py-2 px-3 text-em-low"> | ||
<input | ||
className="outline-none font-semibold flex-grow text-sm w-5 text-center" | ||
type="number" | ||
pattern="[0-9]*" | ||
value={hours} | ||
onKeyDown={(evt) => | ||
["e", "E", "+", "-"].includes(evt.key) && | ||
evt.preventDefault() | ||
} | ||
onChange={(event) => { | ||
const hours = event.target.value; | ||
const hoursNumber = Number(hours); | ||
if ( | ||
hoursNumber >= 0 && | ||
hoursNumber < 24 && | ||
hours.length < 3 | ||
) { | ||
setHours(hours); | ||
} | ||
}} | ||
/> | ||
</div> | ||
<span>:</span> | ||
<div className="flex items-center border border-surface-75 rounded-xl py-2 px-3 text-em-low"> | ||
<input | ||
className="outline-none font-semibold flex-grow text-sm w-5 text-center" | ||
type="number" | ||
pattern="[0-9]*" | ||
value={minutes} | ||
onKeyDown={(evt) => | ||
["e", "E", "+", "-"].includes(evt.key) && | ||
evt.preventDefault() | ||
} | ||
onChange={(event) => { | ||
const minutes = event.target.value; | ||
const minutesNumber = Number(minutes); | ||
if ( | ||
minutesNumber >= 0 && | ||
minutesNumber < 60 && | ||
minutes.length < 3 | ||
) { | ||
setMinutes(minutes); | ||
} | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<Button | ||
action="primary" | ||
onClick={() => { | ||
const newDate = new Date(currentDate); | ||
newDate.setHours(Number(hours)); | ||
newDate.setMinutes(Number(minutes)); | ||
setDateTime(newDate); | ||
close(); | ||
}} | ||
> | ||
Set Date and Time | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
</Popover.Panel> | ||
</> | ||
)} | ||
</> | ||
)} | ||
</Popover> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./TokenPicker"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.