diff --git a/cypress/e2e/spec.cy.js b/cypress/e2e/spec.cy.js index 9a37d49..3e2bd25 100644 --- a/cypress/e2e/spec.cy.js +++ b/cypress/e2e/spec.cy.js @@ -62,4 +62,18 @@ describe('basic', () => { .should('have.css', 'background-color') .and('match', /(rgb\(0, 0, 0\))/) }) + + it('changes month and year from dropdowns', function () { + cy.visit('localhost:8000') + + // Months are 0-indexed: 10 => November + cy.get('.preachjs-calendar--header-month').select('10') + cy.get('.preachjs-calendar--header-year').select('2030') + + cy.get('.preachjs-calendar--grid').should( + 'have.attr', + 'aria-label', + 'Nov 2030' + ) + }) }) diff --git a/example/src/main.css b/example/src/main.css index de3d7b2..3637bf6 100644 --- a/example/src/main.css +++ b/example/src/main.css @@ -153,6 +153,21 @@ nav a { font-weight: 600; } +.preachjs-calendar + .preachjs-calendar--header + .preachjs-calendar--header-selectors { + display: flex; + gap: 6px; +} + +.preachjs-calendar .preachjs-calendar--header select { + border: 1px solid #efefef; + border-radius: 6px; + padding: 4px 8px; + font-size: 14px; + background-color: white; +} + .preachjs-calendar .preachjs-calendar--grid { width: 100%; border-collapse: collapse; diff --git a/src/calendar.js b/src/calendar.js index 4e0185f..170f86c 100644 --- a/src/calendar.js +++ b/src/calendar.js @@ -9,6 +9,9 @@ import { sortByDate, } from './utils.js' +const YEAR_RANGE_OFFSET = 100 +const YEAR_OPTION_COUNT = YEAR_RANGE_OFFSET * 2 + 1 + /** * @param {import("./calendar").CalendarProps} props * @returns @@ -72,6 +75,18 @@ export function Calendar({ }) let tabIndexOffset = 3 + const monthLabel = new Intl.DateTimeFormat(locale, { month: 'long' }) + const yearInView = activeDate$.value.getFullYear() + const monthInView = activeDate$.value.getMonth() + const monthOptions = new Array(12).fill(null).map((_, monthIndex) => { + return { + value: monthIndex, + label: monthLabel.format(new Date(2000, monthIndex, 1)), + } + }) + const yearOptions = new Array(YEAR_OPTION_COUNT) + .fill(null) + .map((_, offset) => yearInView - YEAR_RANGE_OFFSET + offset) return (
@@ -87,7 +102,38 @@ export function Calendar({ > - +
+ + +