index.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React from 'react'
  2. import style from './index.less'
  3. import PropTypes from 'prop-types'
  4. import { YearList, MonthList } from './SelectList'
  5. import Content from './Content'
  6. import Time from './Time'
  7. /**
  8. * handleChange: 时间改变时会触发的方法(在点击具体日期和输入改变时分秒时触发)
  9. * 该方法会传入一个对象如{year:2018,month:1,day:1,hour:1,minute:1,second:1}
  10. * needTime:是否显示下方时间输入控件 不显示则默认的时分秒为0
  11. */
  12. class Calendar extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. const date = new Date();
  16. this.year = date.getFullYear();
  17. this.month = date.getMonth() + 1;
  18. this.day = date.getDate();
  19. this.inputing = false;
  20. this.state = {
  21. year: this.year,
  22. month: this.month,
  23. day: this.day,
  24. select: {
  25. year: this.year,
  26. month: this.month,
  27. day: 0,
  28. hour:0,
  29. minute:0,
  30. second:0
  31. }
  32. }
  33. }
  34. componentDidMount(){
  35. this.setState({
  36. year: this.props.timeLis && this.props.timeLis.year || this.year,
  37. month: this.props.timeLis && this.props.timeLis.month || this.month,
  38. day: this.props.timeLis && this.props.timeLis.day || this.day,
  39. select: {
  40. year: this.props.timeLis && this.props.timeLis.year || this.year,
  41. month: this.props.timeLis && this.props.timeLis.month || this.month,
  42. day: this.props.timeLis && this.props.timeLis.day || this.day,//这里设置初始选中的值
  43. hour: this.props.timeLis && this.props.timeLis.hour,
  44. minute: this.props.timeLis && this.props.timeLis.minute,
  45. second: this.props.timeLis && this.props.timeLis.second
  46. }
  47. })
  48. }
  49. handleYearSelect(year) {
  50. if (year === this.year && this.state.month > this.month) {
  51. this.setState({
  52. year: year,
  53. month: this.month
  54. });
  55. } else {
  56. this.setState({
  57. year: year
  58. });
  59. }
  60. }
  61. handleMonthSelect(month) {
  62. this.setState({
  63. month: month
  64. });
  65. }
  66. handleChange(info) {
  67. if (this.inputing) {
  68. return;
  69. }
  70. this.inputing = true;
  71. info.year = this.state.year;
  72. info.month = this.state.month;
  73. // info.day = this.state.day;//没选日就加上默认值
  74. if (info.hour == null) {
  75. info.hour = this.state.select.hour;
  76. info.minute = this.state.select.minute;
  77. info.second = this.state.select.second;
  78. }
  79. this.props.handleChange(Object.assign({}, info));
  80. this.setState({
  81. select: info
  82. });
  83. }
  84. timeSure() {
  85. if (this.inputing) {
  86. return;
  87. }
  88. this.inputing = true;
  89. let info = this.state.select
  90. info.year = this.state.year;
  91. info.month = this.state.month;
  92. // info.day = this.state.day;//没选日就加上默认值
  93. if (info.hour == null) {
  94. info.hour = this.state.select.hour;
  95. info.minute = this.state.select.minute;
  96. info.second = this.state.select.second;
  97. }
  98. this.props.timeSure(Object.assign({}, info))
  99. this.setState({
  100. select: info
  101. });
  102. }
  103. handleTodayClick() {
  104. const info = {
  105. year: this.year,
  106. month: this.month,
  107. day: this.day,
  108. hour: this.state.select.hour,
  109. minute: this.state.select.minute,
  110. second: this.state.select.second
  111. };
  112. this.setState({
  113. select: info
  114. });
  115. // this.props.timeSure(Object.assign({}, info))
  116. this.props.handleChange(info);
  117. }
  118. genTimeComponent() {
  119. return this.props.needTime ? <Time hour={this.state.select.hour} minute={this.state.select.minute} second={this.state.select.second} day={this.state.select.day} handleChange={(info) => { this.handleChange(info) }} /> : null;
  120. }
  121. render() {
  122. return (
  123. <div className={style.wrapper} id="calendarDate" style={{ top: this.props.top }}>
  124. <div className={style.top}>
  125. <div className={style.year}>
  126. <YearList select={this.state.year} canSelectFuture={this.props.canSelectFuture} handleChange={(info) => this.handleYearSelect(info)} />
  127. </div>
  128. <div className={style.month}>
  129. <MonthList year={this.state.year} canSelectFuture={this.props.canSelectFuture} select={this.state.month} handleChange={(info) => this.handleMonthSelect(info)} />
  130. </div>
  131. <div className={style.button}>
  132. <button onClick={() => this.handleTodayClick()}>{'今天'}</button>
  133. </div>
  134. </div>
  135. <Content selectTime={this.state.select} year={this.state.year} month={this.state.month} day={this.state.day} handleClick={(info) => this.handleChange(info)} canSelectFuture={this.props.canSelectFuture} />
  136. {this.genTimeComponent()}
  137. {
  138. this.props.sure ? <div className={style.timeSure} onClick={() => this.timeSure({})}>确定</div> : ''
  139. }
  140. </div>
  141. )
  142. }
  143. componentDidUpdate() {
  144. this.inputing = false;
  145. }
  146. }
  147. export default Calendar;
  148. Calendar.propTypes = {
  149. handleChange: PropTypes.func,
  150. needTime: PropTypes.bool,
  151. canSelectFuture: PropTypes.bool
  152. };