react-toolbox/components/time_picker/dialog.jsx

106 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
className: React.PropTypes.string,
initialTime: React.PropTypes.object,
format: React.PropTypes.oneOf(['24hr', 'ampm']),
2015-11-10 17:07:14 +03:00
onCancel: React.PropTypes.func,
onSelect: React.PropTypes.func
};
2015-09-06 01:12:29 +03:00
static defaultProps = {
2015-11-10 17:07:14 +03:00
active: false,
2015-11-05 13:28:42 +03:00
className: '',
initialTime: new Date(),
format: '24hr'
};
2015-09-06 01:12:29 +03:00
state = {
display: 'hours',
time: this.props.initialTime,
actions: [
2015-11-10 17:07:14 +03:00
{ label: 'Cancel', className: style.button, onClick: this.handleCancel.bind(this) },
{ label: 'Ok', className: style.button, onClick: this.handleSelect.bind(this) }
]
};
2015-09-06 01:12:29 +03:00
2015-11-10 17:07:14 +03:00
handleClockChange = (value) => {
this.setState({time: value});
2015-10-22 02:31:17 +03:00
};
displayMinutes = () => {
this.setState({display: 'minutes'});
};
displayHours = () => {
this.setState({display: 'hours'});
};
toggleTimeMode = () => {
this.refs.clock.toggleTimeMode();
};
2015-09-06 01:12:29 +03:00
2015-11-10 17:07:14 +03:00
handleCancel () {
if (this.props.onCancel) this.props.onCancel();
}
2015-09-06 01:12:29 +03:00
2015-11-10 17:07:14 +03:00
handleSelect () {
if (this.props.onSelect) this.props.onSelect(this.state.time);
}
2015-09-06 01:12:29 +03:00
formatHours () {
2015-09-06 01:12:29 +03:00
if (this.props.format === 'ampm') {
return this.state.time.getHours() % 12 || 12;
} else {
return this.state.time.getHours();
}
}
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}`;
const format = `format-${time.getTimeMode(this.state.time)}`;
const className = `${style.dialog} ${style[display]} ${style[format]}`;
2015-09-06 01:12:29 +03:00
return (
2015-11-10 17:07:14 +03:00
<Dialog className={className} active={this.props.active} type="custom" actions={this.state.actions}>
2015-10-11 22:17:12 +03:00
<header className={style.header}>
<span className={style.hours} onClick={this.displayHours}>
{ ('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>
<span className={style.minutes} onClick={this.displayMinutes}>
{ ('0' + this.state.time.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}
initialTime={this.props.initialTime}
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;