import React, { Component } from 'react'; import moment from 'moment'; import { Form, Grid } from 'semantic-ui-react'; import { defaultDateFormat } from '../../constants/constants'; class MonthSelector extends Component { constructor(props) { super(props); const initialDateTime = moment(); const initialMonth = (initialDateTime.month() === 0) ? 11 : initialDateTime.month() - 1; const initialYear = (initialDateTime.month() === 0) ? initialDateTime.year() - 1 : initialDateTime.year(); this.state = { month: initialMonth, year: initialYear, monthLabel: props.monthLabel || 'Month', yearLabel: props.yearLabel || 'Year', }; } onMonthSelectionChange = (event, data) => { const newMonthValue = data.value; this.setState({month: newMonthValue}); }; onYearChange = (event) => { const newYearValue = event.target.value ? parseInt(event.target.value) : this.state.year; this.setState({year: newYearValue}); }; onButtonClick() { const { onDatesUpdate } = this.props; const { month, year } = this.state; const monthYearMoment = moment().month(month).year(year); if (onDatesUpdate){ onDatesUpdate({ startDate: monthYearMoment.clone().startOf('month').format(defaultDateFormat), endDate: monthYearMoment.clone().endOf('month').format(defaultDateFormat), }); } } componentDidMount() { this.onButtonClick(); } render() { const { month, year, monthLabel, yearLabel } = this.state; const { inlineButton } = this.props; const buttonLabel = this.props.buttonLabel || 'Save'; const buttonRender = ( { inlineButton && } {buttonLabel} ); const monthDropdownOptions = [ {value: 0, text: 'January'}, {value: 1, text: 'February'}, {value: 2, text: 'March'}, {value: 3, text: 'April'}, {value: 4, text: 'May'}, {value: 5, text: 'June'}, {value: 6, text: 'July'}, {value: 7, text: 'August'}, {value: 8, text: 'September'}, {value: 9, text: 'October'}, {value: 10, text: 'November'}, {value: 11, text: 'December'}, ]; return (
{ inlineButton && buttonRender } { !inlineButton && {buttonRender} }
); } } export default MonthSelector;