react-toolbox/components/time_picker/ClockHours.jsx

98 lines
2.7 KiB
React
Raw Normal View History

import React from 'react';
2015-11-23 00:13:27 +03:00
import utils from '../utils/utils';
import Face from './ClockFace';
import Hand from './ClockHand';
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;
2015-10-22 02:31:17 +03:00
class Hours extends React.Component {
static propTypes = {
center: React.PropTypes.object,
2015-09-06 20:29:51 +03:00
format: React.PropTypes.oneOf(['24hr', 'ampm']),
onChange: React.PropTypes.func,
onHandMoved: React.PropTypes.func,
radius: React.PropTypes.number,
selected: React.PropTypes.number,
spacing: React.PropTypes.number
};
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}
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)}
/>
{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}
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;