forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatepickerControl.jsx
More file actions
127 lines (119 loc) · 3.71 KB
/
DatepickerControl.jsx
File metadata and controls
127 lines (119 loc) · 3.71 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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,
};
const isTimePicker = type === DATEPICKER_TYPES.time;
let describedByIds;
if (isTimePicker) {
const ids = [`${controlName}-timehint`];
if (helpText) {
ids.push(`${controlName}-helptext`);
}
describedByIds = ids.filter(Boolean).join(' ') || undefined;
} else if (helpText) {
describedByIds = `${controlName}-helptext`;
}
return (
<Form.Group controlId={controlName} 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
id={controlName}
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}
aria-describedby={describedByIds}
onChange={(date) => {
if (isValidDate(date)) {
onChange(convertToStringFromDate(date));
}
}}
/>
</div>
{isTimePicker && (
<Form.Text id={`${controlName}-timehint`} className="sr-only">
{intl.formatMessage(messages.timepickerScreenreaderHint, {
timeFormat: inputFormat[type].toLocaleUpperCase(),
})}
</Form.Text>
)}
{helpText && (
<Form.Control.Feedback id={`${controlName}-helptext`}>
{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;