@jk-core/components 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ import { CalendarView } from '../type';
2
+
3
+ interface Props {
4
+ method: CalendarView;
5
+ selectMode: CalendarView;
6
+ date: Date;
7
+ setDate:(date: Date) => void;
8
+ min: Date;
9
+ max: Date;
10
+ }
11
+ const useCalendarNav = ({
12
+ method, selectMode, date, setDate, min, max,
13
+ }:Props) => {
14
+ const disabled = (direction: 'prev' | 'next') => {
15
+ if (selectMode === 'year' || method !== selectMode) return true;
16
+
17
+ if (method === 'day') {
18
+ if (direction === 'prev') {
19
+ const prevMonth = new Date(date.getFullYear(), date.getMonth() - 1, 1);
20
+ return prevMonth < min;
21
+ }
22
+ const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 1);
23
+ return nextMonth > max;
24
+ }
25
+
26
+ if (method === 'month') {
27
+ if (direction === 'prev') {
28
+ const prevYear = new Date(date.getFullYear() - 1, date.getMonth(), 1);
29
+ return prevYear < min;
30
+ }
31
+ const nextYear = new Date(date.getFullYear() + 1, date.getMonth(), 1);
32
+ return nextYear > max;
33
+ }
34
+
35
+ if (method === 'year') {
36
+ if (direction === 'prev') {
37
+ const prevDecade = new Date(date.getFullYear() - 10, date.getMonth(), 1);
38
+ return prevDecade < min;
39
+ }
40
+ const nextDecade = new Date(date.getFullYear() + 10, date.getMonth(), 1);
41
+ return nextDecade > max;
42
+ }
43
+
44
+ return false;
45
+ };
46
+
47
+ const onArrowClick = (direction: 'prev' | 'next') => {
48
+ const offset = direction === 'prev' ? -1 : 1;
49
+ const minDate = new Date(2000, 0, 1);
50
+ const maxDate = new Date(2099, 11, 31);
51
+
52
+ if (method === 'day') {
53
+ const newDate = new Date(date.getFullYear(), date.getMonth() + offset, 1);
54
+
55
+ if (newDate >= minDate && newDate <= maxDate) {
56
+ setDate(newDate);
57
+ }
58
+ }
59
+
60
+ if (method === 'month') {
61
+ const newDate = new Date(date.getFullYear() + offset, date.getMonth(), 1);
62
+
63
+ if (newDate >= minDate && newDate <= maxDate) {
64
+ setDate(newDate);
65
+ }
66
+ }
67
+
68
+ if (method === 'year') {
69
+ const newDate = new Date(date.getFullYear() + offset * 10, date.getMonth(), 1);
70
+
71
+ if (newDate >= minDate && newDate <= maxDate) {
72
+ setDate(newDate);
73
+ }
74
+ }
75
+ };
76
+
77
+ return { disabled, onArrowClick };
78
+ };
79
+
80
+ export default useCalendarNav;
@@ -0,0 +1,47 @@
1
+ import { CalendarView } from '../type';
2
+
3
+ interface UseDateSelectProps {
4
+ viewDate: Date;
5
+ setViewDate: (date: Date) => void;
6
+ method: CalendarView;
7
+ setSelectMode:(mode:CalendarView) => void;
8
+ onChange:(date:Date) => void;
9
+
10
+ }
11
+
12
+ const useDateSelect = ({
13
+ viewDate, setViewDate, onChange, setSelectMode, method,
14
+ }: UseDateSelectProps) => {
15
+ const min = new Date(2000, 0, 1);
16
+ const max = new Date(2099, 11, 31);
17
+
18
+ const onDayClick = (day: Date) => {
19
+ if (day < min || day > max) {
20
+ return;
21
+ }
22
+
23
+ setViewDate(day);
24
+ onChange(day);
25
+ };
26
+
27
+ const onMonthClick = (month:number) => {
28
+ const newDate = new Date(viewDate.getFullYear(), month, 1);
29
+
30
+ setViewDate(newDate);
31
+
32
+ if (method !== 'month') setSelectMode(method);
33
+ if (method === 'month') onChange(newDate);
34
+ };
35
+
36
+ const onYearClick = (year:number) => {
37
+ const newDate = new Date(year, 0, 1);
38
+
39
+ setViewDate(newDate);
40
+ if (method !== 'year') setSelectMode(method);
41
+ if (method === 'year') onChange(newDate);
42
+ };
43
+
44
+ return { onDayClick, onMonthClick, onYearClick };
45
+ };
46
+
47
+ export default useDateSelect;
@@ -0,0 +1,166 @@
1
+ /* eslint-disable react/no-array-index-key */
2
+ import { useState } from 'react';
3
+ import { cn } from '@jk-core/utils';
4
+ import CloseIcon from '../assets/close.svg';
5
+ import DropIcon from '../assets/drop-arrow.svg';
6
+ import styles from './Calendar.module.scss';
7
+ import { CalendarView } from './type';
8
+ import getWeeksInMonth from './utils/getWeeksInMonth';
9
+ import DayTile from './components/DayTile';
10
+ import MonthTile from './components/MonthTile';
11
+ import YearTile from './components/YearTile';
12
+ import useCalendarNav from './hooks/useCalendarNav';
13
+ import useDateSelect from './hooks/useDateSelect';
14
+
15
+ import '../styles/color.scss';
16
+
17
+ interface CalendarProps {
18
+ date?: Date;
19
+ view?: CalendarView;
20
+ tileContent?: (date: Date | undefined, view: CalendarView) => React.ReactNode;
21
+ onChange?:(date:Date)=>void;
22
+ min?: Date;
23
+ max?: Date;
24
+ onClose?:()=>void;
25
+ }
26
+
27
+ export default function Calendar({
28
+ date: selectedDate, view, tileContent, onChange = () => { }, onClose,
29
+ min = new Date(2000, 0, 1), max = new Date(2099, 11, 31),
30
+ }:CalendarProps) {
31
+ const [viewDate, setViewDate] = useState<Date>(selectedDate || new Date());
32
+ const [method, setMethod] = useState<CalendarView>(view || 'day');
33
+ const [selectMode, setSelectMode] = useState<CalendarView>('day');
34
+ const weeksInMonth = getWeeksInMonth(viewDate);
35
+ const { onDayClick, onMonthClick, onYearClick } = useDateSelect({
36
+ viewDate,
37
+ setViewDate: (data) => setViewDate(data),
38
+ onChange,
39
+ setSelectMode,
40
+ method,
41
+ });
42
+ const { disabled, onArrowClick } = useCalendarNav({
43
+ method, selectMode, date: viewDate, setDate: setViewDate, min, max,
44
+ });
45
+
46
+ return (
47
+ <div className={styles.calendar}>
48
+ <div className={styles.calendar__close}>
49
+ {onClose
50
+ && (
51
+ <CloseIcon onClick={onClose} />
52
+ )}
53
+ </div>
54
+ {/* 일/월/년 선택 버튼 */}
55
+ <div className={styles.view}>
56
+ <div className={cn({
57
+ [styles.view__block]: true,
58
+ [styles['view__block--second']]: method === 'month',
59
+ [styles['view__block--last']]: method === 'year',
60
+ })}
61
+ />
62
+ <button
63
+ className={cn({
64
+ [styles.view__selector]: true,
65
+ [styles['view__selector--selected']]: method === 'day',
66
+ })}
67
+ type="button"
68
+ onClick={() => { setMethod('day'); setSelectMode('day'); }}
69
+ >일
70
+ </button>
71
+ <button
72
+ className={cn({
73
+ [styles.view__selector]: true,
74
+ [styles['view__selector--selected']]: method === 'month',
75
+ })}
76
+ type="button"
77
+ onClick={() => { setMethod('month'); setSelectMode('month'); }}
78
+ >월
79
+ </button>
80
+ <button
81
+ className={cn({
82
+ [styles.view__selector]: true,
83
+ [styles['view__selector--selected']]: method === 'year',
84
+ })}
85
+ type="button"
86
+ onClick={() => { setMethod('year'); setSelectMode('year'); }}
87
+ >년
88
+ </button>
89
+ </div>
90
+
91
+ <div className={styles.nav}>
92
+ <button
93
+ className={styles.nav__button}
94
+ type="button"
95
+ onClick={() => onArrowClick('prev')}
96
+ disabled={disabled('prev')}
97
+ >
98
+ ◀︎
99
+ </button>
100
+ <div className={styles.nav__label}>
101
+ {method === 'year' && '연도 선택'}
102
+ {method !== 'year' && (
103
+ <button
104
+ className={cn({
105
+ [styles['nav__label--date']]: true,
106
+ [styles['nav__label--date-selected']]: selectMode === 'year',
107
+ })}
108
+ type="button"
109
+ onClick={() => setSelectMode('year')}
110
+ >
111
+ {`${viewDate.getFullYear()}년`}<DropIcon />
112
+ </button>
113
+ )}
114
+ {method === 'day' && (
115
+ <button
116
+ className={cn({
117
+ [styles['nav__label--date']]: true,
118
+ [styles['nav__label--date-selected']]: selectMode === 'month',
119
+ })}
120
+ type="button"
121
+ onClick={() => setSelectMode('month')}
122
+ >
123
+ {`${viewDate.getMonth() + 1}월`}<DropIcon />
124
+ </button>
125
+ )}
126
+ </div>
127
+ <button
128
+ className={styles.nav__button}
129
+ type="button"
130
+ onClick={() => onArrowClick('next')}
131
+ disabled={disabled('next')}
132
+ >►
133
+ </button>
134
+ </div>
135
+
136
+ {(method === 'day' && selectMode === 'day') && (
137
+ <DayTile
138
+ selectedDate={selectedDate}
139
+ weeksInMonth={weeksInMonth}
140
+ // selectRange={selectRange}
141
+ // selectedRange={selectedRange}
142
+ handleDayClick={onDayClick}
143
+ tileContent={() => (tileContent ? tileContent(selectedDate, method) : null)}
144
+ />
145
+ )}
146
+
147
+ {((method === 'month' || selectMode === 'month') && selectMode !== 'year') && (
148
+ <MonthTile
149
+ selectedDate={selectedDate}
150
+ viewDate={viewDate}
151
+ handleMonthClick={onMonthClick}
152
+ tileContent={tileContent}
153
+
154
+ />
155
+ )}
156
+
157
+ {(method === 'year' || selectMode === 'year') && (
158
+ <YearTile
159
+ selectedDate={selectedDate}
160
+ onClick={onYearClick}
161
+ tileContent={tileContent}
162
+ />
163
+ )}
164
+ </div>
165
+ );
166
+ }
@@ -0,0 +1,6 @@
1
+ export type CalendarView = 'day' | 'month' | 'year';
2
+
3
+ export interface CalendarRange {
4
+ start: Date | null;
5
+ end: Date | null;
6
+ }
@@ -0,0 +1,45 @@
1
+ const getWeeksInMonth = (viewDate:Date) => {
2
+ const startOfMonth = new Date(viewDate.getFullYear(), viewDate.getMonth(), 1);
3
+ const endOfMonth = new Date(viewDate.getFullYear(), viewDate.getMonth() + 1, 0);
4
+ const weeks = [];
5
+ let currentWeek = [];
6
+
7
+ const startDayOfWeek = startOfMonth.getDay();
8
+ if (startDayOfWeek !== 0) {
9
+ const prevMonthEnd = new Date(startOfMonth);
10
+ prevMonthEnd.setDate(0);
11
+ for (let i = startDayOfWeek - 1; i >= 0; i -= 1) {
12
+ const prevDate = new Date(prevMonthEnd);
13
+ prevDate.setDate(prevMonthEnd.getDate() - i);
14
+ currentWeek.push({ thisMonth: false, date: prevDate });
15
+ }
16
+ }
17
+
18
+ const currentDate = new Date(startOfMonth);
19
+
20
+ while (currentDate <= endOfMonth) {
21
+ currentWeek.push({ thisMonth: true, date: new Date(currentDate) });
22
+ if (currentDate.getDay() === 6) {
23
+ weeks.push(currentWeek);
24
+ currentWeek = [];
25
+ }
26
+ currentDate.setDate(currentDate.getDate() + 1);
27
+ }
28
+
29
+ const endDayOfWeek = endOfMonth.getDay();
30
+ if (endDayOfWeek !== 6) {
31
+ for (let i = 1; i <= 6 - endDayOfWeek; i += 1) {
32
+ const nextDate = new Date(endOfMonth);
33
+ nextDate.setDate(endOfMonth.getDate() + i);
34
+ currentWeek.push({ thisMonth: false, date: nextDate });
35
+ }
36
+ }
37
+
38
+ if (currentWeek.length > 0) {
39
+ weeks.push(currentWeek);
40
+ }
41
+
42
+ return weeks;
43
+ };
44
+
45
+ export default getWeeksInMonth;
@@ -0,0 +1,8 @@
1
+ import { CalendarRange } from '../type';
2
+
3
+ const isInRange = (day: Date, range:CalendarRange) => {
4
+ if (!range.start || !range.end) return false;
5
+ return day > range.start && day < range.end;
6
+ };
7
+
8
+ export default isInRange;
@@ -0,0 +1,21 @@
1
+ import { CalendarView } from '../type';
2
+
3
+ const isSameDay = (date1: Date | null, date2: Date | null, view: CalendarView = 'day'): boolean => {
4
+ if (date1 === null || date2 === null) return false;
5
+
6
+ switch (view) {
7
+ case 'day':
8
+ return date1.getFullYear() === date2.getFullYear()
9
+ && date1.getMonth() === date2.getMonth()
10
+ && date1.getDate() === date2.getDate();
11
+ case 'month':
12
+ return date1.getFullYear() === date2.getFullYear()
13
+ && date1.getMonth() === date2.getMonth();
14
+ case 'year':
15
+ return date1.getFullYear() === date2.getFullYear();
16
+ default:
17
+ return false;
18
+ }
19
+ };
20
+
21
+ export default isSameDay;
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
2
+
3
+ <!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
4
+ <svg width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="#000000" fill="#000000" stroke-width="0.696">
5
+
6
+ <g id="SVGRepo_bgCarrier" stroke-width="0"/>
7
+
8
+ <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>
9
+
10
+ <g id="SVGRepo_iconCarrier">
11
+
12
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M19.207 6.207a1 1 0 0 0-1.414-1.414L12 10.586 6.207 4.793a1 1 0 0 0-1.414 1.414L10.586 12l-5.793 5.793a1 1 0 1 0 1.414 1.414L12 13.414l5.793 5.793a1 1 0 0 0 1.414-1.414L13.414 12l5.793-5.793z" />
13
+
14
+ </g>
15
+
16
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="25" height="25" viewBox="0 0 20 20" fill="none" stroke="#2D2D2D" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M5 7.5L10 13L15 7.5" stroke-width="1.4" stroke-linecap="round"/>
3
+ </svg>
package/src/index.tsx ADDED
@@ -0,0 +1,3 @@
1
+ import Calendar from './Calendar';
2
+
3
+ export { Calendar };
@@ -0,0 +1,90 @@
1
+ :root {
2
+ --white: #ffffff;
3
+ --black: #000000;
4
+ --P-5: #eff5ff;
5
+ --P-10: #d3e1fb;
6
+ --P-20: #a7c4f7;
7
+ --P-30: #7ca6f3;
8
+ --P-40: #5089ef;
9
+ --P-50: #246beb;
10
+ --P-60: #1d56bc;
11
+ --P-70: #16408d;
12
+ --P-90: #07152f;
13
+ --P-100: #000000;
14
+ --S-5: #edf1f5;
15
+ --S-10: #cdd7e4;
16
+ --S-20: #b4c4d6;
17
+ --S-30: #99b0cb;
18
+ --S-40: #2a5c96;
19
+ --S-50: #003675;
20
+ --S-60: #002b5e;
21
+ --S-70: #002036;
22
+ --S-80: #00162f;
23
+ --S-90: #000b17;
24
+ --G-5: #f8f8f8;
25
+ --G-10: #f0f0f0;
26
+ --G-20: #e4e4e4;
27
+ --G-30: #d8d8d8;
28
+ --G-40: #c6c6c6;
29
+ --G-50: #8e8e8e;
30
+ --G-60: #717171;
31
+ --G-70: #555555;
32
+ --G-80: #2d2d2d;
33
+ --G-90: #1d1d1d;
34
+ --Point-5: #fdf2f3;
35
+ --Point-10: #f8d6d8;
36
+ --Point-20: #f5a3a8;
37
+ --Point-30: #f1747c;
38
+ --Point-40: #ec4651;
39
+ --Point-50: #e71825;
40
+ --Point-60: #b9131e;
41
+ --Point-70: #8b0e16;
42
+ --Point-80: #5c0a0f;
43
+ --Point-90: #2e0507;
44
+ --Warning-5: #fff8e9;
45
+ --Warning-10: #ffeac1;
46
+ --Warning-20: #ffe2a7;
47
+ --Warning-30: #ffd47c;
48
+ --Warning-40: #ffc550;
49
+ --Warning-50: #ffb724;
50
+ --Warning-60: #98690a;
51
+ --Warning-70: #66490e;
52
+ --Warning-80: #4d370b;
53
+ --Warning-90: #332507;
54
+ --Success-5: #eef7f0;
55
+ --Success-10: #cee9d4;
56
+ --Success-20: #b2dcbb;
57
+ --Success-30: #8cca99;
58
+ --Success-40: #33a14b;
59
+ --Success-50: #008a1e;
60
+ --Success-60: #006e18;
61
+ --Success-70: #005312;
62
+ --Success-80: #00370c;
63
+ --Success-90: #002207;
64
+ --Info-5: #e9f0ff;
65
+ --Info-10: #d4e1ff;
66
+ --Info-20: #a9c3ff;
67
+ --Info-30: #7da4ff;
68
+ --Info-40: #5286ff;
69
+ --Info-50: #2768ff;
70
+ --Info-60: #1f53cc;
71
+ --Info-70: #173e99;
72
+ --Info-80: #0c1f4d;
73
+ --Info-90: #040a1a;
74
+ --Red: #e40000;
75
+ --Red2: #ffe4e4;
76
+ --Green: #2fb400;
77
+ --Green-2: #d7ffe0;
78
+ --Orange: #ff8800;
79
+ --Orange-5: #ffead1;
80
+ --Orange-10: #ffdacc;
81
+ --Orange-30: #ff8f66;
82
+ --Orange-40: #ff6a33;
83
+ --Orange-50: #ff4500;
84
+ --Orange-60: #d53209;
85
+ --Orange-70: #992900;
86
+ --Orange-80: #661c00;
87
+ --Orange-90: #330e00;
88
+ --Modal-Shadow: #0000005a;
89
+ --Modal-Background: #6666663a;
90
+ }
@@ -0,0 +1,22 @@
1
+ // 스크롤바 너비 14px 추가
2
+ $pc: 1396px;
3
+ $tablet: 1395px;
4
+ $mobile: 774px;
5
+
6
+ @mixin pc {
7
+ @media (min-width: $pc) {
8
+ @content;
9
+ }
10
+ }
11
+
12
+ @mixin tablet {
13
+ @media (max-width: $tablet) {
14
+ @content;
15
+ }
16
+ }
17
+
18
+ @mixin mobile {
19
+ @media (max-width: $mobile) {
20
+ @content;
21
+ }
22
+ }
package/src/svg.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ // svg를 ReactComponent처럼 사용하기 위해 선언
2
+ declare module '*.svg' {
3
+ import { HTMLAttributes } from 'react';
4
+
5
+ export default React.Component<HTMLAttributes<HTMLDivElement>>;
6
+ }
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "baseUrl": "./src",
6
+ "outDir": "dist",
7
+ "rootDir": "./src",
8
+ "jsx": "react-jsx",
9
+ "emitDeclarationOnly": true,
10
+ "types": ["node", "vite/client","vite-plugin-svgr/client"],
11
+ },
12
+ "include": ["./src", "./src/svg.d.ts"]
13
+ }