king
2021-11-04 4190affb665438b1067af88f09e8557abbbfea2b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { DatePicker } from 'antd'
import moment from 'moment'
 
const { MonthPicker } = DatePicker
 
/**
 * @description 自定义时间选择器
 */
class MkDatePicker extends Component {
  constructor(props) {
    super(props)
 
    const config = props.config
 
    let mode = 'date'
    let format = 'YYYY-MM-DD'
 
    if (config.type === 'datemonth') {
      mode = 'month'
      format = 'YYYY-MM'
    } else if (config.type === 'datetime') {
      mode = 'datetime'
      format = 'YYYY-MM-DD HH:mm:ss'
    }
    let value = config.initval || null
    if (value) {
      value = moment(value, format)
    }
 
    this.state = {
      value,
      minDate: config.minDate ? moment().add(config.minDate, 'days').endOf('day') : '',
      maxDate: config.maxDate ? moment().add(config.maxDate, 'days').endOf('day') : '',
      mode,
      format
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  onChange = (val) => {
    const { format } = this.state
 
    this.props.onChange(val ? moment(val).format(format) : '')
  }
 
  disabledDate = (current) => {
    const { minDate, maxDate } = this.state
 
    if (!current || (!maxDate && !minDate)) {
      return false
    }
    
    if (!maxDate) {
      return current < minDate
    } else if (!minDate) {
      return current > maxDate
    } else {
      return current < minDate || current > maxDate
    }
  }
 
  render() {
    const { config } = this.props
    const { value, mode } = this.state
 
    if (mode === 'date') {
      return <DatePicker defaultValue={value} disabledDate={this.disabledDate} disabled={config.readonly} onChange={this.onChange}/>
    } else if (mode === 'month') {
      return <MonthPicker defaultValue={value} disabled={config.readonly} onChange={this.onChange}/>
    } else if (mode === 'datetime') {
      return <DatePicker defaultValue={value} disabledDate={this.disabledDate} showTime disabled={config.readonly} onChange={this.onChange}/>
    }
  }
}
 
export default MkDatePicker