react-toolbox/components/date_picker/CalendarDay.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
2016-05-21 21:52:48 +03:00
import classnames from 'classnames';
2017-01-26 20:05:32 +03:00
import time from '../utils/time';
2015-09-09 23:34:56 +03:00
class Day extends Component {
static propTypes = {
day: PropTypes.number,
disabled: PropTypes.bool,
onClick: PropTypes.func,
2017-01-26 20:05:32 +03:00
selectedDate: PropTypes.instanceOf(Date),
sundayFirstDayOfWeek: PropTypes.bool,
theme: PropTypes.shape({
active: PropTypes.string,
day: PropTypes.string,
2017-01-26 20:05:32 +03:00
disabled: PropTypes.string,
2016-05-21 21:52:48 +03:00
}),
2017-01-26 20:05:32 +03:00
viewDate: PropTypes.instanceOf(Date),
};
2015-09-07 02:41:59 +03:00
2017-01-26 20:05:32 +03:00
dayStyle() {
2015-09-07 02:41:59 +03:00
if (this.props.day === 1) {
const e = (this.props.sundayFirstDayOfWeek) ? 0 : 1;
const firstDay = time.getFirstWeekDay(this.props.viewDate) - e;
2015-09-07 02:41:59 +03:00
return {
2017-01-26 20:05:32 +03:00
marginLeft: `${(firstDay >= 0 ? firstDay : 6) * (100 / 7)}%`,
2015-09-07 02:41:59 +03:00
};
}
2017-01-26 20:05:32 +03:00
return undefined;
}
2015-09-07 02:41:59 +03:00
2017-01-26 20:05:32 +03:00
isSelected() {
2015-09-07 02:41:59 +03:00
const sameYear = this.props.viewDate.getFullYear() === this.props.selectedDate.getFullYear();
const sameMonth = this.props.viewDate.getMonth() === this.props.selectedDate.getMonth();
const sameDay = this.props.day === this.props.selectedDate.getDate();
return sameYear && sameMonth && sameDay;
}
2015-09-07 02:41:59 +03:00
handleClick = () => {
if (!this.props.disabled && this.props.onClick) {
this.props.onClick(this.props.day);
}
};
2017-01-26 20:05:32 +03:00
render() {
2016-05-21 21:52:48 +03:00
const className = classnames(this.props.theme.day, {
[this.props.theme.active]: this.isSelected(),
2017-01-26 20:05:32 +03:00
[this.props.theme.disabled]: this.props.disabled,
2015-11-28 15:55:25 +03:00
});
2015-11-16 11:56:05 +03:00
2015-09-07 02:41:59 +03:00
return (
2017-01-26 20:05:32 +03:00
<div data-react-toolbox="day" className={className} style={this.dayStyle()}>
<span onClick={this.handleClick}>
{this.props.day}
</span>
2015-09-07 02:41:59 +03:00
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Day;