Files
redash/client/app/components/DateRangeInput.jsx
Levko Kravets b0b4d5e26a Convert Angular services to CommonJS-style and use them in React components instead of injecting (#3331)
* Refine Auth service: remove dead code and fix race condition
* Export services in CommonJS style
* Refine Users, Events and OfflineListener services
* Refactor Notifications service - rewrite to CommonJS
* Replace Angular service injection with imports in React components
* Fix Footer tests
* Events service -> recordEvent function
* CR1
2019-01-24 16:24:58 +02:00

59 lines
1.4 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 DateRangeInput({
value,
onSelect,
className,
}) {
const format = clientConfig.dateFormat || 'YYYY-MM-DD';
const additionalAttributes = {};
if (isArray(value) && value[0].isValid() && value[1].isValid()) {
additionalAttributes.defaultValue = value;
}
return (
<RangePicker
className={className}
{...additionalAttributes}
format={format}
onChange={onSelect}
/>
);
}
DateRangeInput.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.');
}
},
onSelect: PropTypes.func,
className: PropTypes.string,
};
DateRangeInput.defaultProps = {
value: null,
onSelect: () => {},
className: '',
};
export default function init(ngModule) {
ngModule.component('dateRangeInput', react2angular(DateRangeInput));
}
init.init = true;