Skip to content

Commit

Permalink
Merge branch 'master' into data-table-layout-cells-on-columns-change
Browse files Browse the repository at this point in the history
  • Loading branch information
chasestarr committed Jan 8, 2021
2 parents 1a7da1e + ab82638 commit a88c1ec
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 178 deletions.
47 changes: 27 additions & 20 deletions src/datepicker/calendar-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ export default class CalendarHeader<T = Date> extends React.Component<
};

dateHelpers: DateHelpers<T>;
items: Array<{id: string, label: string}>;
minYear: number;
maxYear: number;

constructor(props: HeaderPropsT<T>) {
super(props);
this.dateHelpers = new DateHelpers(props.adapter);
this.items = [];
}

state = {
Expand Down Expand Up @@ -350,28 +354,31 @@ export default class CalendarHeader<T = Date> extends React.Component<
(x, i) => i + minDateMonth,
);

const items = [];

for (let i = minYear; i <= maxYear; i++) {
let months;
if (i === minYear && i === maxYear) {
months = maxYearMonths.filter(month => minYearMonths.includes(month));
} else if (i === minYear) {
months = minYearMonths;
} else if (i === maxYear) {
months = maxYearMonths;
} else {
months = defaultMonths;
}
months.forEach(month => {
items.push({
id: yearMonthToId(i, month),
label: `${this.dateHelpers.getMonthInLocale(month, locale)} ${i}`,
if (this.maxYear !== maxYear || this.minYear !== minYear) {
this.maxYear = maxYear;
this.minYear = minYear;
this.items = [];
for (let i = minYear; i <= maxYear; i++) {
let months;
if (i === minYear && i === maxYear) {
months = maxYearMonths.filter(month => minYearMonths.includes(month));
} else if (i === minYear) {
months = minYearMonths;
} else if (i === maxYear) {
months = maxYearMonths;
} else {
months = defaultMonths;
}
months.forEach(month => {
this.items.push({
id: yearMonthToId(i, month),
label: `${this.dateHelpers.getMonthInLocale(month, locale)} ${i}`,
});
});
});
}
}

const initialIndex = items.findIndex(item => {
const initialIndex = this.items.findIndex(item => {
return (
item.id ===
yearMonthToId(
Expand Down Expand Up @@ -408,7 +415,7 @@ export default class CalendarHeader<T = Date> extends React.Component<
highlightedIndex: initialIndex,
isFocused: true,
}}
items={items}
items={this.items}
onItemSelect={({item, event}) => {
event.preventDefault();
const [year, month] = idToYearMonth(item.id);
Expand Down

0 comments on commit a88c1ec

Please sign in to comment.