-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCycleDropDown.tsx
More file actions
67 lines (54 loc) · 1.94 KB
/
CycleDropDown.tsx
File metadata and controls
67 lines (54 loc) · 1.94 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
65
66
67
import { useState, useEffect } from 'react';
import { IcDropdown } from '@/assets/svg';
import CycleList from '@/common/component/CycleDropDown/CycleList';
import * as styles from '@/common/component/CycleDropDown/CycleDropDown.css';
const CYCLE_TYPE = ['매일', '매주', '한 번'] as const;
type DisplayCycleType = (typeof CYCLE_TYPE)[number];
const CYCLE_MAPPING = {
매일: 'DAILY',
매주: 'WEEKLY',
'한 번': 'ONCE',
} as const;
type CycleType = 'DAILY' | 'WEEKLY' | 'ONCE';
type CycleDropDownProps = {
initialType?: CycleType;
onChange?: (type: CycleType) => void;
};
const CycleDropDown = ({ initialType = 'DAILY', onChange }: CycleDropDownProps) => {
const [isOpen, setIsOpen] = useState(false);
const [selectedType, setSelectedType] = useState<DisplayCycleType>(
(Object.entries(CYCLE_MAPPING).find(([, v]) => v === initialType)?.[0] as DisplayCycleType) ||
'매일',
);
useEffect(() => {
if (initialType) {
const label =
typeof initialType === 'string' &&
(initialType === 'DAILY' || initialType === 'WEEKLY' || initialType === 'ONCE')
? CYCLE_TYPE[Object.values(CYCLE_MAPPING).indexOf(initialType)]
: initialType;
setSelectedType(label);
}
}, [initialType]);
const toggleDropdown = () => {
setIsOpen((prev) => !prev);
};
const handleType = (type: DisplayCycleType) => {
setSelectedType(type);
setIsOpen(false);
onChange?.(CYCLE_MAPPING[type]);
};
const state = isOpen ? 'clicked' : 'default';
return (
<>
<button className={styles.cycleContainer} onClick={toggleDropdown} type="button">
<span className={styles.cycleText({ state })}>{selectedType}</span>
<IcDropdown className={styles.dropdownIcon({ state })} />
</button>
<div className={styles.cycleListContainer}>
{isOpen && <CycleList selectedType={selectedType} onSelect={handleType} />}
</div>
</>
);
};
export default CycleDropDown;