import React, { Component } from 'react';
import { DatePicker, Radio } from 'antd';
import { MILLISECOND_IN_TIME_UNIT } from 'utils/constants';
import { timeFormatStr } from 'utils/time';
import styles from './index.less';
const { RangePicker } = DatePicker;
const { h, d, w } = MILLISECOND_IN_TIME_UNIT;
export default class index extends Component {
constructor(props) {
super(props);
const { defaultValue } = props;
this.state = {
defaultValue: defaultValue !== undefined ? defaultValue : h,
value: null,
start: null,
end: null,
};
}
componentDidMount() {
const { defaultValue, value } = this.state;
this.onChangeType(value || defaultValue);
}
get options() {
return [
{ label: t('All'), value: 0 },
{ label: t('In the last hour'), value: h },
{ label: t('Recently a day'), value: d },
{ label: t('In the last 7 days'), value: w },
{ label: t('In the last 30 days'), value: 30 * d },
{ label: t('Custom'), value: 1 },
];
}
onChangeType = (value) => {
const newValue = {
value,
};
if (value !== 1) {
newValue.start = undefined;
newValue.end = undefined;
}
this.setState(
{
...newValue,
},
() => {
this.onChange(newValue);
}
);
};
onChange = (body) => {
const { onChange } = this.props;
onChange &&
onChange({
...this.state,
...body,
});
};
onDateChange = (date) => {
const newValue = {
start: date ? date[0] : null,
end: date ? date[1] : null,
};
this.setState(
{
...newValue,
},
() => {
this.onChange(newValue);
}
);
};
render() {
const { defaultValue, value } = this.state;
return (
<div className={styles.wrapper}>
<Radio.Group
defaultValue={defaultValue}
value={value}
options={this.options}
buttonStyle="solid"
optionType="button"
onChange={(e) => this.onChangeType(e.target.value)}
/>
{value === 1 && (
<RangePicker
onCalendarChange={this.onDateChange}
format={timeFormatStr.YMDHm}
showTime
/>
)}
</div>
);
}
}