react-toolbox/components/time_picker/ClockHours.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-05-31 11:23:05 +03:00
import React, { Component, PropTypes } from 'react';
import utils from '../utils/utils.js';
import Hand from './ClockHand.js';
import Face from './ClockFace.js';
2015-09-06 20:29:51 +03:00
const outerNumbers = [0, ...utils.range(13, 24)];
const innerNumbers = [12, ...utils.range(1, 12)];
2015-10-12 04:26:03 +03:00
const innerSpacing = 1.7;
2015-09-06 20:29:51 +03:00
const step = 360 / 12;
2016-05-31 11:23:05 +03:00
class Hours extends Component {
static propTypes = {
2016-05-31 11:23:05 +03:00
center: PropTypes.object,
format: PropTypes.oneOf(['24hr', 'ampm']),
onChange: PropTypes.func,
onHandMoved: PropTypes.func,
radius: PropTypes.number,
selected: PropTypes.number,
spacing: PropTypes.number,
theme: PropTypes.object
};
2015-09-06 20:29:51 +03:00
state = {
inner: this.props.format === '24hr' && this.props.selected > 0 && this.props.selected <= 12
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleHandMove = (degrees, radius) => {
const currentInner = radius < this.props.radius - this.props.spacing * innerSpacing;
2015-09-06 20:29:51 +03:00
if (this.props.format === '24hr' && this.state.inner !== currentInner) {
2015-10-11 22:17:12 +03:00
this.setState({inner: currentInner}, () => {
this.props.onChange(this.valueFromDegrees(degrees));
});
} else {
this.props.onChange(this.valueFromDegrees(degrees));
2015-09-06 20:29:51 +03:00
}
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleMouseDown = (event) => {
2015-09-06 20:29:51 +03:00
this.refs.hand.mouseStart(event);
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
2015-10-22 02:31:17 +03:00
handleTouchStart = (event) => {
2015-09-06 20:29:51 +03:00
this.refs.hand.touchStart(event);
2015-10-22 02:31:17 +03:00
};
2015-09-06 20:29:51 +03:00
valueFromDegrees (degrees) {
2015-09-06 20:29:51 +03:00
if (this.props.format === 'ampm' || this.props.format === '24hr' && this.state.inner) {
return innerNumbers[degrees / step];
2015-09-06 20:29:51 +03:00
} else {
return outerNumbers[degrees / step];
2015-09-06 20:29:51 +03:00
}
}
2015-09-06 20:29:51 +03:00
renderInnerFace (innerRadius) {
if (this.props.format === '24hr') {
return (
<Face
2015-10-22 02:31:17 +03:00
onTouchStart={this.handleTouchStart}
onMouseDown={this.handleMouseDown}
numbers={innerNumbers}
spacing={this.props.spacing}
radius={innerRadius}
theme={this.props.theme}
2015-10-11 22:17:12 +03:00
active={this.props.selected}
/>
);
}
}
2015-09-06 20:29:51 +03:00
render () {
const { format, selected, radius, spacing, center, onHandMoved } = this.props;
const is24hr = format === '24hr';
2015-09-06 20:29:51 +03:00
return (
<div>
<Face
2015-10-22 02:31:17 +03:00
onTouchStart={this.handleTouchStart}
onMouseDown={this.handleMouseDown}
numbers={is24hr ? outerNumbers : innerNumbers}
spacing={spacing}
radius={radius}
twoDigits={is24hr}
2015-10-11 22:17:12 +03:00
active={is24hr ? selected : (selected % 12 || 12)}
theme={this.props.theme}
2015-10-11 22:17:12 +03:00
/>
{this.renderInnerFace(radius - spacing * innerSpacing)}
2015-09-06 20:29:51 +03:00
<Hand ref='hand'
2015-09-08 12:03:01 +03:00
angle={selected * step}
2015-10-12 04:26:03 +03:00
length={(this.state.inner ? radius - spacing * innerSpacing : radius) - spacing}
2015-10-22 02:31:17 +03:00
onMove={this.handleHandMove}
theme={this.props.theme}
2015-09-08 12:03:01 +03:00
onMoved={onHandMoved}
origin={center}
2015-10-11 22:17:12 +03:00
step={step}
/>
2015-09-06 20:29:51 +03:00
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Hours;