react-toolbox/components/time_picker/TimePickerDialog.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-05-22 14:25:56 +03:00
import classnames from 'classnames';
2015-09-09 23:34:56 +03:00
import time from '../utils/time';
2015-11-23 00:13:27 +03:00
import Clock from './Clock';
2015-09-09 23:34:56 +03:00
import Dialog from '../dialog';
2015-09-06 01:12:29 +03:00
2015-10-22 02:31:17 +03:00
class TimePickerDialog extends React.Component {
static propTypes = {
2015-11-10 17:07:14 +03:00
active: React.PropTypes.bool,
className: React.PropTypes.string,
2015-09-06 01:12:29 +03:00
format: React.PropTypes.oneOf(['24hr', 'ampm']),
2015-11-10 23:39:47 +03:00
onDismiss: React.PropTypes.func,
onSelect: React.PropTypes.func,
2016-05-22 14:25:56 +03:00
theme: React.PropTypes.shape({
am: React.PropTypes.string.isRequired,
amFormat: React.PropTypes.string.isRequired,
ampm: React.PropTypes.string.isRequired,
button: React.PropTypes.string.isRequired,
dialog: React.PropTypes.string.isRequired,
header: React.PropTypes.string.isRequired,
hours: React.PropTypes.string.isRequired,
hoursDisplay: React.PropTypes.string.isRequired,
minutes: React.PropTypes.string.isRequired,
2016-05-22 20:09:38 +03:00
minutesDisplay: React.PropTypes.string.isRequired,
2016-05-22 14:25:56 +03:00
pm: React.PropTypes.string.isRequired,
pmFormat: React.PropTypes.string.isRequired,
separator: React.PropTypes.string.isRequired
}),
2015-11-10 23:39:47 +03:00
value: React.PropTypes.object
};
2015-09-06 01:12:29 +03:00
static defaultProps = {
2015-11-10 17:07:14 +03:00
active: false,
2015-11-10 23:39:47 +03:00
format: '24hr',
value: new Date()
};
2015-09-06 01:12:29 +03:00
state = {
display: 'hours',
2015-11-10 23:39:47 +03:00
displayTime: this.props.value
};
2015-09-06 01:12:29 +03:00
componentDidUpdate (prevProps) {
if (!prevProps.active && this.props.active) {
2015-11-10 23:39:47 +03:00
setTimeout(this.refs.clock.handleCalculateShape, 1000);
}
}
2015-10-22 02:31:17 +03:00
2015-11-10 23:39:47 +03:00
handleClockChange = (value) => {
this.setState({displayTime: value});
2015-10-22 02:31:17 +03:00
};
handleSelect = (event) => {
this.props.onSelect(this.state.displayTime, event);
2015-10-22 02:31:17 +03:00
};
toggleTimeMode = () => {
2015-11-10 23:39:47 +03:00
this.setState({displayTime: time.toggleTimeMode(this.state.displayTime)});
2015-10-22 02:31:17 +03:00
};
2015-09-06 01:12:29 +03:00
handleHandMoved = () => {
if (this.state.display === 'hours') this.setState({display: 'minutes'});
};
2015-11-10 23:39:47 +03:00
switchDisplay = (display) => {
this.setState({display});
};
2015-09-06 01:12:29 +03:00
2015-11-10 23:39:47 +03:00
actions = [
2016-05-22 14:25:56 +03:00
{ label: 'Cancel', className: this.props.theme.button, onClick: this.props.onDismiss },
{ label: 'Ok', className: this.props.theme.button, onClick: this.handleSelect }
2015-11-10 23:39:47 +03:00
];
2015-09-06 01:12:29 +03:00
formatHours () {
2015-09-06 01:12:29 +03:00
if (this.props.format === 'ampm') {
2015-11-10 23:39:47 +03:00
return this.state.displayTime.getHours() % 12 || 12;
2015-09-06 01:12:29 +03:00
} else {
2015-11-10 23:39:47 +03:00
return this.state.displayTime.getHours();
2015-09-06 01:12:29 +03:00
}
}
2015-09-06 01:12:29 +03:00
renderAMPMLabels () {
2016-05-22 14:25:56 +03:00
const { theme } = this.props;
2015-09-06 01:12:29 +03:00
if (this.props.format === 'ampm') {
2015-10-11 22:17:12 +03:00
return (
2016-05-22 14:25:56 +03:00
<div className={theme.ampm}>
<span className={theme.am} onClick={this.toggleTimeMode}>AM</span>
<span className={theme.pm} onClick={this.toggleTimeMode}>PM</span>
2015-10-11 22:17:12 +03:00
</div>
);
2015-09-06 01:12:29 +03:00
}
}
2015-09-06 01:12:29 +03:00
render () {
2016-05-22 14:25:56 +03:00
const { theme } = this.props;
const display = `${this.state.display}Display`;
const format = `${time.getTimeMode(this.state.displayTime)}Format`;
const className = classnames([theme.dialog, theme[display], theme[format]], this.props.className);
2015-09-06 01:12:29 +03:00
return (
2015-11-10 23:39:47 +03:00
<Dialog active={this.props.active} className={className} actions={this.actions}>
2016-05-22 14:25:56 +03:00
<header className={theme.header}>
<span className={theme.hours} onClick={this.switchDisplay.bind(this, 'hours')}>
{('0' + this.formatHours()).slice(-2)}
2015-09-06 01:12:29 +03:00
</span>
2016-05-22 14:25:56 +03:00
<span className={theme.separator}>:</span>
<span className={theme.minutes} onClick={this.switchDisplay.bind(this, 'minutes')}>
{('0' + this.state.displayTime.getMinutes()).slice(-2)}
2015-09-06 01:12:29 +03:00
</span>
{this.renderAMPMLabels()}
2015-09-06 01:12:29 +03:00
</header>
<Clock
ref='clock'
2015-09-06 01:12:29 +03:00
display={this.state.display}
format={this.props.format}
2015-10-22 02:31:17 +03:00
onChange={this.handleClockChange}
onHandMoved={this.handleHandMoved}
2016-05-22 14:25:56 +03:00
theme={this.props.theme}
time={this.state.displayTime}
2015-10-11 22:17:12 +03:00
/>
2015-09-06 01:12:29 +03:00
</Dialog>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default TimePickerDialog;