You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code example here (Switch between years and months) for captionElement doesn't take into consideration what month your toMonth or endMonth props fall on.
For example, if your forMonth prop falls on May of 2016 the component will still render Jan - Dec for 2016 but instead, you would only want it to show and start on May
Here's a code example for the desired functionality:
function YearMonthForm({ date, localeUtils, onChange }) {
const months = localeUtils.getMonths();
const monthsToRender = [];
let currentYear = date.getFullYear();
for (let month in months) {
var dt = new Date(currentYear, month);
var c = new Date(toMonth.getTime());
c.setDate(c.getDate()+1);
var inRange = dt >= fromMonth && dt < toMonth;
if (inRange) {
monthsToRender.push(months[month]);
};
};
const years = [];
for (let i = fromMonth.getFullYear(); i <= toMonth.getFullYear(); i += 1) {
years.push(i);
}
const handleChange = function handleChange(e) {
const { year, month } = e.target.form;
onChange(new Date(year.value, month.value));
};
return (
<form className="DayPicker-Caption">
<select name="month" onChange={handleChange} value={date.getMonth()}>
{monthsToRender.map((month, i) => (
<option key={month} value={i}>
{month}
</option>
))}
</select>
<select name="year" onChange={handleChange} value={date.getFullYear()}>
{years.map(year => (
<option key={year} value={year}>
{year}
</option>
))}
</select>
</form>
);
};
This discussion was converted from issue #851 on February 10, 2021 02:56.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The code example here (Switch between years and months) for captionElement doesn't take into consideration what month your
toMonth
orendMonth
props fall on.For example, if your
forMonth
prop falls on May of 2016 the component will still render Jan - Dec for 2016 but instead, you would only want it to show and start on MayHere's a code example for the desired functionality:
Beta Was this translation helpful? Give feedback.
All reactions