-
Notifications
You must be signed in to change notification settings - Fork 302
/
Range.js
96 lines (81 loc) · 2.31 KB
/
Range.js
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
import React, { Component } from 'react'
import classnames from 'classnames'
import PropTypes from '../utils/proptypes'
import Datepicker from './Datetime'
import PureRender from '../mixins/PureRender'
import FormItem from '../higherOrders/FormItem'
import { compose } from '../utils/compose'
import { format } from '../utils/strings'
import { getLang } from '../lang'
import _datepickers from '../styles/_datepicker.scss'
class Range extends Component {
constructor (props) {
super(props)
this.firstChange = this.firstChange.bind(this)
this.secondChange = this.secondChange.bind(this)
this.handleClear = this.handleClear.bind(this)
}
handleChange (val, index) {
let values = [...this.props.value]
values[index] = val
this.props.onChange(values)
}
firstChange (value) {
this.handleChange(value, 0)
}
secondChange (value) {
this.handleChange(value, 1)
}
handleClear () {
this.props.onChange([])
}
render () {
const { className, value, min, max, con, hasError, ...other } = this.props
const type = other.type.replace('-range', '')
return (
<div className={classnames(_datepickers.range, className, hasError && _datepickers.danger)}>
<Datepicker min={min} {...other}
max={value[1]}
value={value[0]}
type={type}
onChange={this.firstChange}
/>
{con}
<Datepicker max={max} {...other}
min={value[0]}
value={value[1]}
onClear={this.handleClear}
type={type}
onChange={this.secondChange}
/>
</div>
)
}
}
Range.isFormItem = true
Range.propTypes = {
className: PropTypes.string,
con: PropTypes.any,
hasError: PropTypes.bool,
max: PropTypes.datetime,
min: PropTypes.datetime,
names: PropTypes.arrayOf(PropTypes.string),
onChange: PropTypes.func,
value: PropTypes.array
}
Range.defaultProps = {
con: '-',
names: [],
type: 'datetime',
value: []
}
function validate (value = [], args) {
if (!args.required) return true
let result = (value[0] && value[1]) ? true
: new Error(format(getLang('validation.tips.required'), args.label || ''))
return result
}
export default compose(
FormItem.register(['datetime-range', 'date-range', 'time-range'], { valueType: 'datetime', validate }),
PureRender()
)(Range)