diff --git a/src/components/model/PopulationChart/hooks/index.tsx b/src/components/model/PopulationChart/hooks/index.tsx index bcdcc04..b94023a 100644 --- a/src/components/model/PopulationChart/hooks/index.tsx +++ b/src/components/model/PopulationChart/hooks/index.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react'; +import { useMemo, useState } from 'react'; import { useSearchParams } from 'next/navigation'; import { array, number, object, string } from 'zod'; @@ -20,11 +20,25 @@ export type VisibleData = z.infer; type IUsePopulationChart = { visibleData: VisibleData | null; + activePrefectures: string[]; + handleActivePrefectures: (prefecture: string) => void; }; export const usePopulationChart = (): IUsePopulationChart => { const searchParams = useSearchParams(); const isSearchParamNull = searchParams == null; + const [activePrefectures, setActivePrefectures] = useState([]); + + const handleActivePrefectures = (prefecture: string): void => { + setActivePrefectures((prevActivePrefectures) => { + if (prevActivePrefectures.includes(prefecture)) { + return prevActivePrefectures.filter( + (prevPrefecture) => prevPrefecture !== prefecture + ); + } + return [...prevActivePrefectures, prefecture]; + }); + }; const params = useMemo( () => @@ -73,5 +87,7 @@ export const usePopulationChart = (): IUsePopulationChart => { return { visibleData, + activePrefectures, + handleActivePrefectures, }; }; diff --git a/src/components/model/PopulationChart/index.test.tsx b/src/components/model/PopulationChart/index.test.tsx index ee15599..91b4ab0 100644 --- a/src/components/model/PopulationChart/index.test.tsx +++ b/src/components/model/PopulationChart/index.test.tsx @@ -94,6 +94,8 @@ describe('Population Chart model Component', () => { ], }, ]} + activePrefectures={[]} + handleActivePrefectures={jest.fn()} /> ); diff --git a/src/components/model/PopulationChart/presentations/index.tsx b/src/components/model/PopulationChart/presentations/index.tsx index 9ef212b..825aa15 100644 --- a/src/components/model/PopulationChart/presentations/index.tsx +++ b/src/components/model/PopulationChart/presentations/index.tsx @@ -16,11 +16,19 @@ import { PopulationChartEmptyPresentation } from './empty'; import type { VisibleData } from '../hooks'; +import { PRIMARY } from '@/constants/colors'; + type Props = { data: VisibleData; + activePrefectures: string[]; + handleActivePrefectures: (prefecture: string) => void; }; -export const PopulationChartPresentation: React.FC = ({ data }) => ( +export const PopulationChartPresentation: React.FC = ({ + data, + activePrefectures, + handleActivePrefectures, +}) => (
= ({ data }) => ( /> - + { + handleActivePrefectures(data.value); + }} + /> {data.map(({ prefecture, data }) => ( = ({ data }) => ( dataKey="value" data={data} name={prefecture} + stroke={ + activePrefectures.length > 0 && + !activePrefectures.includes(prefecture) + ? '#ccc' + : PRIMARY.light + } + strokeWidth={ + activePrefectures.length == 0 + ? 1 + : !activePrefectures.includes(prefecture) + ? 1 + : 4 + } + onClick={(): void => handleActivePrefectures(prefecture)} /> ))} @@ -58,10 +84,17 @@ export const PopulationChartPresentation: React.FC = ({ data }) => ( ); export const PopulationChartContainer: React.FC = () => { - const { visibleData } = usePopulationChart(); + const { visibleData, handleActivePrefectures, activePrefectures } = + usePopulationChart(); if (visibleData == null || visibleData.length === 0) { return ; } - return ; + return ( + + ); };