react-toolbox/components/time_picker/dialog.jsx

109 lines
2.8 KiB
React
Raw Normal View History

import React from 'react';
2015-10-21 09:13:24 +03:00
import autobind from 'autobind-decorator'
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-21 09:13:24 +03:00
@autobind
export default class TimePickerDialog extends React.Component {
static propTypes = {
2015-09-06 01:12:29 +03:00
className: React.PropTypes.string,
initialTime: React.PropTypes.object,
format: React.PropTypes.oneOf(['24hr', 'ampm']),
onTimeSelected: React.PropTypes.func
};
2015-09-06 01:12:29 +03:00
static defaultProps = {
2015-09-06 01:12:29 +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-10-21 09:13:24 +03:00
{ label: 'Cancel', className: style.button, onClick: this.onTimeCancel },
{ label: 'Ok', className: style.button, onClick: this.onTimeSelected }
]
};
2015-09-06 01:12:29 +03:00
2015-09-06 23:11:39 +03:00
onClockChange (newTime) {
this.setState({time: newTime});
}
2015-09-06 01:12:29 +03:00
onTimeCancel () {
this.refs.dialog.hide();
}
2015-09-06 01:12:29 +03:00
onTimeSelected () {
if (this.props.onTimeSelected) this.props.onTimeSelected(this.state.time);
this.refs.dialog.hide();
}
2015-09-06 01:12:29 +03:00
displayMinutes () {
this.setState({display: 'minutes'});
}
2015-09-06 01:12:29 +03:00
displayHours () {
this.setState({display: 'hours'});
}
2015-09-06 01:12:29 +03:00
toggleTimeMode () {
this.refs.clock.toggleTimeMode();
}
2015-09-06 01:12:29 +03:00
show () {
this.refs.dialog.show();
setTimeout(this.refs.clock.calculateShape, 1000);
}
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-10-11 22:17:12 +03:00
<Dialog ref="dialog" className={className} type="custom" actions={this.state.actions}>
<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"
display={this.state.display}
format={this.props.format}
initialTime={this.props.initialTime}
2015-10-21 09:13:24 +03:00
onChange={this.onClockChange}
2015-10-11 22:17:12 +03:00
/>
2015-09-06 01:12:29 +03:00
</Dialog>
);
}
};