Files
redash/client/app/components/DateTimeRangeInput.jsx
2019-01-31 11:21:45 +02:00

64 lines
1.6 KiB
JavaScript

import moment from 'moment';
import { isArray } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
import DatePicker from 'antd/lib/date-picker';
import { clientConfig } from '@/services/auth';
const { RangePicker } = DatePicker;
export function DateTimeRangeInput({
value,
withSeconds,
onSelect,
className,
}) {
const format = (clientConfig.dateFormat || 'YYYY-MM-DD') +
(withSeconds ? ' HH:mm:ss' : ' HH:mm');
const additionalAttributes = {};
if (isArray(value) && value[0].isValid() && value[1].isValid()) {
additionalAttributes.defaultValue = value;
}
return (
<RangePicker
className={className}
showTime
{...additionalAttributes}
format={format}
onChange={onSelect}
/>
);
}
DateTimeRangeInput.propTypes = {
value: (props, propName, componentName) => {
const value = props[propName];
if (
(value !== null) && !(
isArray(value) && (value.length === 2) &&
moment.isMoment(value[0]) && moment.isMoment(value[1])
)
) {
return new Error('Prop `' + propName + '` supplied to `' + componentName +
'` should be an array of two Moment.js instances.');
}
},
withSeconds: PropTypes.bool,
onSelect: PropTypes.func,
className: PropTypes.string,
};
DateTimeRangeInput.defaultProps = {
value: null,
withSeconds: false,
onSelect: () => {},
className: '',
};
export default function init(ngModule) {
ngModule.component('dateTimeRangeInput', react2angular(DateTimeRangeInput));
}
init.init = true;