react-toolbox/components/time_picker/TimePickerDialog.js

130 lines
3.8 KiB
JavaScript
Raw Normal View History

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