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 = (