king
2024-02-03 131bc212f0cfe964c9a38bbe6178aca4f4a16677
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { DatePicker } from 'antd'
import moment from 'moment'
 
const { MonthPicker, WeekPicker, RangePicker } = DatePicker
 
/**
 * @description 自定义时间选择器
 */
class MkDatePicker extends Component {
  constructor(props) {
    super(props)
 
    const config = props.config
 
    let mode = 'date'
    let format = config.format || 'YYYY-MM-DD'
 
    if (config.type === 'datemonth') {
      mode = 'month'
    } else if (config.type === 'week') {
      mode = 'week'
    } else if (config.type === 'daterange') {
      mode = 'daterange'
    }
 
    let value = config.initval || null
 
    if (mode === 'daterange') {
      if (value) {
        let val = value.split(',')
        value = [moment(val[0], format), moment(val[1], format)]
      } else {
        value = [null, null]
      }
    } else if (value) {
      value = moment(value, format)
    }
 
    this.state = {
      value,
      mode,
      precision: config.precision || 'day',
      format
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { config } = this.props
 
    if (config.checkShift && nextProps.config.initval && nextProps.config.initval !== config.initval) {
      let val = nextProps.config.initval.split(',')
 
      this.setState({
        value: [moment(val[0], config.format), moment(val[1], config.format)]
      })
 
      this.props.onChange(nextProps.config.initval, true)
    }
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  onChange = (val) => {
    const { format, mode } = this.state
 
    this.setState({value: val})
 
    if (mode === 'daterange') {
      let _val = val
      if (_val && !_val[0]) {
        _val = ''
      }
      this.props.onChange(_val ? `${moment(_val[0]).format(format)},${moment(_val[1]).format(format)}` : '', false)
    } else {
      this.props.onChange(val ? moment(val).format(format) : '', false)
    }
  }
 
  render() {
    const { value, mode, format, precision } = this.state
 
    if (mode === 'date') {
      return <DatePicker dropdownClassName={'mk-date-picker ' + precision} value={value} showTime={format !== 'YYYY-MM-DD'} format={format} onChange={this.onChange}/>
    } else if (mode === 'month') {
      return <MonthPicker value={value} onChange={this.onChange}/>
    } else if (mode === 'week') {
      return <WeekPicker value={value} onChange={this.onChange}/>
    } else if (mode === 'daterange') {
      return <RangePicker ranges={format === 'YYYY-MM-DD' ? {
        '今天': [moment(), moment()],
        '当月': [moment().startOf('month'), moment().endOf('month')],
        '上月': [moment().subtract(1, 'months').startOf('month'), moment().subtract(1, 'months').endOf('month')],
      } : null} dropdownClassName={'mk-date-picker ' + precision} placeholder={['开始日期', '结束日期']} showTime={format !== 'YYYY-MM-DD'} format={format} value={value} onChange={this.onChange}/>
    }
  }
}
 
export default MkDatePicker