-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMandalart.tsx
More file actions
64 lines (51 loc) · 1.55 KB
/
Mandalart.tsx
File metadata and controls
64 lines (51 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { useState } from 'react';
import { Main, Sub } from './Square';
import * as styles from './Mandalart.css';
import { MOCK_MANDALART_DATA } from './mock';
export type Cycle = 'DAILY' | 'WEEKLY' | 'ONCE';
export type MandalartType = 'TODO_SUB' | 'TODO_MAIN' | 'TODO_EDIT' | 'MY_MANDAL';
export interface SubGoal {
title: string;
position: number;
cycle: Cycle;
}
interface MandalartProps {
mainGoal?: string;
subGoals?: SubGoal[];
type: MandalartType;
onGoalClick?: (position: number) => void;
}
const CENTER_INDEX = 4;
const Mandalart = ({
mainGoal = MOCK_MANDALART_DATA.mainGoal,
subGoals = MOCK_MANDALART_DATA.subGoals,
type,
onGoalClick,
}: MandalartProps) => {
const [selectedGoal, setSelectedGoal] = useState<number | null>(null);
const handleGoalClick = (position: number) => {
setSelectedGoal(selectedGoal === position ? null : position);
onGoalClick?.(position);
};
const renderSquare = (index: number) => {
if (index === CENTER_INDEX) {
return <Main key={index} content={mainGoal} type={type} />;
}
const subGoalIndex = index > CENTER_INDEX ? index - 1 : index;
const subGoal = subGoals[subGoalIndex];
return (
<Sub
key={index}
content={subGoal.title}
isCompleted={selectedGoal === subGoalIndex}
onClick={() => handleGoalClick(subGoalIndex)}
type={type}
/>
);
};
const squares = Array(9)
.fill(null)
.map((_, index) => renderSquare(index));
return <div className={styles.grid[type]}>{squares}</div>;
};
export default Mandalart;