Skip to content

Commit 81c91ca

Browse files
authored
Merge pull request #77 from devinmatte/disable-years-outside-range
Disabling years outside maxDate and minDate range
2 parents 8d8ba29 + cfefeec commit 81c91ca

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/components/Calendar/Years.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { RoundedButton } from "../utils";
55

66
interface Props {
77
year: number;
8+
minYear: number | null;
9+
maxYear: number | null;
810
clickYear: (data: number) => void;
911
}
1012

11-
const Years: React.FC<Props> = ({ year, clickYear }) => {
13+
const Years: React.FC<Props> = ({ year, minYear, maxYear, clickYear }) => {
1214
return (
1315
<div className="w-full grid grid-cols-2 gap-2 mt-2">
1416
{generateArrayNumber(year, year + 11).map((item, index) => (
@@ -18,6 +20,9 @@ const Years: React.FC<Props> = ({ year, clickYear }) => {
1820
onClick={() => {
1921
clickYear(item);
2022
}}
23+
disabled={
24+
(maxYear !== null && item > maxYear) || (minYear !== null && item < minYear)
25+
}
2126
>
2227
<>{item}</>
2328
</RoundedButton>

src/components/Calendar/index.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ import Months from "./Months";
2727
import Week from "./Week";
2828
import Years from "./Years";
2929

30+
import { DateType } from "types";
31+
3032
interface Props {
3133
date: dayjs.Dayjs;
34+
minDate?: DateType | null;
35+
maxDate?: DateType | null;
3236
onClickPrevious: () => void;
3337
onClickNext: () => void;
3438
changeMonth: (month: number) => void;
@@ -37,6 +41,8 @@ interface Props {
3741

3842
const Calendar: React.FC<Props> = ({
3943
date,
44+
minDate,
45+
maxDate,
4046
onClickPrevious,
4147
onClickNext,
4248
changeMonth,
@@ -226,6 +232,14 @@ const Calendar: React.FC<Props> = ({
226232
}
227233
};
228234
}, [current, date, next, previous]);
235+
const minYear = React.useMemo(
236+
() => (minDate && dayjs(minDate).isValid() ? dayjs(minDate).year() : null),
237+
[minDate]
238+
);
239+
const maxYear = React.useMemo(
240+
() => (maxDate && dayjs(maxDate).isValid() ? dayjs(maxDate).year() : null),
241+
[maxDate]
242+
);
229243

230244
return (
231245
<div className="w-full md:w-[297px] md:min-w-[297px]">
@@ -300,7 +314,9 @@ const Calendar: React.FC<Props> = ({
300314
<div className="px-0.5 sm:px-2 mt-0.5 min-h-[285px]">
301315
{showMonths && <Months clickMonth={clickMonth} />}
302316

303-
{showYears && <Years year={year} clickYear={clickYear} />}
317+
{showYears && (
318+
<Years year={year} minYear={minYear} maxYear={maxYear} clickYear={clickYear} />
319+
)}
304320

305321
{!showMonths && !showYears && (
306322
<>

src/components/Datepicker.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ const Datepicker: React.FC<DatepickerType> = ({
338338
onClickNext={nextMonthFirst}
339339
changeMonth={changeFirstMonth}
340340
changeYear={changeFirstYear}
341+
minDate={minDate}
342+
maxDate={maxDate}
341343
/>
342344

343345
{useRange && (
@@ -352,6 +354,8 @@ const Datepicker: React.FC<DatepickerType> = ({
352354
onClickNext={nextMonthSecond}
353355
changeMonth={changeSecondMonth}
354356
changeYear={changeSecondYear}
357+
minDate={minDate}
358+
maxDate={maxDate}
355359
/>
356360
</>
357361
)}

src/components/utils.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export const PrimaryButton: React.FC<Button> = ({ children, onClick, disabled =
169169
export const RoundedButton: React.FC<Button> = ({
170170
children,
171171
onClick,
172+
disabled,
172173
roundedFull = false,
173174
padding = "py-[0.55rem]"
174175
}) => {
@@ -183,11 +184,13 @@ export const RoundedButton: React.FC<Button> = ({
183184
: `${darkClass} transition-all duration-300 hover:bg-gray-100 rounded-full p-[0.45rem] focus:ring-1`;
184185
const buttonFocusColor =
185186
BUTTON_COLOR.focus[primaryColor as keyof typeof BUTTON_COLOR.focus];
186-
return `${defaultClass} ${buttonFocusColor}`;
187-
}, [padding, primaryColor, roundedFull]);
187+
const disabledClass = disabled ? "line-through" : "";
188+
189+
return `${defaultClass} ${buttonFocusColor} ${disabledClass}`;
190+
}, [disabled, padding, primaryColor, roundedFull]);
188191

189192
return (
190-
<button type="button" className={getClassName()} onClick={onClick}>
193+
<button type="button" className={getClassName()} onClick={onClick} disabled={disabled}>
191194
{children}
192195
</button>
193196
);

0 commit comments

Comments
 (0)