react-multi-date-picker icon indicating copy to clipboard operation
react-multi-date-picker copied to clipboard

Limit number of picked days

Open Ansaen opened this issue 2 years ago • 3 comments

Hello, Is there some way to limit number of picked days?

Obviously i am using "multiple". Let´s say i would like to limit it to 3 days, so when u pick 3 different days u can´t choose more but, when u deselect one of them u can again pick another date.

Any idea ?

Ansaen avatar May 28 '22 17:05 Ansaen

Hi there,

I am also facing the same issue. Were you able to fix this? @Ansaen

ibrahimrehman1 avatar Nov 15 '22 15:11 ibrahimrehman1

The solution I found is to use the mapDays prop and pass the disabled prop when reaching the limit for other than selected days:

value={values}
mapDays={({ date }) => {
    const reachLimit = values.length === selectionLimit;
    const isNotSelectedDate = !values.some(val => moment(val).isSame(moment(date.toDate())));

    return reachLimit && isNotSelectedDate ? { disabled: true } : {};
}}

michsko544 avatar Jan 19 '23 15:01 michsko544

Thank you for this code, that helped a lot! In my calendar I wanted to disable all dates when a limit was reached except the selected dates, so that users could de-select / modify.

"params.max" is the limit, passed into React as a WordPress JS localization.

mapDays={({ date, selectedDate, isSameDate }) => {
	const reachLimit = values.length >= params.max;
	const isNotSelectedDate = !selectedDate.some(val => {
		return isSameDate(date, val);
	});
	return reachLimit && isNotSelectedDate ? { disabled: true } : {};
}}

I'm using the properties date, selectedDate and the function isSameDate as detailed here: https://shahabyazdi.github.io/react-multi-date-picker/map-days/

EdithAllison avatar Nov 30 '23 09:11 EdithAllison