Skip to content

Commit 3e53a95

Browse files
committed
Code refactoring
1 parent 0d55e86 commit 3e53a95

File tree

10 files changed

+70
-67
lines changed

10 files changed

+70
-67
lines changed

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"plugin:react/recommended",
1111
"plugin:@typescript-eslint/recommended",
1212
"plugin:prettier/recommended",
13-
"plugin:react-hooks/recommended"
13+
"plugin:react-hooks/recommended",
14+
"next/core-web-vitals"
1415
],
1516
"overrides": [],
1617
"parser": "@typescript-eslint/parser",
@@ -26,7 +27,7 @@
2627
"version": "detect"
2728
}
2829
},
29-
"plugins": ["react", "@typescript-eslint", "import", "prettier"],
30+
"plugins": ["react", "@typescript-eslint", "import", "prettier", "@next/eslint-plugin-next"],
3031
"rules": {
3132
"indent": "off",
3233
"linebreak-style": ["error", "unix"],

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@
7373
},
7474
"lint-staged": {
7575
"*.{ts,tsx}": [
76-
"npm run lint"
76+
"eslint",
77+
"prettier --write"
7778
],
78-
"*.{ts,tsx,css,scss,md}": [
79-
"npm run pret:fix"
79+
"*.{css,scss,json,md}": [
80+
"prettier --write"
8081
]
8182
}
8283
}

src/components/Calendar/Days.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ interface Props {
2323
onClickNextDays: (day: number) => void;
2424
}
2525

