-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathDatepickerControl.jsx
More file actions
102 lines (95 loc) · 2.93 KB
/
DatepickerControl.jsx
File metadata and controls
102 lines (95 loc) · 2.93 KB
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
import React from 'react';
import DatePicker from 'react-datepicker';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Form, Icon } from '@openedx/paragon';
import { Calendar } from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
import { convertToDateFromString, convertToStringFromDate, isValidDate } from '../../utils';
import { DATE_FORMAT, TIME_FORMAT } from '../../constants';
import messages from './messages';
export const DATEPICKER_TYPES = {
date: 'date',
time: 'time',
};
const DatepickerControl = ({
type,
label,
value,
showUTC,
readonly,
helpText,
isInvalid,
controlName,
onChange,
}) => {
const intl = useIntl();
const formattedDate = convertToDateFromString(value);
const inputFormat = {
[DATEPICKER_TYPES.date]: DATE_FORMAT,
[DATEPICKER_TYPES.time]: TIME_FORMAT,
};
return (
<Form.Group className="form-group-custom datepicker-custom">
<Form.Label className="d-flex justify-content-between">
{label}
{showUTC && (
<span className="h6 font-weight-normal text-gray-500 mb-0">
({intl.formatMessage(messages.datepickerUTC)})
</span>
)}
</Form.Label>
<div className="position-relative">
{type === DATEPICKER_TYPES.date && !readonly && (
<Icon
src={Calendar}
className="datepicker-custom-control-icon"
alt={intl.formatMessage(messages.calendarAltText)}
/>
)}
<DatePicker
name={controlName}
selected={formattedDate}
disabled={readonly}
dateFormat={inputFormat[type]}
timeFormat={inputFormat[type]}
className={classNames('datepicker-custom-control', {
'datepicker-custom-control_readonly': readonly,
'datepicker-custom-control_isInvalid': isInvalid,
})}
autoComplete="off"
selectsStart
showTimeSelect={type === DATEPICKER_TYPES.time}
showTimeSelectOnly={type === DATEPICKER_TYPES.time}
placeholderText={inputFormat[type].toLocaleUpperCase()}
showPopperArrow={false}
onChange={(date) => {
if (isValidDate(date)) {
onChange(convertToStringFromDate(date));
}
}}
/>
</div>
{helpText && <Form.Control.Feedback>{helpText}</Form.Control.Feedback>}
</Form.Group>
);
};
DatepickerControl.defaultProps = {
helpText: '',
showUTC: false,
value: '',
readonly: false,
isInvalid: false,
};
DatepickerControl.propTypes = {
type: PropTypes.oneOf(Object.values(DATEPICKER_TYPES)).isRequired,
value: PropTypes.string,
label: PropTypes.string.isRequired,
showUTC: PropTypes.bool,
helpText: PropTypes.string,
readonly: PropTypes.bool,
isInvalid: PropTypes.bool,
controlName: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
};
export default DatepickerControl;