6

I am currently trying to add a Custom Header to my react-datepicker calendar. However my calendar also displays multiple months so when I use the renderCustomHeader prop, it only adds a custom header to the first calendar displayed and the second calendar ends up with no header.

example calendar

This is what the code currently looks like:

<DatePicker
        renderCustomHeader={({
            date,
            changeYear,
            changeMonth,
            decreaseMonth,
            increaseMonth,
            prevMonthButtonDisabled,
            nextMonthButtonDisabled
        }) => (
            <div
                style={{
                    margin: 10,
                    display: 'flex',
                    justifyContent: 'center'
                }}
            >
                <button onClick={decreaseMonth} disabled={prevMonthButtonDisabled}>
                    {'<'}
                </button>
                <select
                    value={date.getFullYear()}
                    onChange={({ target: { value } }) => changeYear(value)}
                >
                    {years.map(option => (
                        <option key={option} value={option}>
                            {option}
                        </option>
                    ))}
                </select>
    
                <select
                    value={months[date.getMonth()]}
                    onChange={({ target: { value } }) =>
                        changeMonth(months.indexOf(value))
                    }
                >
                    {months.map(option => (
                        <option key={option} value={option}>
                            {option}
                        </option>
                    ))}
                </select>
    
                <button onClick={increaseMonth} disabled={nextMonthButtonDisabled}>
                    {'>'}
                </button>
            </div>
        )}
        selected={startDate}
        onChange={ date => onChange(date) }
        renderDayContents={renderDayContents}
        monthsShown={2}
    />

How can I target the second calendar to add a Custom Header?

Duskendale
  • 142
  • 4
  • 13
  • Why not using mutliple datepickers with a single header instead of using only one datepicker with multiple headers ? BTW your example was super useful to me. It is super clear how to make your own customaizable header through props Thanks! – yaritaft Oct 20 '21 at 04:35

0 Answers0