26-
const Days: React.FC<Props> = ({
27-
calendarData,
28-
onClickPreviousDays,
29-
onClickDay,
30-
onClickNextDays
31-
}) => {
26+
const Days: React.FC<Props> = props => {
27+
// Props
28+
const { calendarData, onClickPreviousDays, onClickDay, onClickNextDays } = props;
29+
3230
// Contexts
3331
const {
3432
primaryColor,

src/components/Calendar/Years.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import DatepickerContext from "contexts/DatepickerContext";
21
import React, { useContext } from "react";
32

43
import { generateArrayNumber } from "../../helpers";
54
import { RoundedButton } from "../utils";
65

6+
import DatepickerContext from "contexts/DatepickerContext";
7+
78
interface Props {
89
year: number;
910
currentYear: number;

src/components/Calendar/index.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import dayjs from "dayjs";
22
import React, { useCallback, useContext, useEffect, useMemo, useState } from "react";
3-
import { DateType } from "types";
43

54
import { CALENDAR_SIZE, DATE_FORMAT } from "../../constants";
65
import DatepickerContext from "../../contexts/DatepickerContext";
@@ -28,6 +27,8 @@ import Months from "./Months";
2827
import Week from "./Week";
2928
import Years from "./Years";
3029

30+
import { DateType } from "types";
31+
3132
interface Props {
3233
date: dayjs.Dayjs;
3334
minDate?: DateType | null;
@@ -38,15 +39,10 @@ interface Props {
3839
changeYear: (year: number) => void;
3940
}
4041

41-
const Calendar: React.FC<Props> = ({
42-
date,
43-
minDate,
44-
maxDate,
45-
onClickPrevious,
46-
onClickNext,
47-
changeMonth,
48-
changeYear
49-
}) => {
42+
const Calendar: React.FC<Props> = props => {
43+
// Props
44+
const { date, minDate, maxDate, onClickPrevious, onClickNext, changeMonth, changeYear } = props;
45+
5046
// Contexts
5147
const {
5248
period,
@@ -66,6 +62,7 @@ const Calendar: React.FC<Props> = ({
6662
const [showMonths, setShowMonths] = useState(false);
6763
const [showYears, setShowYears] = useState(false);
6864
const [year, setYear] = useState(date.year());
65+
6966
// Functions
7067
const previous = useCallback(() => {
7168
return getLastDaysInMonth(

src/components/Datepicker.tsx

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,47 @@ import { Period, DatepickerType, ColorKeys } from "../types";
1313

1414
import { Arrow, VerticalDash } from "./utils";
1515

16-
const Datepicker: React.FC<DatepickerType> = ({
17-
primaryColor = "blue",
18-
value = null,
19-
onChange,
20-
useRange = true,
21-
showFooter = false,
22-
showShortcuts = false,
23-
configs = undefined,
24-
asSingle = false,
25-
placeholder = null,
26-
separator = "~",
27-
startFrom = null,
28-
i18n = LANGUAGE,
29-
disabled = false,
30-
inputClassName = null,
31-
containerClassName = null,
32-
popupClassName = null,
33-
toggleClassName = null,
34-
toggleIcon = undefined,
35-
displayFormat = DATE_FORMAT,
36-
readOnly = false,
37-
minDate = null,
38-
maxDate = null,
39-
dateLooking = "forward",
40-
disabledDates = null,
41-
inputId,
42-
inputName,
43-
startWeekOn = "sun",
44-
classNames = undefined,
45-
popoverDirection = undefined,
46-
required = false
47-
}) => {
48-
// Ref
16+
const Datepicker = (props: DatepickerType) => {
17+
// Props
18+
const {
19+
primaryColor = "blue",
20+
value = null,
21+
onChange,
22+
useRange = true,
23+
showFooter = false,
24+
showShortcuts = false,
25+
configs = undefined,
26+
asSingle = false,
27+
placeholder = null,
28+
separator = "~",
29+
startFrom = null,
30+
i18n = LANGUAGE,
31+
disabled = false,
32+
inputClassName = null,
33+
containerClassName = null,
34+
toggleClassName = null,
35+
popupClassName = null,
36+
toggleIcon = undefined,
37+
displayFormat = DATE_FORMAT,
38+
readOnly = false,
39+
minDate = null,
40+
maxDate = null,
41+
dateLooking = "forward",
42+
disabledDates = null,
43+
inputId,
44+
inputName,
45+
startWeekOn = "sun",
46+
classNames = undefined,
47+
popoverDirection = undefined,
48+
required = false
49+
} = props;
50+
51+
// Refs
4952
const containerRef = useRef<HTMLDivElement | null>(null);
5053
const calendarContainerRef = useRef<HTMLDivElement | null>(null);
5154
const arrowRef = useRef<HTMLDivElement | null>(null);
5255

53-
// State
56+
// States
5457
const [firstDate, setFirstDate] = useState<dayjs.Dayjs>(
5558
startFrom && dayjs(startFrom).isValid() ? dayjs(startFrom) : dayjs()
5659
);
@@ -246,6 +249,7 @@ const Datepicker: React.FC<DatepickerType> = ({
246249
}
247250
return DEFAULT_COLOR;
248251
}, [primaryColor]);
252+
249253
const contextValues = useMemo(() => {
250254
return {
251255
asSingle,
@@ -318,8 +322,8 @@ const Datepicker: React.FC<DatepickerType> = ({
318322
classNames,
319323
inputRef,
320324
popoverDirection,
321-
firstGotoDate,
322-
required
325+
required,
326+
firstGotoDate
323327
]);
324328

325329
const containerClassNameOverload = useMemo(() => {

src/components/Shortcuts.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import dayjs from "dayjs";
2-
import React, { useCallback, useContext, useMemo } from "react";
2+
import React, { ReactNode, useCallback, useContext, useMemo } from "react";
33

44
import { DATE_FORMAT, TEXT_COLOR } from "../constants";
55
import DEFAULT_SHORTCUTS from "../constants/shortcuts";
66
import DatepickerContext from "../contexts/DatepickerContext";
77
import { Period, ShortcutsItem } from "../types";
88

99
interface ItemTemplateProps {
10-
children: JSX.Element;
10+
children: ReactNode;
1111
key: number;
1212
item: ShortcutsItem | ShortcutsItem[];
1313
}
@@ -26,7 +26,7 @@ const ItemTemplate = React.memo((props: ItemTemplateProps) => {
2626
} = useContext(DatepickerContext);
2727

2828
// Functions
29-
const getClassName: () => string = useCallback(() => {
29+
const getClassName = useCallback(() => {
3030
const textColor = TEXT_COLOR["600"][primaryColor as keyof (typeof TEXT_COLOR)["600"]];
3131
const textColorHover = TEXT_COLOR.hover[primaryColor as keyof typeof TEXT_COLOR.hover];
3232
return `whitespace-nowrap w-1/2 md:w-1/3 lg:w-auto transition-all duration-300 hover:bg-gray-100 dark:hover:bg-white/10 p-2 rounded cursor-pointer ${textColor} ${textColorHover}`;
@@ -134,7 +134,7 @@ const Shortcuts: React.FC = () => {
134134
return shortcutOptions?.length ? (
135135
<div className="md:border-b mb-3 lg:mb-0 lg:border-r lg:border-b-0 border-gray-300 dark:border-gray-700 pr-1">
136136
<ul className="w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0">
137-
{shortcutOptions.map(([key, item], index: number) =>
137+
{shortcutOptions.map(([key, item], index) =>
138138
Array.isArray(item) ? (
139139
item.map((item, index) => (
140140
<ItemTemplate key={index} item={item}>

src/components/utils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useContext } from "react";
1+
import React, { ReactNode, useCallback, useContext } from "react";
22

33
import { BG_COLOR, BORDER_COLOR, BUTTON_COLOR, RING_COLOR } from "../constants";
44
import DatepickerContext from "../contexts/DatepickerContext";
@@ -8,7 +8,7 @@ interface IconProps {
88
}
99

1010
interface Button {
11-
children: JSX.Element | JSX.Element[];
11+
children: ReactNode;
1212
onClick: React.MouseEventHandler<HTMLButtonElement>;
1313
disabled?: boolean;
1414
roundedFull?: boolean;

src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import { ReactNode } from "react";
22

33
import { COLORS } from "../constants";
44

@@ -72,7 +72,7 @@ export interface DatepickerType {
7272
popupClassName?: ((className: string) => string) | string | null;
7373
inputClassName?: ((className: string) => string) | string | null;
7474
toggleClassName?: ((className: string) => string) | string | null;
75-
toggleIcon?: (open: boolean) => React.ReactNode;
75+
toggleIcon?: (open: boolean) => ReactNode;
7676
inputId?: string;
7777
inputName?: string;
7878
displayFormat?: string;

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"skipLibCheck": true,
2121
"noEmit": true,
2222
"resolveJsonModule": true,
23-
"isolatedModules": true
23+
"isolatedModules": true,
24+
"incremental": true
2425
},
2526
"include": ["src/**/*"],
2627
"exclude": ["node_modules"]

0 commit comments

Comments
 (0)