index.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React from 'react'
  2. import styles from './index.less'
  3. import PropTypes from 'prop-types'
  4. import Item from '../ContentItem'
  5. class Content extends React.Component {
  6. constructor() {
  7. super();
  8. this.state= {
  9. now: new Date().getTime()
  10. }
  11. }
  12. genItems(){
  13. let is_now_month=this.props.selectTime.year===this.props.year && this.props.selectTime.month===this.props.month;
  14. const Items=[];
  15. const first_day=new Date(this.props.year,this.props.month-1,1).getDay();
  16. let key=0;
  17. if(first_day!==7){
  18. let pre_day=new Date(this.props.year,this.props.month-1,0).getDate()-first_day+1;
  19. for(let i=0;i<first_day;i++){
  20. Items.push(<Item value={pre_day++} key={key++} clazzKey={'gray'} handleClick={this.props.handleClick}/>);
  21. }
  22. }
  23. const last_date=new Date(this.props.year,this.props.month,0);
  24. const day = last_date.getDate();
  25. for(let i=1,len=day;i<=len;i++){
  26. const currentTime = new Date(this.props.year,this.props.month-1,i);
  27. const exceed = +currentTime > this.state.now&&!this.props.canSelectFuture;
  28. let itemClassName = "";
  29. if(exceed) {
  30. itemClassName = "gray";
  31. }else{
  32. itemClassName = (is_now_month && i===this.props.selectTime.day)?'select':'';
  33. }
  34. Items.push(<Item value={i} key={key++} handleClick={this.props.handleClick} clazzKey={itemClassName}/>);
  35. }
  36. if(last_date.getDay()!==6){
  37. let next_month_first_day=new Date(this.props.year,this.props.month,1).getDay();
  38. for(let i=1,len=7-next_month_first_day;i<=len;i++){
  39. Items.push(<Item value={i} key={key++} clazzKey={'gray'} handleClick={this.props.handleClick}/>);
  40. }
  41. }
  42. return Items;
  43. }
  44. render() {
  45. return (
  46. <div className={styles.wrapper}>
  47. <ol className={styles.week}>
  48. <li>日</li>
  49. <li>一</li>
  50. <li>二</li>
  51. <li>三</li>
  52. <li>四</li>
  53. <li>五</li>
  54. <li>六</li>
  55. </ol>
  56. <ol className={styles.day}>
  57. {this.genItems()}
  58. </ol>
  59. </div>
  60. )
  61. }
  62. }
  63. export default Content;
  64. Content.propTypes={
  65. year:PropTypes.number,
  66. month:PropTypes.number,
  67. selectTime:PropTypes.object,
  68. handleClick:PropTypes.func,
  69. canSelectFuture:PropTypes.bool
  70. };