/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ import React from "react"; import PropTypes from "prop-types"; import FocusTrap from "focus-trap-react"; import classNames from "classnames"; import { ScreenReaderOnly } from "./screen_reader_only"; import { addMonths, cloneDate, formatDate, getStartOfMonth, isAfter, isSameMonth, isSameYear, isBefore } from "./date_utils"; function generateMonthYears(minDate, maxDate) { const list = []; const currDate = getStartOfMonth(cloneDate(minDate)); const lastDate = getStartOfMonth(cloneDate(maxDate)); while (!isAfter(currDate, lastDate)) { list.push(cloneDate(currDate)); addMonths(currDate, 1); } return list; } export default class MonthYearDropdownOptions extends React.Component { static propTypes = { minDate: PropTypes.object.isRequired, maxDate: PropTypes.object.isRequired, onCancel: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, scrollableMonthYearDropdown: PropTypes.bool, date: PropTypes.object.isRequired, dateFormat: PropTypes.string.isRequired, accessibleMode: PropTypes.bool }; constructor(props) { super(props); this.state = { monthYearsList: generateMonthYears( this.props.minDate, this.props.maxDate ), preSelection: getStartOfMonth(cloneDate(this.props.date)), readInstructions: false }; } componentDidMount() { if (this.preSelectionDiv) { this.preSelectionDiv.scrollIntoView({ behavior: "instant", block: "nearest", inline: "nearest" }); } } componentDidUpdate(prevProps, prevState) { if (this.preSelectionDiv) { this.preSelectionDiv.scrollIntoView({ behavior: "instant", block: "nearest", inline: "nearest" }); } } renderOptions = () => { return this.state.monthYearsList.map(monthYear => { const monthYearPoint = monthYear.valueOf(); const isSameMonthYear = isSameYear(this.props.date, monthYear) && isSameMonth(this.props.date, monthYear); const isPreselectionSameMonthYear = isSameYear(this.state.preSelection, monthYear) && isSameMonth(this.state.preSelection, monthYear); return (
You are focused on a month / year selector menu. Use the up and down arrows to select a month / year combination, then hit enter to confirm your selection. {formatDate(this.state.preSelection, this.props.dateFormat)} is the currently focused month / year.
); } return this.props.accessibleMode ? (