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 ( {label} {showUTC && ( ({intl.formatMessage(messages.datepickerUTC)}) )}
{type === DATEPICKER_TYPES.date && !readonly && ( )} { if (isValidDate(date)) { onChange(convertToStringFromDate(date)); } }} />
{helpText && {helpText}}
); }; 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;