import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; import {range} from 'lodash'; import {gettext} from 'core/utils'; import {Popup, Content, Header, Footer} from '../../Popup'; import {Button} from '../../'; import './style.scss'; /** * @ngdoc react * @name TimeInputPopup * @description Main Popup Component of TimePicker */ export class TimeInputPopup extends React.Component { static propTypes: any; static defaultProps: any; hours: any; minutes: any; constructor(props) { super(props); this.state = { selectedHourIndex: 0, selectedMinuteIndex: 0, currentTime: moment(), }; this.hours = range(0, 24); this.minutes = range(0, 60, 5); } componentWillMount() { const {value} = this.props; let hourIndex; let minuteIndex; let inputDateTime = value && moment.isMoment(value) ? moment(value) : this.state.currentTime; // Round it to nearest 5 minutes mark if needed const diffMins = 5 - inputDateTime.minute() % 5; if (diffMins !== 5) { inputDateTime = inputDateTime.add(diffMins, 'm'); } hourIndex = inputDateTime.hour(); minuteIndex = inputDateTime.minute() / 5; this.setState({ selectedHourIndex: hourIndex, selectedMinuteIndex: minuteIndex, }); } setselectedHourIndex(val) { this.setState({selectedHourIndex: val}); } setselectedMinuteIndex(val) { this.setState({selectedMinuteIndex: val}); } handleConfirm(addMinutes) { const {onChange, close} = this.props; if (addMinutes) { this.state.currentTime.add(addMinutes, 'm'); onChange(this.state.currentTime.format('HH:mm')); } else { onChange(this.state.selectedHourIndex + ':' + (this.state.selectedMinuteIndex * 5)); } // Close the timepicker close(); } render() { return (
Hours
    {this.hours.map((hour, index) => (
  • {hour < 10 ? '0' + hour : hour}
  • ))}
Minutes
    {this.minutes.map((minute, index) => (
  • {minute < 10 ? '0' + minute : minute}
  • ))}
); } } TimeInputPopup.propTypes = { value: PropTypes.object, onChange: PropTypes.func.isRequired, close: PropTypes.func.isRequired, target: PropTypes.string.isRequired, popupContainer: PropTypes.func, };