DatePicker.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import Styles from './picker.css'
  4. import { getDateFormatFromSepecificDate } from '../../utils'
  5. import Modal from '../modal/Modal'
  6. import { CHINESE_MODEL, WESTERN_MODEL, _ } from '../../const'
  7. import { DateContext, initialData } from '../../context'
  8. import { setSelectedDays, getDaysOfMonth, getWeekSort } from '../../helper'
  9. class DatePicker extends Component {
  10. constructor(props) {
  11. super(props)
  12. const { defaultDate } = this.props
  13. this.state = {
  14. value: defaultDate,
  15. showModal: false,
  16. ...initialData,
  17. }
  18. this.onModalOpen = this.onModalOpen.bind(this)
  19. this.onInputChange = this.onInputChange.bind(this)
  20. this.onInputClear = this.onInputClear.bind(this)
  21. this.onModalClose = this.onModalClose.bind(this)
  22. this.onChangeModel = this.onChangeModel.bind(this)
  23. this.onSelectDay = this.onSelectDay.bind(this)
  24. }
  25. onModalOpen() {
  26. this.setState({ showModal: true })
  27. }
  28. onModalClose() {
  29. this.setState({ showModal: false })
  30. }
  31. onInputChange(event) {
  32. this.setState({ value: event.target.value })
  33. }
  34. onInputClear() {
  35. this.setState({ value: '', showModal: false })
  36. }
  37. onChangeModel(model) {
  38. const { value } = this.state
  39. let nextModel = model
  40. if (model === CHINESE_MODEL) {
  41. nextModel = WESTERN_MODEL
  42. } else {
  43. nextModel = CHINESE_MODEL
  44. }
  45. const weekTags = getWeekSort(nextModel)
  46. const changeModelDays = getDaysOfMonth(_, _, nextModel)
  47. const afterSetDays = setSelectedDays(changeModelDays, value)
  48. this.setState({
  49. model: nextModel,
  50. weekTags: weekTags,
  51. days: afterSetDays,
  52. })
  53. }
  54. onSelectDay(day) {
  55. const { days } = this.state
  56. const afterSetDays = setSelectedDays(days, day.full)
  57. this.setState({ value: day.full, days: afterSetDays }, () => {
  58. // this.onModalClose()
  59. })
  60. }
  61. render() {
  62. const { inline } = this.props
  63. const { value, showModal } = this.state
  64. return (
  65. <div className={Styles.wrapper}>
  66. <div
  67. className={Styles.container}
  68. style={inline ? { display: 'inline-block' } : {}}
  69. >
  70. <input
  71. type="text"
  72. placeholder="请选择日期"
  73. className={Styles.input}
  74. value={value}
  75. onChange={e => this.onInputChange(e)}
  76. onFocus={this.onModalOpen}
  77. />
  78. <i className={Styles.calendar} />
  79. <i
  80. className={Styles.close}
  81. onClick={this.onInputClear}
  82. role="presentation"
  83. />
  84. <div className={Styles.line} />
  85. </div>
  86. <DateContext.Provider
  87. value={
  88. {
  89. ...this.state,
  90. onSelectDay: this.onSelectDay,
  91. onChangeModel: this.onChangeModel,
  92. }
  93. }
  94. >
  95. <Modal
  96. isMounted={showModal}
  97. delayTime={200}
  98. onInputChange={this.onInputChange}
  99. onCloseModal={this.onModalClose}
  100. {...this.state}
  101. />
  102. </DateContext.Provider>
  103. </div>
  104. )
  105. }
  106. }
  107. DatePicker.defaultProps = {
  108. inline: false,
  109. defaultDate: getDateFormatFromSepecificDate(),
  110. }
  111. DatePicker.propTypes = {
  112. inline: PropTypes.bool,
  113. defaultDate: PropTypes.string,
  114. }
  115. export default DatePicker