Created
October 5, 2020 12:34
-
-
Save maxgfr/e51506db6f3cfdfe3703614beeeafc68 to your computer and use it in GitHub Desktop.
Day Picker based on Airbnb react-dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState } from 'react' | |
| import { SingleDatePicker } from 'react-dates' | |
| import moment from 'moment' | |
| export function AppDayPicker({ value, onChange, ...props }) { | |
| const [date, setDate] = useState(null) | |
| const [focusedInput, setFocusedInput] = useState(null) | |
| const returnYears = () => { | |
| let years = [] | |
| for (let i = moment().year() - 100; i <= moment().year(); i++) { | |
| years.push(<option value={i}>{i}</option>) | |
| } | |
| return years | |
| } | |
| const renderMonthElement = ({ month, onMonthSelect, onYearSelect }) => ( | |
| <div style={{ display: 'flex', justifyContent: 'center' }}> | |
| <div> | |
| <select | |
| value={month.month()} | |
| onChange={(e) => onMonthSelect(month, e.target.value)} | |
| > | |
| {moment.months().map((label, value) => ( | |
| <option value={value}>{label}</option> | |
| ))} | |
| </select> | |
| </div> | |
| <div> | |
| <select | |
| value={month.year()} | |
| onChange={(e) => onYearSelect(month, e.target.value)} | |
| > | |
| {returnYears()} | |
| </select> | |
| </div> | |
| </div> | |
| ) | |
| return ( | |
| <SingleDatePicker | |
| date={date} | |
| focused={focusedInput} | |
| onDateChange={(date) => { | |
| setDate(date) | |
| }} | |
| onFocusChange={(focused) => { | |
| setFocusedInput(focused) | |
| }} | |
| renderMonthElement={renderMonthElement} | |
| /> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment