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

feat(disable): add callback to allow disable custom dates #99

Merged
merged 3 commits into from
Sep 15, 2024
Merged
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
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export default function App() {
startDate={range.startDate}
endDate={range.endDate}
dates={dates}
//isDateDisabled={(date) => [0, 6].includes(dayjs(date).day())} // disable weekends
//minDate={dayjs().startOf('day')}
//maxDate={dayjs().add(3, 'day').endOf('day')}
//disabledDates={[dayjs(), dayjs().add(1, 'day')]}
Expand Down
2 changes: 2 additions & 0 deletions src/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
onChange,
initialView = 'day',
height,
isDateDisabled,
...rest
} = props;

Expand Down Expand Up @@ -133,21 +134,21 @@
currentYear: action.payload,
};
case CalendarActionKind.CHANGE_SELECTED_DATE:
const { date } = action.payload;

Check warning on line 137 in src/DateTimePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

'date' is already declared in the upper scope on line 77 column 5
return {
...prevState,
date,
currentDate: date,
};
case CalendarActionKind.CHANGE_SELECTED_RANGE:
const { startDate, endDate } = action.payload;

Check warning on line 144 in src/DateTimePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

'startDate' is already declared in the upper scope on line 78 column 5

Check warning on line 144 in src/DateTimePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

'endDate' is already declared in the upper scope on line 79 column 5
return {
...prevState,
startDate,
endDate,
};
case CalendarActionKind.CHANGE_SELECTED_MULTIPLE:
const { dates } = action.payload;

Check warning on line 151 in src/DateTimePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

'dates' is already declared in the upper scope on line 80 column 5
return {
...prevState,
dates,
Expand Down Expand Up @@ -192,7 +193,7 @@
}, []);

const onSelectDate = useCallback(
(date: DateType) => {

Check warning on line 196 in src/DateTimePicker.tsx

View workflow job for this annotation

GitHub Actions / lint

'date' is already declared in the upper scope on line 77 column 5
if (onChange) {
if (mode === 'single') {
const newDate = timePicker ? date : getStartOfDay(date);
Expand Down Expand Up @@ -309,6 +310,7 @@
onSelectYear,
onChangeMonth,
onChangeYear,
isDateDisabled,
}}
>
<Calendar
Expand Down
1 change: 1 addition & 0 deletions src/components/DaySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
displayFullDays,
minDate,
maxDate,
firstDayOfWeek,
disabledDates,
firstDayOfWeek
).map((day, index) => {
Expand All @@ -57,7 +58,7 @@
let rightCrop = day.dayOfMonth === fullDaysInMonth;

const isFirstDayOfMonth = day.dayOfMonth === 1;
const isLastDayOfMonth = ((day?.dayOfMonth || 0) - ((day?.dayOfMonth || 0) - day.day)) === fullDaysInMonth;

Check failure on line 61 in src/components/DaySelector.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·((day?.dayOfMonth·||·0)·-·((day?.dayOfMonth·||·0)·-·day.day))·===` with `⏎············(day?.dayOfMonth·||·0)·-·((day?.dayOfMonth·||·0)·-·day.day)·===⏎···········`

const isToday = areDatesOnSameDay(day.date, today);
let inRange = false;
Expand Down Expand Up @@ -89,7 +90,7 @@
if (
(isFirstDayOfMonth && selectedEndDay) ||
(isLastDayOfMonth && selectedStartDay) ||
dayjs(startDate).format('DDMMYYYY') === dayjs(endDate).format('DDMMYYYY')

Check failure on line 93 in src/components/DaySelector.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎···············`
) {
inRange = false;
}
Expand Down
15 changes: 10 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs';
import type { DateType, IDayObject } from './types';
import type { DatePickerBaseProps, DateType, IDayObject } from './types';

export const CALENDAR_FORMAT = 'YYYY-MM-DD HH:mm';
export const DATE_FORMAT = 'YYYY-MM-DD';
Expand Down Expand Up @@ -188,6 +188,7 @@ export const getMonthDays = (
displayFullDays: boolean,
minDate: DateType,
maxDate: DateType,
firstDayOfWeek: number,
disabledDates: DateType[] | ((date: DateType) => boolean) | undefined,
firstDayOfWeek: number
): IDayObject[] => {
Expand All @@ -210,7 +211,8 @@ export const getMonthDays = (
maxDate,
disabledDates,
false,
index + 1
index + 1,
isDateDisabled
);
})
: Array(prevMonthOffset).fill(null);
Expand All @@ -225,7 +227,8 @@ export const getMonthDays = (
maxDate,
disabledDates,
true,
prevMonthOffset + day
prevMonthOffset + day,
isDateDisabled
);
});

Expand All @@ -239,7 +242,8 @@ export const getMonthDays = (
maxDate,
disabledDates,
false,
daysInCurrentMonth + prevMonthOffset + day
daysInCurrentMonth + prevMonthOffset + day,
isDateDisabled
);
});

Expand All @@ -265,7 +269,8 @@ const generateDayObject = (
maxDate: DateType,
disabledDates: DateType[] | ((date: DateType) => boolean) | undefined,
isCurrentMonth: boolean,
dayOfMonth: number
dayOfMonth: number,
isDateDisabled?: DatePickerBaseProps['isDateDisabled']
) => {
return {
text: day.toString(),
Expand Down
Loading