UNPKG

cactus-daterangepicker

Version:

date-range-picker

34 lines (31 loc) 956 B
/** * 日期范围选择器 * 组件支持返回 string 值 无需手动从 moment 对象转 string */ import React, { Component } from 'react'; import { DatePicker } from 'antd'; import moment from 'moment'; export default class DateRange extends Component { render() { const { ...props } = this.props; const { value, onChange, format } = props; // 将string格式处理成moment格式 还给原组件 if (Array.isArray(value) && value.length > 0) { props.value = [ value[0] ? moment(value[0], format) : null, value[1] ? moment(value[1], format) : null, ]; } // 将返回值moment格式处理成string格式 props.onChange = (date) => { if (Array.isArray(date)) { date = date.map(item => item.format(format)); } if (typeof onChange === 'function') { onChange(date); } return date; }; return (<DatePicker.RangePicker {...props} />); } }