Skip to content

Commit 448e290

Browse files
committed
Missing understand the triggers to change the global value.
1 parent 0d78a2c commit 448e290

File tree

9 files changed

+78
-41
lines changed

9 files changed

+78
-41
lines changed

src/components/Calendar/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const Calendar: React.FC<Props> = ({
5050
}) => {
5151
// Contexts
5252
const {
53+
hour,
54+
minute,
55+
periodDay,
5356
period,
5457
changePeriod,
5558
changeDayHover,

src/components/Datepicker.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { COLORS, DATE_FORMAT, DEFAULT_COLOR, LANGUAGE } from "../constants";
1010
import DatepickerContext from "../contexts/DatepickerContext";
1111
import { formatDate, nextMonth, previousMonth } from "../helpers";
1212
import useOnClickOutside from "../hooks";
13-
import { Period, DatepickerType, ColorKeys } from "../types";
13+
import { Period, DatepickerType, ColorKeys, PeriodDay } from "../types";
1414

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

@@ -65,7 +65,7 @@ const Datepicker: React.FC<DatepickerType> = ({
6565

6666
const [hour, setHour] = useState<string>("1");
6767
const [minute, setMinute] = useState<string>("00");
68-
const [periodDay, setPeriodDay] = useState<"AM" | "PM">("PM");
68+
const [periodDay, setPeriodDay] = useState<PeriodDay>("PM");
6969

7070
// Custom Hooks use
7171
useOnClickOutside(containerRef, () => {
@@ -108,7 +108,7 @@ const Datepicker: React.FC<DatepickerType> = ({
108108
setMinute(minute);
109109
}, []);
110110

111-
const changePeriodDay = useCallback((periodDay: "AM" | "PM") => {
111+
const changePeriodDay = useCallback((periodDay: PeriodDay) => {
112112
setPeriodDay(periodDay);
113113
}, []);
114114

@@ -282,6 +282,9 @@ const Datepicker: React.FC<DatepickerType> = ({
282282
changeInputText: (newText: string) => setInputText(newText),
283283
updateFirstDate: (newDate: dayjs.Dayjs) => firstGotoDate(newDate),
284284
changeDatepickerValue: onChange,
285+
hour,
286+
minute,
287+
periodDay,
285288
changeHour,
286289
changeMinute,
287290
changePeriodDay,
@@ -319,6 +322,9 @@ const Datepicker: React.FC<DatepickerType> = ({
319322
dayHover,
320323
inputText,
321324
onChange,
325+
hour,
326+
minute,
327+
periodDay,
322328
changeHour,
323329
changeMinute,
324330
changePeriodDay,

src/components/Input.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ const Input: React.FC<Props> = (e: Props) => {
120120

121121
changeInputText(e.target.value);
122122
},
123-
[asSingle, displayFormat, separator, changeDatepickerValue, changeDayHover, changeInputText]
123+
[
124+
asSingle,
125+
asTimePicker,
126+
changeInputText,
127+
displayFormat,
128+
separator,
129+
changeDatepickerValue,
130+
changeDayHover
131+
]
124132
);
125133

126134
const handleInputKeyDown = useCallback(

src/components/Time.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import dayjs from "dayjs";
21
import React, { ChangeEvent, useCallback, useContext } from "react";
32

4-
import { BG_COLOR, BORDER_COLOR, DATE_FORMAT, RING_COLOR } from "../constants";
3+
import { RING_COLOR } from "../constants";
54
import DatepickerContext from "../contexts/DatepickerContext";
65
import { classNames as cn } from "../helpers";
76

7+
import { PeriodDay } from "types";
8+
89
const Time: React.FC = () => {
910
// Contexts
1011

@@ -27,7 +28,7 @@ const Time: React.FC = () => {
2728
const handleChangeMinute = (e: ChangeEvent<HTMLSelectElement>) => changeMinute(e.target.value);
2829

2930
const handleChangePeriodDay = (e: ChangeEvent<HTMLSelectElement>) =>
30-
changePeriodDay(e.target.value as "AM" | "PM");
31+
changePeriodDay(e.target.value as PeriodDay);
3132

3233
return (
3334
<div className="w-full md:w-auto flex items-center justify-center space-x-3">

src/constants/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const LANGUAGE = "en";
2727

2828
export const DATE_FORMAT = "YYYY-MM-DD";
2929

30+
export const DATE_TIME_FORMAT = "DD/MM/YYYY hh:mm A";
31+
3032
export const START_WEEK = "sun";
3133

3234
export const DATE_LOOKING_OPTIONS = ["forward", "backward", "middle"];

src/contexts/DatepickerContext.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
DateRangeType,
1111
ClassNamesTypeProp,
1212
PopoverDirectionType,
13-
ColorKeys
13+
ColorKeys,
14+
PeriodDay
1415
} from "../types";
1516

1617
interface DatepickerStore {
@@ -23,9 +24,12 @@ interface DatepickerStore {
2324
arrowContainer: React.RefObject<HTMLDivElement> | null;
2425
hideDatepicker: () => void;
2526
period: Period;
27+
hour: string;
28+
minute: string;
29+
periodDay: PeriodDay;
2630
changeHour: (hour: string) => void;
2731
changeMinute: (minute: string) => void;
28-
changePeriodDay: (periodDay: "AM" | "PM") => void;
32+
changePeriodDay: (periodDay: PeriodDay) => void;
2933
changePeriod: (period: Period) => void;
3034
dayHover: string | null;
3135
changeDayHover: (day: string | null) => void;
@@ -73,6 +77,9 @@ const DatepickerContext = createContext<DatepickerStore>({
7377
inputText: "",
7478
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars
7579
changeInputText: text => {},
80+
hour: "",
81+
minute: "",
82+
periodDay: "PM",
7683
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars
7784
changeHour: text => {},
7885
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars

src/helpers/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,24 +628,24 @@ export function dateIsValid(date: Date | number) {
628628
}
629629

630630
export function formatDateTimeToISO(
631-
dateIncoming: Date,
632-
hourIncoming: number,
633-
minute: number,
631+
dateIncoming: Date | string,
632+
hourIncoming: string,
633+
minute: string,
634634
period: string
635635
): string {
636636
// Adjust hour based on period (AM/PM)
637637
const hour = (() => {
638-
if (period === "PM" && hourIncoming !== 12) return hourIncoming + 12;
638+
if (period === "PM" && hourIncoming !== String(12)) return hourIncoming + 12;
639639

640-
if (period === "AM" && hourIncoming === 12) return 0;
640+
if (period === "AM" && hourIncoming === String(12)) return 0;
641641

642642
return hourIncoming;
643643
})();
644644

645645
// Create a new Date object and set the components
646646
const date = new Date(dateIncoming);
647-
date.setHours(hour);
648-
date.setMinutes(minute);
647+
date.setHours(Number(hour));
648+
date.setMinutes(Number(minute));
649649

650650
// Format date to ISO 8601
651651
const isoString = date.toISOString();

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from "react";
22

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

5+
export type PeriodDay = "AM" | "PM";
6+
57
export interface Period {
68
start: string | null;
79
end: string | null;

tsconfig.json

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
{
2-
"compilerOptions": {
3-
"target": "esnext",
4-
"lib": ["dom", "esnext"],
5-
"module": "esnext",
6-
"jsx": "preserve",
7-
"moduleResolution": "node",
8-
"forceConsistentCasingInFileNames": true,
9-
"strict": true,
10-
"noImplicitReturns": true,
11-
"allowSyntheticDefaultImports": true,
12-
"esModuleInterop": true,
13-
"baseUrl": "src/",
14-
"declaration": true,
15-
"outDir": "./dist",
16-
"inlineSources": true,
17-
"sourceMap": true,
18-
"rootDir": "src",
19-
"allowJs": true,
20-
"skipLibCheck": true,
21-
"noEmit": true,
22-
"resolveJsonModule": true,
23-
"isolatedModules": true
24-
},
25-
"include": ["src/**/*"],
26-
"exclude": ["node_modules"]
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"lib": [
5+
"dom",
6+
"esnext"
7+
],
8+
"module": "esnext",
9+
"jsx": "preserve",
10+
"moduleResolution": "node",
11+
"forceConsistentCasingInFileNames": true,
12+
"strict": true,
13+
"noImplicitReturns": true,
14+
"allowSyntheticDefaultImports": true,
15+
"esModuleInterop": true,
16+
"baseUrl": "src/",
17+
"declaration": true,
18+
"outDir": "./dist",
19+
"inlineSources": true,
20+
"sourceMap": true,
21+
"rootDir": "src",
22+
"allowJs": true,
23+
"skipLibCheck": true,
24+
"noEmit": true,
25+
"resolveJsonModule": true,
26+
"isolatedModules": true,
27+
"incremental": true
28+
},
29+
"include": [
30+
"src/**/*"
31+
],
32+
"exclude": [
33+
"node_modules"
34+
]
2735
}

0 commit comments

Comments
 (0)