react-toolbox/components/time_picker/ClockFace.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-01-26 20:05:32 +03:00
/* eslint-disable no-mixed-operators */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-05-22 14:25:56 +03:00
import classnames from 'classnames';
2015-09-09 23:34:56 +03:00
2016-05-31 11:23:05 +03:00
class Face extends Component {
static propTypes = {
2016-05-31 11:23:05 +03:00
active: PropTypes.number,
2017-01-26 20:05:32 +03:00
numbers: PropTypes.arrayOf(PropTypes.number),
2016-05-31 11:23:05 +03:00
radius: PropTypes.number,
spacing: PropTypes.number,
theme: PropTypes.shape({
active: PropTypes.string,
face: PropTypes.string,
2017-01-26 20:05:32 +03:00
number: PropTypes.string,
2016-05-22 14:25:56 +03:00
}),
2017-01-26 20:05:32 +03:00
twoDigits: PropTypes.bool,
};
static defaultProps = {
active: null,
numbers: [],
radius: 0,
2017-01-26 20:05:32 +03:00
twoDigits: false,
};
2015-09-06 20:29:51 +03:00
2017-01-26 20:05:32 +03:00
numberStyle(rad, num) {
2015-09-06 20:29:51 +03:00
return {
position: 'absolute',
left: (rad + rad * Math.sin(360 * (Math.PI / 180) / 12 * (num - 1)) + this.props.spacing),
2017-01-26 20:05:32 +03:00
top: (rad - rad * Math.cos(360 * (Math.PI / 180) / 12 * (num - 1)) + this.props.spacing),
2015-09-06 20:29:51 +03:00
};
}
2015-09-06 20:29:51 +03:00
2017-01-26 20:05:32 +03:00
faceStyle() {
2015-09-06 20:29:51 +03:00
return {
height: this.props.radius * 2,
2017-01-26 20:05:32 +03:00
width: this.props.radius * 2,
2015-09-06 20:29:51 +03:00
};
}
2015-09-06 20:29:51 +03:00
renderNumber = (number, idx) => {
2016-05-22 14:25:56 +03:00
const { active, radius, spacing, theme, twoDigits } = this.props;
return (
2015-10-11 22:17:12 +03:00
<span
2017-01-26 20:05:32 +03:00
className={classnames(theme.number, { [theme.active]: number === active })}
2016-05-22 14:25:56 +03:00
style={this.numberStyle(radius - spacing, idx + 1)}
2015-10-11 22:17:12 +03:00
key={number}
>
2017-01-26 20:05:32 +03:00
{twoDigits ? (`0${number}`).slice(-2) : number}
</span>
);
}
2017-01-26 20:05:32 +03:00
render() {
const { numbers, onTouchStart, onMouseDown, theme } = this.props; // eslint-disable-line
2015-09-06 20:29:51 +03:00
return (
2015-10-11 22:17:12 +03:00
<div
2016-05-22 14:25:56 +03:00
className={theme.face}
onTouchStart={onTouchStart}
onMouseDown={onMouseDown}
2015-10-11 22:17:12 +03:00
style={this.faceStyle()}
>
{numbers.map(this.renderNumber)}
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 Face;