Skip to content

Commit cfefeec

Browse files
committed
Disabling years outside maxDate and minDate range
1 parent 88b33d7 commit cfefeec

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/components/Calendar/Years.tsx

Lines changed: 6 additions & 1 deletion
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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ import Months from "./Months";
2626
import Week from "./Week";
2727
import Years from "./Years";
2828

29+
import { DateType } from "types";
30+
2931
interface Props {
3032
date: dayjs.Dayjs;
33+
minDate?: DateType | null;
34+
maxDate?: DateType | null;
3135
onClickPrevious: () => void;
3236
onClickNext: () => void;
3337
changeMonth: (month: number) => void;
@@ -36,6 +40,8 @@ interface Props {
3640

3741
const Calendar: React.FC<Props> = ({
3842
date,
43+
minDate,
44+
maxDate,
3945
onClickPrevious,
4046
onClickNext,
4147
changeMonth,
@@ -229,6 +235,14 @@ const Calendar: React.FC<Props> = ({
229235
}
230236
};
231237
}, [current, date, next, previous]);
238+
const minYear = React.useMemo(
239+
() => (minDate && dayjs(minDate).isValid() ? dayjs(minDate).year() : null),
240+
[minDate]
241+
);
242+
const maxYear = React.useMemo(
243+
() => (maxDate && dayjs(maxDate).isValid() ? dayjs(maxDate).year() : null),
244+
[maxDate]
245+
);
232246

233247
return (
234248
<div className="w-full md:w-[297px] md:min-w-[297px]">
@@ -303,7 +317,9 @@ const Calendar: React.FC<Props> = ({
303317
<div className="px-0.5 sm:px-2 mt-0.5 min-h-[285px]">
304318
{showMonths && <Months clickMonth={clickMonth} />}
305319

306-
{showYears && <Years year={year} clickYear={clickYear} />}
320+
{showYears && (
321+
<Years year={year} minYear={minYear} maxYear={maxYear} clickYear={clickYear} />
322+
)}
307323

308324
{!showMonths && !showYears && (
309325
<>

src/components/Datepicker.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ const Datepicker: React.FC<Props> = ({
367367
onClickNext={nextMonthFirst}
368368
changeMonth={changeFirstMonth}
369369
changeYear={changeFirstYear}
370+
minDate={minDate}
371+
maxDate={maxDate}
370372
/>
371373

372374
{useRange && (
@@ -381,6 +383,8 @@ const Datepicker: React.FC<Props> = ({
381383
onClickNext={nextMonthSecond}
382384
changeMonth={changeSecondMonth}
383385
changeYear={changeSecondYear}
386+
minDate={minDate}
387+
maxDate={maxDate}
384388
/>
385389
</>
386390
)}

src/components/utils.tsx

Lines changed: 6 additions & 3 deletions
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)