Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for displaying (only) the year picker #89

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/components/Calendar/Years.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import React from "react";
import React, { useContext } from "react";

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

import DatepickerContext from "contexts/DatepickerContext";

interface Props {
year: number;
clickYear: (data: number) => void;
}

const Years: React.FC<Props> = ({ year, clickYear }) => {
const { yearPickerStartYearOffset } = useContext(DatepickerContext);

return (
<div className="w-full grid grid-cols-2 gap-2 mt-2">
{generateArrayNumber(year, year + 11).map((item, index) => (
{generateArrayNumber(
year + (yearPickerStartYearOffset || 0),
year + (yearPickerStartYearOffset || 0) + 11
).map((item, index) => (
<RoundedButton
key={index}
padding="py-3"
Expand Down
65 changes: 39 additions & 26 deletions src/components/Calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ interface Props {
onClickNext: () => void;
changeMonth: (month: number) => void;
changeYear: (year: number) => void;
showYearPicker?: boolean;
}

const Calendar: React.FC<Props> = ({
date,
onClickPrevious,
onClickNext,
changeMonth,
changeYear
changeYear,
showYearPicker
}) => {
// Contexts
const {
Expand All @@ -59,7 +61,7 @@ const Calendar: React.FC<Props> = ({

// States
const [showMonths, setShowMonths] = useState(false);
const [showYears, setShowYears] = useState(false);
const [showYears, setShowYears] = useState(showYearPicker);
const [year, setYear] = useState(date.year());
// Functions
const previous = useCallback(() => {
Expand Down Expand Up @@ -88,29 +90,8 @@ const Calendar: React.FC<Props> = ({
showYears && setShowYears(false);
}, [showYears]);

const clickMonth = useCallback(
(month: number) => {
setTimeout(() => {
changeMonth(month);
setShowMonths(!showMonths);
}, 250);
},
[changeMonth, showMonths]
);

const clickYear = useCallback(
(year: number) => {
setTimeout(() => {
changeYear(year);
setShowYears(!showYears);
}, 250);
},
[changeYear, showYears]
);

const clickDay = useCallback(
(day: number, month = date.month() + 1, year = date.year()) => {
const fullDay = `${year}-${month}-${day}`;
const selectDateHelper = useCallback(
(fullDay: string) => {
let newStart;
let newEnd = null;

Expand Down Expand Up @@ -183,7 +164,6 @@ const Calendar: React.FC<Props> = ({
changeDatepickerValue,
changeDayHover,
changePeriod,
date,
hideDatepicker,
period.end,
period.start,
Expand All @@ -192,6 +172,39 @@ const Calendar: React.FC<Props> = ({
]
);

const clickMonth = useCallback(
(month: number) => {
setTimeout(() => {
changeMonth(month);
setShowMonths(!showMonths);
}, 250);
},
[changeMonth, showMonths]
);

const clickYear = useCallback(
(year: number) => {
setTimeout(() => {
setShowYears(showYearPicker ? true : !showYears);
if (showYearPicker) {
const fullDay = `${year}-${1}-${1}`;
selectDateHelper(fullDay);
} else {
changeYear(year);
}
}, 250);
},
[changeYear, selectDateHelper, showYearPicker, showYears]
);

const clickDay = useCallback(
(day: number, month = date.month() + 1, year = date.year()) => {
const fullDay = `${year}-${month}-${day}`;
selectDateHelper(fullDay);
},
[date, selectDateHelper]
);

const clickPreviousDays = useCallback(
(day: number) => {
const newDate = previousMonth(date);
Expand Down
14 changes: 11 additions & 3 deletions src/components/Datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Props {
useRange?: boolean;
showFooter?: boolean;
showShortcuts?: boolean;
showYearPicker?: boolean;
configs?: {
shortcuts?: {
today?: string;
Expand Down Expand Up @@ -59,6 +60,7 @@ interface Props {
maxDate?: DateType | null;
disabledDates?: DateRangeType[] | null;
startWeekOn?: string | null;
yearPickerStartYearOffset?: number;
}

const Datepicker: React.FC<Props> = ({
Expand Down Expand Up @@ -87,7 +89,9 @@ const Datepicker: React.FC<Props> = ({
inputId,
inputName,
startWeekOn = "sun",
classNames = undefined
classNames = undefined,
showYearPicker = false,
yearPickerStartYearOffset = undefined
}) => {
// Ref
const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -314,7 +318,8 @@ const Datepicker: React.FC<Props> = ({
startWeekOn,
classNames,
onChange,
input: inputRef
input: inputRef,
yearPickerStartYearOffset
};
}, [
asSingle,
Expand Down Expand Up @@ -345,7 +350,8 @@ const Datepicker: React.FC<Props> = ({
inputName,
startWeekOn,
classNames,
inputRef
inputRef,
yearPickerStartYearOffset
]);

return (
Expand Down Expand Up @@ -377,6 +383,7 @@ const Datepicker: React.FC<Props> = ({
onClickNext={nextMonthFirst}
changeMonth={changeFirstMonth}
changeYear={changeFirstYear}
showYearPicker={showYearPicker}
/>

{useRange && (
Expand All @@ -391,6 +398,7 @@ const Datepicker: React.FC<Props> = ({
onClickNext={nextMonthSecond}
changeMonth={changeSecondMonth}
changeYear={changeSecondYear}
showYearPicker={showYearPicker}
/>
</>
)}
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/DatepickerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface DatepickerStore {
inputId?: string;
inputName?: string;
classNames?: ClassNamesTypeProp | undefined;
yearPickerStartYearOffset?: number;
}

const DatepickerContext = createContext<DatepickerStore>({
Expand Down Expand Up @@ -91,7 +92,8 @@ const DatepickerContext = createContext<DatepickerStore>({
inputName: undefined,
startWeekOn: START_WEEK,
toggleIcon: undefined,
classNames: undefined
classNames: undefined,
yearPickerStartYearOffset: undefined
});

export default DatepickerContext;