/* * 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 classNames from "classnames"; import FocusTrap from "focus-trap-react"; import { ScreenReaderOnly } from "./screen_reader_only"; function generateYears(year, noOfYear, minDate, maxDate) { var list = []; for (var i = 0; i < 2 * noOfYear + 1; i++) { const newYear = year + noOfYear - i; let isInRange = true; if (minDate) { isInRange = minDate.year() <= newYear; } if (maxDate && isInRange) { isInRange = maxDate.year() >= newYear; } if (isInRange) { list.push(newYear); } } return list; } export default class YearDropdownOptions extends React.Component { static propTypes = { minDate: PropTypes.object, maxDate: PropTypes.object, onCancel: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired, scrollableYearDropdown: PropTypes.bool, year: PropTypes.number.isRequired, yearDropdownItemNumber: PropTypes.number, accessibleMode: PropTypes.bool }; constructor(props) { super(props); const { yearDropdownItemNumber, scrollableYearDropdown } = props; const noOfYear = yearDropdownItemNumber || (scrollableYearDropdown ? 10 : 5); this.state = { yearsList: generateYears( this.props.year, noOfYear, this.props.minDate, this.props.maxDate ), preSelection: this.props.year, readInstructions: false }; } componentDidMount() { if (this.preSelectionDiv) { this.preSelectionDiv.scrollIntoView({ behavior: "instant", block: "nearest", inline: "nearest" }); } } componentDidUpdate(prevProps, prevState) { if ( this.preSelectionDiv && prevState.preSelection !== this.state.preSelection ) { this.preSelectionDiv.scrollIntoView({ behavior: "instant", block: "nearest", inline: "nearest" }); } } renderOptions = () => { var selectedYear = this.props.year; var options = this.state.yearsList.map(year => (
You are focused on a year selector menu. Use the up and down arrows to select a year, then hit enter to confirm your selection. {this.state.preSelection} is the currently focused year.
); } return this.props.accessibleMode ? (