import React from 'react'
import { observable } from 'mobx'
import moment from 'moment'
import { getLocale } from '@gm-pc/locales'
import { QuickSelectListOptions } from './types'
import DateRangePicker from './date_range_picker'
const store = observable({
begin: new Date(),
end: new Date(),
changeDate(begin: Date | null, end: Date | null) {
console.log('begin: ', begin)
console.log('end: ', end)
this.begin = begin as any
this.end = end as any
},
})
const _store = {
begin: null,
end: null,
changeDate(begin: Date | null, end: Date | null) {
this.begin = begin as any
this.end = end as any
},
}
const storeNull = observable(_store)
const store3 = observable({
begin: moment().hour(14).minute(0).toDate(),
end: moment().hour(18).minute(0).toDate(),
changeDate(begin: Date | null, end: Date | null) {
console.log('begin: ', begin)
console.log('end: ', end)
this.begin = begin as any
this.end = end as any
},
})
const disabledBegin = (date: Date) => {
return moment(date).isSameOrBefore(moment(date).hour(11))
}
const lastDaysofLastMonth = moment().add('month', -1).endOf('month')
const diffDays = moment().diff(lastDaysofLastMonth, 'days')
const lastMonthDays = moment(
lastDaysofLastMonth.format('YYYY-MM'),
'YYYY-MM'
).daysInMonth()
const quickList: QuickSelectListOptions[] = [
{
range: [
[0, 'day'],
[0, 'day'],
],
text: getLocale('今天'),
},
{
range: [
[-(lastMonthDays + diffDays), 'day'],
[-(diffDays + 1), 'day'],
],
text: getLocale('上个月'),
},
]
export const ComDateRangePickerInfo = () => (
<>
额外增加时间选择功能说明:
时间选择展示以 00:00 ~ 24:00 表示一天
24:00的返回形式为moment(date).endOf(`day`),与第二天 00:00 相差 1ms
调用方传给后台时需要自行加一层判断转换成 第二天的00:00
若需要自定义 日期展示格式, 也需要自行处理这个情况
特殊限制只能通过disabledDate来实现
>
)
export const ComDateRangePicker = () => (
<>
storeNull.changeDate(begin, end)}
/>
自定义日期展示
store.changeDate(begin, end)}
renderDate={(begin, end) =>
`${moment(begin).format('YYYY-MM-DD')} - ${moment(end).format('MM / DD')}`
}
/>
children自定义
storeNull.changeDate(begin, end)}
>
开始
{storeNull.begin ? moment(storeNull.begin ?? undefined).format('YYYY-MM-DD') : ''}
结束
{storeNull.end ? moment(storeNull.end ?? undefined).format('YYYY-MM-DD') : ''}
>
)
export const ComDateRangePickerWithDisabledDate = () => (
<>
store.changeDate(begin, end)}
min={moment().toDate()}
max={moment().add('day', 10).toDate()}
/>
限制一个月
storeNull.changeDate(begin, end)}
disabledDate={(d, { begin }) => {
if (begin) {
if (+moment(d) > +moment(begin).add('month', 1)) {
return true
}
}
return false
}}
/>
>
)
export const ComDateRangePickerWithClear = () => (
store.changeDate(begin, end)}
canClear
/>
)
export const ComDateRangePickerWithTimeSelect = () => (
store3.changeDate(begin, end)}
enabledTimeSelect
beginTimeSelect={{
defaultTime: moment().startOf('day').hour(12).toDate(),
disabledSpan: disabledBegin,
}}
endTimeSelect={{
disabledSpan: disabledBegin,
}}
timeSpan={60 * 60 * 1000}
/>
)
export const ComDateRangePickerWithQuickSelect = () => (
<>
自定义左侧选择
store.changeDate(begin, end)}
customQuickSelectList={quickList}
/>
>
)
export default {
title: '表单/DateRangePicker',
}