-
Notifications
You must be signed in to change notification settings - Fork 9
/
DateInputField.react.js
114 lines (101 loc) · 2.92 KB
/
DateInputField.react.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import DateConverter from '@opuscapita/i18n/lib/converters/DateConverter';
import dayjs from '../../dayjs';
import './DateInputField.less';
let propTypes = {
className: PropTypes.string,
dateFormat: PropTypes.string,
disabled: PropTypes.bool,
locale: PropTypes.string,
error: PropTypes.bool,
onChange: PropTypes.func,
onError: PropTypes.func,
onRef: PropTypes.func,
value: PropTypes.object
};
let defaultProps = {
className: '',
dateFormat: 'dd/MM/yyyy',
disabled: false,
locale: 'en',
error: false,
onChange: () => {},
onError: () => {},
onRef: () => {},
value: null
};
export default
class DateInputField extends Component {
constructor(props) {
super(props);
this.state = {
inputValue: props.value ? dayjs(props.value.toISOString()).format(props.dateFormat) : ''
};
}
componentWillReceiveProps(nextProps) {
if (this.props.error || this.props.value !== nextProps.value || this.props.dateFormat !== nextProps.dateFormat) {
let inputValue = nextProps.value ? dayjs(nextProps.value.toISOString()).format(nextProps.dateFormat) : '';
this.setState({ inputValue });
}
}
clear = () => {
this.setState({ inputValue: '' });
};
validate(dateString, dateFormat) {
const i18nCompatibleFormat = dateFormat.replace(/D/g, 'd').replace(/Y/g, 'y');
const dc = new DateConverter(i18nCompatibleFormat);
try {
const date = dc.stringToValue(dateString);
const value = !dateString.length ? null : date;
this.props.onChange(value);
} catch (err) {
this.props.onError(err.message);
}
}
handleInputChange = event => {
let inputValue = event.target.value;
this.validate(inputValue, this.props.dateFormat);
this.setState({ inputValue });
};
handleRef = () => {
this.props.onRef(this);
};
render() {
let {
className,
dateFormat,
disabled,
error, // eslint-disable-line no-unused-vars
locale, // eslint-disable-line no-unused-vars
onChange, // eslint-disable-line no-unused-vars
onError, // eslint-disable-line no-unused-vars
onRef, // eslint-disable-line no-unused-vars
value, // eslint-disable-line no-unused-vars
...restProps
} = this.props;
let {
inputValue
} = this.state;
return (
<div
ref={this.handleRef}
className={`opuscapita_date-input-field form-control ${className}`}
disabled={disabled}
>
<input
ref={ref => { this.dateInputFieldRef = ref }}
type="text"
className={`opuscapita_date-input-field__input`}
value={inputValue}
placeholder={dateFormat}
disabled={disabled}
onChange={this.handleInputChange}
{...restProps}
/>
</div>
);
}
}
DateInputField.propTypes = propTypes;
DateInputField.defaultProps = defaultProps;