react-toolbox/components/time_picker/dialog.jsx

102 lines
2.8 KiB
React
Raw Normal View History

import React from 'react';
2015-10-11 22:17:12 +03:00
import style from './style';
2015-09-09 23:34:56 +03:00
import time from '../utils/time';
2015-10-20 18:34:40 +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,
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,
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
2015-11-10 23:39:47 +03:00
componentWillUpdate (nextProps) {
if (!this.props.active && nextProps.active) {
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
};
2015-11-10 23:39:47 +03:00
handleSelect = () => {
this.props.onSelect(this.state.displayTime);
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
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 = [
{ label: 'Cancel', className: style.button, onClick: this.props.onDismiss },
{ label: 'Ok', className: style.button, onClick: this.handleSelect }
];
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 () {
if (this.props.format === 'ampm') {
2015-10-11 22:17:12 +03:00
return (
<div className={style.ampm}>
<span className={style.am} onClick={this.toggleTimeMode}>AM</span>
<span className={style.pm} onClick={this.toggleTimeMode}>PM</span>
</div>
);
2015-09-06 01:12:29 +03:00
}
}
2015-09-06 01:12:29 +03:00
render () {
2015-10-11 22:17:12 +03:00
const display = `display-${this.state.display}`;
2015-11-10 23:39:47 +03:00
const format = `format-${time.getTimeMode(this.state.displayTime)}`;
2015-10-11 22:17:12 +03:00
const className = `${style.dialog} ${style[display]} ${style[format]}`;
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}>
2015-10-11 22:17:12 +03:00
<header className={style.header}>
2015-11-10 23:39:47 +03:00
<span className={style.hours} onClick={this.switchDisplay.bind(this, 'hours')}>
{ ('0' + this.formatHours()).slice(-2) }
2015-09-06 01:12:29 +03:00
</span>
2015-10-11 22:17:12 +03:00
<span className={style.separator}>:</span>
2015-11-10 23:39:47 +03:00
<span className={style.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() }
</header>
<Clock
ref='clock'
2015-09-06 01:12:29 +03:00
display={this.state.display}
format={this.props.format}
2015-11-10 23:39:47 +03:00
time={this.state.displayTime}
2015-10-22 02:31:17 +03:00
onChange={this.handleClockChange}
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;