import * as ArrowIcon from 'material-design-icons/hardware/svg/production/ic_keyboard_arrow_down_24px.svg'; // Libs import * as React from 'react'; // Misc import InputGroup from '../inputGroup/InputGroup'; import { BaseInputProps, BaseInputState, BaseInput, } from '../base/input/BaseInput'; import { withFormContext } from '../form/withFormContext'; import TimeUtil from '../utils/TimeUtil'; import moment, { Duration } from 'moment'; import { withThemeContext } from '../themeProvider/withThemeContext'; import classNames from 'classnames'; export interface TimeProps extends BaseInputProps { onChange?: (e: React.ChangeEvent) => void; timeChange: (time: Duration) => void; time: Duration; min?: Duration; max?: Duration; showDateDiff?: boolean; } export interface TimeState extends BaseInputState { hoursText?: string; minutesText?: string; } export class TimeRaw extends BaseInput { public static defaultProps = Object.assign({}, BaseInput.defaultProps, { type: 'time', placeholder: '', }) as unknown as TimeProps; constructor(props: TimeProps) { super(props); this.handleHoursChange = this.handleHoursChange.bind(this); this.handleMinutesChange = this.handleMinutesChange.bind(this); } public render() { let hours = this.props.time?.hours?.() ?? 0; let minutes = this.props.time?.minutes?.() ?? 0; if (minutes < 0) { hours--; } minutes = (minutes + 60) % 60; hours = (hours + 24) % 24; var mid = 'AM'; if (hours % 24 >= 12) { hours = hours % 12; mid = 'PM'; } if (hours % 12 === 0) { // At 00 hours we need to show 12 am hours = 12; } if (this.props.showDateDiff && this.props.min) { const diffDays = TimeUtil.dayDiff(this.props.min, this.props.time); if (diffDays > 0) { mid += ' +' + diffDays.toString(); } } return (
:
{this.renderDefaultValidation()} {mid} {this.props.label && ( )}
); } private onBlur = () => { if (this.state.hoursText) { this.handleHoursChange(this.state.hoursText); } if (this.state.minutesText) { this.handleMinutesChange(this.state.minutesText); } this.setState({ minutesText: undefined, hoursText: undefined }, () => this.handleBlur() ); }; private onMinutesChanged = (e: React.ChangeEvent) => { // this.handleMinutesChange(e.target.value); this.setState({ minutesText: e.target.value }); }; private removeMinuteClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleMinutesChange( ((this.props.time?.minutes?.() ?? 0) - 1).toString() ); }; private addMinuteClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleMinutesChange( ((this.props.time?.minutes?.() ?? 0) + 1).toString() ); }; private removeHourClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleHoursChange(((this.props.time?.hours?.() ?? 0) - 1).toString()); }; private onHoursChanged = (e: React.ChangeEvent) => { // this.handleHoursChange(e.target.value); this.setState({ hoursText: e.target.value }); }; private addHourClick = (e: React.MouseEvent) => { e.preventDefault(); this.handleHoursChange(((this.props.time?.hours?.() ?? 0) + 1).toString()); }; private handleLimits(time: Duration) { if (this.props.min) { if ( (time?.asMilliseconds?.() ?? 0) - this.props.min.asMilliseconds() <= 0 ) { this.props.timeChange(this.props.min.clone()); return; } } if (this.props.max) { if ( this.props.max.asMilliseconds() - (time?.asMilliseconds?.() ?? 0) <= 0 ) { this.props.timeChange(this.props.max.clone()); return; } } this.props.timeChange(time); } private handleHoursChange(hoursString: string) { let num = Number(hoursString); if (hoursString === '') { num = 0; } if (!isNaN(num)) { let newTime: Duration = moment .duration(this.props.time?.asMilliseconds?.() ?? 0, 'milliseconds') .subtract(this.props.time?.hours?.() ?? 0, 'hours') .add(num, 'hours'); this.handleLimits(newTime); } } private handleMinutesChange(minutesString: string) { let num = Number(minutesString); if (minutesString === '') { num = 0; } if (!isNaN(num)) { let newTime: Duration = moment .duration(this.props.time?.asMilliseconds?.() ?? 0, 'milliseconds') .subtract(this.props.time?.minutes?.() ?? 0, 'minutes') .add(num, 'minutes'); this.handleLimits(newTime); } } } export const Time = withThemeContext>( withFormContext(TimeRaw), 'time' ); export default Time;