Skip to content

Commit

Permalink
feat: add prefecture select highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
tosaken1116 committed Oct 5, 2023
1 parent d6b06d1 commit 9b682ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/components/model/PopulationChart/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,11 +20,25 @@ export type VisibleData = z.infer<typeof VisibleDataSchema>;

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<string[]>([]);

const handleActivePrefectures = (prefecture: string): void => {
setActivePrefectures((prevActivePrefectures) => {
if (prevActivePrefectures.includes(prefecture)) {
return prevActivePrefectures.filter(
(prevPrefecture) => prevPrefecture !== prefecture
);
}
return [...prevActivePrefectures, prefecture];
});
};

const params = useMemo(
() =>
Expand Down Expand Up @@ -73,5 +87,7 @@ export const usePopulationChart = (): IUsePopulationChart => {

return {
visibleData,
activePrefectures,
handleActivePrefectures,
};
};
2 changes: 2 additions & 0 deletions src/components/model/PopulationChart/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ describe('Population Chart model Component', () => {
],
},
]}
activePrefectures={[]}
handleActivePrefectures={jest.fn()}
/>
);

Expand Down
41 changes: 37 additions & 4 deletions src/components/model/PopulationChart/presentations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props> = ({ data }) => (
export const PopulationChartPresentation: React.FC<Props> = ({
data,
activePrefectures,
handleActivePrefectures,
}) => (
<div className={container}>
<ResponsiveContainer width="99%" height="80%">
<LineChart
Expand All @@ -42,14 +50,32 @@ export const PopulationChartPresentation: React.FC<Props> = ({ data }) => (
/>
<XAxis dataKey="year" type="category" allowDuplicatedCategory={false} />

<Legend />
<Legend
onClick={(data: { value: string }): void => {
handleActivePrefectures(data.value);
}}
/>
<Tooltip />
{data.map(({ prefecture, data }) => (
<Line
key={prefecture}
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)}
/>
))}
</LineChart>
Expand All @@ -58,10 +84,17 @@ export const PopulationChartPresentation: React.FC<Props> = ({ data }) => (
);

export const PopulationChartContainer: React.FC = () => {
const { visibleData } = usePopulationChart();
const { visibleData, handleActivePrefectures, activePrefectures } =
usePopulationChart();

if (visibleData == null || visibleData.length === 0) {
return <PopulationChartEmptyPresentation />;
}
return <PopulationChartPresentation data={visibleData} />;
return (
<PopulationChartPresentation
data={visibleData}
activePrefectures={activePrefectures}
handleActivePrefectures={handleActivePrefectures}
/>
);
};

0 comments on commit 9b682ea

Please sign in to comment.