Skip to content

Commit f0c755d

Browse files
committed
Add conditional looking
1 parent 90199d8 commit f0c755d

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

pages/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Datepicker from "../src";
22
import { useState } from "react";
3-
import { COLORS } from "../src/constants";
3+
import { COLORS, DATE_LOOKING_OPTIONS } from "../src/constants";
44
import dayjs from "dayjs";
55
import Head from "next/head";
66

@@ -25,6 +25,7 @@ export default function Playground() {
2525
const [readOnly, setReadOnly] = useState(false);
2626
const [minDate, setMinDate] = useState("");
2727
const [maxDate, setMaxDate] = useState("");
28+
const [dateLooking, setDateLooking] = useState(true);
2829
const [disabledDates, setDisabledDates] = useState([]);
2930
const [newDisabledDates, setNewDisabledDates] = useState({ startDate: "", endDate: "" });
3031
const [startFrom, setStartFrom] = useState("2023-03-01");
@@ -104,6 +105,7 @@ export default function Playground() {
104105
readOnly={readOnly}
105106
minDate={minDate}
106107
maxDate={maxDate}
108+
dateLooking={dateLooking}
107109
disabledDates={disabledDates}
108110
startWeekOn={startWeekOn}
109111
toggleIcon={isEmpty => {
@@ -298,6 +300,25 @@ export default function Playground() {
298300
}}
299301
/>
300302
</div>
303+
<div className="mb-2">
304+
<label className="block" htmlFor="dateLooking">
305+
Date Looking
306+
</label>
307+
<select
308+
className="rounded block w-full border-gray-200 border px-4 py-2"
309+
id="dateLooking"
310+
value={dateLooking}
311+
onChange={e => {
312+
setDateLooking(e.target.value);
313+
}}
314+
>
315+
{DATE_LOOKING_OPTIONS.map((option, i) => (
316+
<option key={i} value={option}>
317+
{option}
318+
</option>
319+
))}
320+
</select>
321+
</div>
301322
</div>
302323
<div className="w-full sm:w-1/3 pr-2 flex flex-col">
303324
<div className="mb-2">

src/components/Calendar/Years.tsx

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

33
import { generateArrayNumber } from "../../helpers";
44
import { RoundedButton } from "../utils";
55

6+
import DatepickerContext from "contexts/DatepickerContext";
7+
68
interface Props {
79
year: number;
810
minYear: number | null;
@@ -11,9 +13,30 @@ interface Props {
1113
}
1214

1315
const Years: React.FC<Props> = ({ year, minYear, maxYear, clickYear }) => {
16+
const { dateLooking } = useContext(DatepickerContext);
17+
18+
let startDate = 0;
19+
let endDate = 0;
20+
21+
switch (dateLooking) {
22+
case "backward":
23+
startDate = year - 11;
24+
endDate = year;
25+
break;
26+
case "middle":
27+
startDate = year - 4;
28+
endDate = year + 7;
29+
break;
30+
case "forward":
31+
default:
32+
startDate = year;
33+
endDate = year + 11;
34+
break;
35+
}
36+
1437
return (
1538
<div className="w-full grid grid-cols-2 gap-2 mt-2">
16-
{generateArrayNumber(year, year + 11).map((item, index) => (
39+
{generateArrayNumber(startDate, endDate).map((item, index) => (
1740
<RoundedButton
1841
key={index}
1942
padding="py-3"

src/components/Datepicker.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const Datepicker: React.FC<DatepickerType> = ({
3535
readOnly = false,
3636
minDate = null,
3737
maxDate = null,
38+
dateLooking = "forward",
3839
disabledDates = null,
3940
inputId,
4041
inputName,
@@ -261,6 +262,7 @@ const Datepicker: React.FC<DatepickerType> = ({
261262
displayFormat,
262263
minDate,
263264
maxDate,
265+
dateLooking,
264266
disabledDates,
265267
inputId,
266268
inputName,
@@ -293,6 +295,7 @@ const Datepicker: React.FC<DatepickerType> = ({
293295
displayFormat,
294296
minDate,
295297
maxDate,
298+
dateLooking,
296299
disabledDates,
297300
inputId,
298301
inputName,

src/constants/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const DATE_FORMAT = "YYYY-MM-DD";
2929

3030
export const START_WEEK = "sun";
3131

32+
export const DATE_LOOKING_OPTIONS = ["forward", "backward", "middle"];
33+
3234
export const DAYS = [0, 1, 2, 3, 4, 5, 6];
3335

3436
export const MONTHS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

src/contexts/DatepickerContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface DatepickerStore {
4444
displayFormat: string;
4545
minDate?: DateType | null;
4646
maxDate?: DateType | null;
47+
dateLooking?: "forward" | "backward" | "middle";
4748
disabledDates?: DateRangeType[] | null;
4849
inputId?: string;
4950
inputName?: string;
@@ -83,6 +84,7 @@ const DatepickerContext = createContext<DatepickerStore>({
8384
displayFormat: DATE_FORMAT,
8485
minDate: null,
8586
maxDate: null,
87+
dateLooking: "forward",
8688
disabledDates: null,
8789
inputId: undefined,
8890
inputName: undefined,

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export interface DatepickerType {
7575
readOnly?: boolean;
7676
minDate?: Date | null;
7777
maxDate?: Date | null;
78+
dateLooking?: "forward" | "backward" | "middle";
7879
disabledDates?: DateRangeType[] | null;
7980
startWeekOn?: string | null;
8081
popoverDirection?: PopoverDirectionType;

0 commit comments

Comments
 (0)