import React from 'react'; import style from './style'; import time from '../utils/time'; import Clock from './clock'; import Dialog from '../dialog'; class TimePickerDialog extends React.Component { static propTypes = { active: React.PropTypes.bool, format: React.PropTypes.oneOf(['24hr', 'ampm']), onDismiss: React.PropTypes.func, onSelect: React.PropTypes.func, value: React.PropTypes.object }; static defaultProps = { active: false, format: '24hr', value: new Date() }; state = { display: 'hours', displayTime: this.props.value }; componentWillUpdate (nextProps) { if (!this.props.active && nextProps.active) { setTimeout(this.refs.clock.handleCalculateShape, 1000); } } handleClockChange = (value) => { this.setState({displayTime: value}); }; handleSelect = () => { this.props.onSelect(this.state.displayTime); }; toggleTimeMode = () => { this.setState({displayTime: time.toggleTimeMode(this.state.displayTime)}); }; switchDisplay = (display) => { this.setState({display}); }; actions = [ { label: 'Cancel', className: style.button, onClick: this.props.onDismiss }, { label: 'Ok', className: style.button, onClick: this.handleSelect } ]; formatHours () { if (this.props.format === 'ampm') { return this.state.displayTime.getHours() % 12 || 12; } else { return this.state.displayTime.getHours(); } } renderAMPMLabels () { if (this.props.format === 'ampm') { return (
AM PM
); } } render () { const display = `display-${this.state.display}`; const format = `format-${time.getTimeMode(this.state.displayTime)}`; const className = `${style.dialog} ${style[display]} ${style[format]}`; return (
{ ('0' + this.formatHours()).slice(-2) } : { ('0' + this.state.displayTime.getMinutes()).slice(-2) } { this.renderAMPMLabels() }
); } } export default TimePickerDialog;