feat: modal calendar in edit tasks

This commit is contained in:
2025-04-22 12:31:55 +03:00
parent 607637b5ca
commit 7a98dbfe91
3 changed files with 209 additions and 151 deletions

View File

@@ -1,13 +1,18 @@
import { cn } from "@/utils/class-merge";
import { ClockIcon } from "@heroicons/react/24/outline";
import { FunctionComponent } from "preact";
import { Dispatch, StateUpdater } from "preact/hooks";
import { Calendar } from "primereact/calendar";
import { Dispatch, StateUpdater, useState } from "preact/hooks";
import { Calendar, CalendarPassThroughMethodOptions } from "primereact/calendar";
import { FormEvent } from "primereact/ts-helpers";
import Button from "./ui/Button";
import ModalWindow from "./ui/Modal";
interface ModalCalendarProps {
isOpen?: boolean;
setIsOpen?: Dispatch<StateUpdater<boolean>>;
onClose?: () => void;
value?: Date;
onChange?: (e: FormEvent<Date>) => void;
}
const TRANSITIONS = {
@@ -22,18 +27,30 @@ const TRANSITIONS = {
},
};
const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpen, onClose }) => {
const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpen, onClose, onChange, value }) => {
const [showTime, setShowTime] = useState(false);
return (
<ModalWindow isOpen={isOpen} setIsOpen={setIsOpen} onClose={onClose} className="md:h-[40rem] md:w-[30rem]">
<ModalWindow
isOpen={isOpen}
setIsOpen={setIsOpen}
onClose={onClose}
className="md:h-[40rem] md:w-[30rem]"
zIndex={60}
>
<div class="w-full flex-1 self-start">
<Calendar
inline
onChange={onChange}
value={value}
hourFormat="24"
showTime={showTime}
pt={{
root: ({ props }) => ({
className: cn("inline-flex max-w-full relative", {
root: ({ props }: CalendarPassThroughMethodOptions) => ({
className: cn("inline-flex w-full relative", {
"opacity-60 select-none pointer-events-none cursor-default": props.disabled,
}),
}),
input: ({ props }) => ({
input: ({ props }: CalendarPassThroughMethodOptions) => ({
root: {
className: cn(
"font-sans text-base text-gray-600 bg-white p-3 border border-gray-300 transition-colors duration-200 appearance-none",
@@ -46,11 +63,11 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
},
}),
dropdownButton: {
root: ({ props }) => ({
root: ({ props }: CalendarPassThroughMethodOptions) => ({
className: cn({ "rounded-l-none": props.icon }),
}),
},
panel: ({ props }) => ({
panel: ({ props }: CalendarPassThroughMethodOptions) => ({
className: cn("bg-[rgba(251,194,199,0.38)]", "min-w-full", {
"shadow-md border-0 absolute": !props.inline,
"inline-block overflow-x-auto border border-gray-300 p-2 rounded-lg": props.inline,
@@ -59,7 +76,7 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
header: {
className: cn(
"flex items-center justify-between",
"p-2 text-gray-700 bg-white font-semibold m-0 border-b border-gray-300 rounded-t-lg"
"p-2 text-gray-700 bg-[rgba(251,194,199,0.38)] font-semibold m-0 border-b border-gray-300 rounded-t-lg"
),
},
previousButton: {
@@ -89,9 +106,9 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
tableHeaderCell: { className: "p-2" },
weekDay: { className: "text-gray-600 " },
day: { className: "p-2" },
dayLabel: ({ context }) => ({
dayLabel: ({ context }: CalendarPassThroughMethodOptions) => ({
className: cn(
"w-10 h-10 rounded-full transition-shadow duration-200 border-transparent border",
"w-6 h-6 rounded-full transition-shadow duration-200 border-transparent border",
"flex items-center justify-center mx-auto overflow-hidden relative",
"focus:outline-none focus:outline-offset-0 focus:shadow-[0_0_0_0.2rem_rgba(191,219,254,1)] ",
{
@@ -105,7 +122,7 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
),
}),
monthPicker: { className: "my-2" },
month: ({ context }) => ({
month: ({ context }: CalendarPassThroughMethodOptions) => ({
className: cn(
"w-1/3 inline-flex items-center justify-center cursor-pointer overflow-hidden relative",
"p-2 transition-shadow duration-200 rounded-lg",
@@ -119,7 +136,7 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
yearPicker: {
className: cn("my-2"),
},
year: ({ context }) => ({
year: ({ context }: CalendarPassThroughMethodOptions) => ({
className: cn(
"w-1/2 inline-flex items-center justify-center cursor-pointer overflow-hidden relative",
"p-2 transition-shadow duration-200 rounded-lg",
@@ -131,7 +148,7 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
),
}),
timePicker: {
className: cn("flex justify-center items-center", "border-t-1 border-solid border-gray-300 p-2"),
className: cn("flex justify-center items-center", "border-t-1 border-solid border-gray-300"),
},
separatorContainer: { className: "flex items-center flex-col px-2" },
separator: { className: "text-xl" },
@@ -154,11 +171,27 @@ const ModalCalendar: FunctionComponent<ModalCalendarProps> = ({ isOpen, setIsOpe
},
groupContainer: { className: "flex" },
group: {
className: cn("flex-1", "border-l border-gray-300 pr-0.5 pl-0.5 pt-0 pb-0", "first:pl-0 first:border-l-0"),
className: cn(
"flex-1",
"border-l border-gray-300 pr-0.5 pl-0.5 pt-0 pb-0",
"first:pl-0 first:border-l-0"
),
},
transition: TRANSITIONS.overlay,
}}
/>
<p class={cn("mt-2 text-center text-xl font-semibold", { hidden: !value })}>
{value && Intl.DateTimeFormat("ru-RU", { day: "2-digit", month: "2-digit", year: "numeric" }).format(value)}
</p>
</div>
<Button
className="flex w-full flex-row items-center justify-center gap-2 self-end"
onClick={() => {
setShowTime(!showTime);
}}
>
<ClockIcon class="h-8" /> <p>Время</p>
</Button>
</ModalWindow>
);
};

View File

@@ -7,28 +7,35 @@ interface ModalWindowProps {
setIsOpen?: Dispatch<StateUpdater<boolean>>;
onClose?: () => void;
className?: string;
zIndex?: number;
}
const ModalWindow: FunctionComponent<ModalWindowProps> = ({ isOpen, children, setIsOpen, onClose, className = "" }) => {
const ModalWindow: FunctionComponent<ModalWindowProps> = ({
isOpen,
children,
setIsOpen,
onClose,
className = "",
zIndex,
}) => {
useEffect(() => {
if (isOpen) return;
if (onClose) onClose();
}, [isOpen]);
return (
isOpen && (
<div
onClick={(e) => {
e.stopPropagation();
if (setIsOpen) setIsOpen(false);
}}
class={cn(
"fixed top-0 left-0 z-50 flex h-screen w-screen cursor-pointer flex-col items-center justify-center bg-black/50",
{
hidden: !isOpen,
}
"fixed top-0 left-0 z-50 flex h-screen w-screen cursor-pointer flex-col items-center justify-center bg-black/50"
)}
style={{ zIndex: zIndex }}
>
<div
class={cn(
"h-[40rem] w-[95%] cursor-auto rounded-[4rem] bg-white px-8 py-12 md:me-[20rem] md:h-[20rem] md:w-[65%] md:px-16",
"flex h-[40rem] w-[95%] cursor-auto flex-col items-center justify-start rounded-[4rem] bg-white px-8 py-12 md:me-[20rem] md:h-[20rem] md:w-[65%] md:px-16",
className
)}
onClick={(e) => e.stopPropagation()}
@@ -36,6 +43,7 @@ const ModalWindow: FunctionComponent<ModalWindowProps> = ({ isOpen, children, se
{children}
</div>
</div>
)
);
};

View File

@@ -17,6 +17,7 @@ import {
} from "@heroicons/react/24/outline";
import { FunctionComponent } from "preact";
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
import { Nullable } from "primereact/ts-helpers";
import { ITask } from "./profile_tasks.dto";
import classes from "./profile_tasks.module.scss";
@@ -64,14 +65,24 @@ const ProfileTasks: FunctionComponent = () => {
localStorage.setItem("tasks", JSON.stringify(tasks));
}, [tasks]);
const [openModal, setIsOpen] = useState(false);
const [openModalCalendar, setOpenModalCalendar] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [isCreating, setIsCreating] = useState(false);
const [editContent, setEditContent] = useState<ITask | null>(null);
const taskNameRef = useRef<HTMLInputElement>(null);
const taskDescriptionRef = useRef<HTMLTextAreaElement>(null);
const [calendarDate, setCalendarDate] = useState<Nullable<Date>>();
return (
<div class={classes.container}>
<ModalCalendar isOpen />
<ModalCalendar
isOpen={openModalCalendar}
setIsOpen={setOpenModalCalendar}
onClose={() => {
setCalendarDate(null);
}}
onChange={(e) => isCreating && setCalendarDate(e.value)}
value={calendarDate!}
/>
<ModalWindow
isOpen={openModal}
setIsOpen={setIsOpen}
@@ -94,7 +105,13 @@ const ProfileTasks: FunctionComponent = () => {
</div>
</div>
<div class="flex flex-col items-center gap-6 self-center md:flex-row md:justify-start md:self-start">
<div class="flex h-full flex-row items-center gap-1 rounded-2xl bg-[rgba(251,194,199,0.38)] px-2 py-1">
<div
class="flex h-full flex-row items-center gap-1 rounded-2xl bg-[rgba(251,194,199,0.38)] px-2 py-1"
onClick={() => {
setOpenModalCalendar(true);
setCalendarDate(editContent.date);
}}
>
<CalendarDaysIcon class="size-10" />
<p>
{Intl.DateTimeFormat("ru-RU", {