Skip to content

Commit fd557e2

Browse files
committed
Add active / current selected month and year
1 parent f0c755d commit fd557e2

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/components/Calendar/Months.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { loadLanguageModule } from "../../helpers";
77
import { RoundedButton } from "../utils";
88

99
interface Props {
10+
currentMonth: number;
1011
clickMonth: (month: number) => void;
1112
}
1213

13-
const Months: React.FC<Props> = ({ clickMonth }) => {
14+
const Months: React.FC<Props> = ({ currentMonth, clickMonth }) => {
1415
const { i18n } = useContext(DatepickerContext);
1516
loadLanguageModule(i18n);
1617
return (
@@ -22,6 +23,7 @@ const Months: React.FC<Props> = ({ clickMonth }) => {
2223
onClick={() => {
2324
clickMonth(item);
2425
}}
26+
active={currentMonth === item}
2527
>
2628
<>{dayjs(`2022-${item}-01`).locale(i18n).format("MMM")}</>
2729
</RoundedButton>

src/components/Calendar/Years.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import DatepickerContext from "contexts/DatepickerContext";
77

88
interface Props {
99
year: number;
10+
currentYear: number;
1011
minYear: number | null;
1112
maxYear: number | null;
1213
clickYear: (data: number) => void;
1314
}
1415

15-
const Years: React.FC<Props> = ({ year, minYear, maxYear, clickYear }) => {
16+
const Years: React.FC<Props> = ({ year, currentYear, minYear, maxYear, clickYear }) => {
1617
const { dateLooking } = useContext(DatepickerContext);
1718

1819
let startDate = 0;
@@ -43,6 +44,7 @@ const Years: React.FC<Props> = ({ year, minYear, maxYear, clickYear }) => {
4344
onClick={() => {
4445
clickYear(item);
4546
}}
47+
active={currentYear === item}
4648
disabled={
4749
(maxYear !== null && item > maxYear) || (minYear !== null && item < minYear)
4850
}

src/components/Calendar/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,18 @@ const Calendar: React.FC<Props> = ({
312312
</div>
313313

314314
<div className="px-0.5 sm:px-2 mt-0.5 min-h-[285px]">
315-
{showMonths && <Months clickMonth={clickMonth} />}
315+
{showMonths && (
316+
<Months currentMonth={calendarData.date.month() + 1} clickMonth={clickMonth} />
317+
)}
316318

317319
{showYears && (
318-
<Years year={year} minYear={minYear} maxYear={maxYear} clickYear={clickYear} />
320+
<Years
321+
year={year}
322+
minYear={minYear}
323+
maxYear={maxYear}
324+
currentYear={calendarData.date.year()}
325+
clickYear={clickYear}
326+
/>
319327
)}
320328

321329
{!showMonths && !showYears && (

src/components/utils.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface Button {
1313
disabled?: boolean;
1414
roundedFull?: boolean;
1515
padding?: string;
16+
active?: boolean;
1617
}
1718

1819
export const DateIcon: React.FC<IconProps> = ({ className = "w-6 h-6" }) => {
@@ -171,23 +172,25 @@ export const RoundedButton: React.FC<Button> = ({
171172
onClick,
172173
disabled,
173174
roundedFull = false,
174-
padding = "py-[0.55rem]"
175+
padding = "py-[0.55rem]",
176+
active = false
175177
}) => {
176178
// Contexts
177179
const { primaryColor } = useContext(DatepickerContext);
178180

179181
// Functions
180182
const getClassName = useCallback(() => {
181183
const darkClass = "dark:text-white/70 dark:hover:bg-white/10 dark:focus:bg-white/10";
184+
const activeClass = active ? "font-semibold bg-gray-50 dark:bg-white/5" : "";
182185
const defaultClass = !roundedFull
183-
? `w-full tracking-wide ${darkClass} transition-all duration-300 px-3 ${padding} uppercase hover:bg-gray-100 rounded-md focus:ring-1`
184-
: `${darkClass} transition-all duration-300 hover:bg-gray-100 rounded-full p-[0.45rem] focus:ring-1`;
186+
? `w-full tracking-wide ${darkClass} ${activeClass} transition-all duration-300 px-3 ${padding} uppercase hover:bg-gray-100 rounded-md focus:ring-1`
187+
: `${darkClass} ${activeClass} transition-all duration-300 hover:bg-gray-100 rounded-full p-[0.45rem] focus:ring-1`;
185188
const buttonFocusColor =
186189
BUTTON_COLOR.focus[primaryColor as keyof typeof BUTTON_COLOR.focus];
187190
const disabledClass = disabled ? "line-through" : "";
188191

189192
return `${defaultClass} ${buttonFocusColor} ${disabledClass}`;
190-
}, [disabled, padding, primaryColor, roundedFull]);
193+
}, [disabled, padding, primaryColor, roundedFull, active]);
191194

192195
return (
193196
<button type="button" className={getClassName()} onClick={onClick} disabled={disabled}>

0 commit comments

Comments
 (0)