I am using plugin react-datepicker . I am trying to highlight multiple dates from array, but now, just only date first array ( date: "04/16/2021" ) have been a successful highlight. how to select highlight multiple dates from data array?.
for example
I have code:
Please tell me, my error code
Replace this
return (
<div className="white-box p-20">
{(() => {
for (let index = 0; index < array.length; index++) {
let highlight = subDays(new Date(`${array[index].date}`), 0);
console.log(highlight);
return (
<DatePicker
locale="id"
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled
}) => (
<div
style={{
marginBottom: "20px",
display: "flex",
justifyContent: "center"
}}
>
<button
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
className="btn-left"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
<select
value={months[date.getMonth()]}
onChange={({ target: { value } }) =>
changeMonth(months.indexOf(value))
}
className="mr-2"
>
{months.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<select
value={date.getFullYear()}
onChange={({ target: { value } }) => changeYear(value)}
>
{years.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<button
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
className="btn-right"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
</div>
)}
selected={startDate}
dateFormat="MM/dd/yyyy"
highlightDates={[highlight]}
inline
/>
);
}
})()}
</div>
);
with this
let highlight = array.map(date => new Date(date.date));
return (
<div className="white-box p-20">
<DatePicker
locale="id"
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled
}) => (
<div
style={{
marginBottom: "20px",
display: "flex",
justifyContent: "center"
}}
>
<button
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
className="btn-left"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
<select
value={months[date.getMonth()]}
onChange={({ target: { value } }) =>
changeMonth(months.indexOf(value))
}
className="mr-2"
>
{months.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<select
value={date.getFullYear()}
onChange={({ target: { value } }) => changeYear(value)}
>
{years.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<button
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
className="btn-right"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
</div>
)}
selected={startDate}
dateFormat="MM/dd/yyyy"
highlightDates={highlight}
inline
/>
</div>
);
Actually In your code is was couple of issues
for (let index = 0; index < array.length; index++) {
highlight.push(subDays(new Date(`${array[index].date}`), 0));
}
let highlight =[];
for (let index = 0; index < array.length; index++) {
highlight.push(subDays(new Date(`${array[index].date}`), 0));
}
highlightDates={highlight}
Complete code is here: https://codesandbox.io/embed/laughing-platform-0mur1?fontsize=14&hidenavigation=1&theme=dark
return (
<div className="white-box p-20">
{(() => {
let highlight =[];
for (let index = 0; index < array.length; index++) {
highlight.push(subDays(new Date(`${array[index].date}`), 0));
}
return (
<DatePicker
locale="id"
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled
}) => (
<div
style={{
marginBottom: "20px",
display: "flex",
justifyContent: "center"
}}
>
<button
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
className="btn-left"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
<select
value={months[date.getMonth()]}
onChange={({ target: { value } }) =>
changeMonth(months.indexOf(value))
}
className="mr-2"
>
{months.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<select
value={date.getFullYear()}
onChange={({ target: { value } }) => changeYear(value)}
>
{years.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<button
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
className="btn-right"
>
<img src="https://cutt.ly/fvNPwH5" />
</button>
</div>
)}
selected={startDate}
dateFormat="MM/dd/yyyy"
highlightDates={highlight}
inline
/>
);
})()}
</div>
);
}
export default Dates;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.