implemented scroll into view
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { FC } from "react";
|
import { FC, useEffect, useRef } from "react";
|
||||||
import { formatYearMonth } from "../lib/format";
|
import { formatYearMonth } from "../lib/format";
|
||||||
import { YearMonth } from "../lib/db-types";
|
import { YearMonth } from "../lib/db-types";
|
||||||
import { formatCurrency } from "../lib/formatStrings";
|
import { formatCurrency } from "../lib/formatStrings";
|
||||||
import { redirect, useParams, useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
export interface MonthCardProps {
|
export interface MonthCardProps {
|
||||||
yearMonth: YearMonth,
|
yearMonth: YearMonth,
|
||||||
@@ -16,12 +16,20 @@ export interface MonthCardProps {
|
|||||||
export const MonthCard:FC<MonthCardProps> = ({ yearMonth, children, monthlyExpense, expanded }) => {
|
export const MonthCard:FC<MonthCardProps> = ({ yearMonth, children, monthlyExpense, expanded }) => {
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const elRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
// setting the `month` will activate the accordion belonging to that month
|
// 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(
|
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} />
|
<input type="radio" name="my-accordion-3" checked={expanded} onChange={handleChange} />
|
||||||
<div className="collapse-title text-xl font-medium">
|
<div className="collapse-title text-xl font-medium">
|
||||||
{`${formatYearMonth(yearMonth)}`}
|
{`${formatYearMonth(yearMonth)}`}
|
||||||
|
|||||||
Reference in New Issue
Block a user