implemented scroll into view

This commit is contained in:
2024-02-01 15:07:44 +01:00
parent 2261e83715
commit bccb00aaeb

View File

@@ -1,10 +1,10 @@
"use client";
import { FC } from "react";
import { FC, useEffect, useRef } from "react";
import { formatYearMonth } from "../lib/format";
import { YearMonth } from "../lib/db-types";
import { formatCurrency } from "../lib/formatStrings";
import { redirect, useParams, useRouter } from "next/navigation";
import { useRouter } from "next/navigation";
export interface MonthCardProps {
yearMonth: YearMonth,
@@ -16,12 +16,20 @@ export interface MonthCardProps {
export const MonthCard:FC<MonthCardProps> = ({ yearMonth, children, monthlyExpense, expanded }) => {
const router = useRouter();
const elRef = useRef<HTMLDivElement>(null);
// setting the `month` will activate the accordion belonging to that month
const handleChange = (event:any) => router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`)
const handleChange = (event:any) => router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`);
useEffect(() => {
if(expanded && elRef.current) {
// if the element i selected > scroll it into view
elRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, []);
return(
<div className="collapse collapse-plus bg-base-200 my-1">
<div className="collapse collapse-plus bg-base-200 my-1" ref={elRef}>
<input type="radio" name="my-accordion-3" checked={expanded} onChange={handleChange} />
<div className="collapse-title text-xl font-medium">
{`${formatYearMonth(yearMonth)}`}