/* eslint-disable max-lines-per-function */
import React, { useState, useMemo } from 'react';
import { DatePicker, ConfigProvider, Select } from '@douyinfe/semi-ui';
import * as _ from 'lodash';
import * as dateFns from 'date-fns';
import { utcToZonedTime } from 'date-fns-tz';
const { Option } = Select;
function Demo() {
const now = new Date();
const startDate = dateFns.addMinutes(dateFns.subDays(now, 1), 5);
const [timeZone, setTimeZone] = useState('GMT+09:00');
const gmtList = useMemo(() => {
const list = [];
for (let hourOffset = -11; hourOffset <= 14; hourOffset++) {
const prefix = hourOffset >= 0 ? '+' : '-';
const hOffset = Math.abs(parseInt(hourOffset, 10));
list.push(`GMT${prefix}${String(hOffset).padStart(2, '0')}:00`);
}
return list;
}, []);
return (
{
const date = new Date(str);
if (str.length <= 4) {
return date.getFullYear() < 2015;
}
return date.getMonth() + 1 < 10;
}}
onChange={date => console.log('date changed', date)}
/>
{
const date = new Date(str);
if (str.length <= 4) {
return date.getFullYear() < 2015;
}
return date.getMonth() + 1 < 10;
}}
/>
Disabled 17:00:00-18:00:00 today
(dateFns.isToday(date)
? {
disabledHours: () => [17, 18],
disabledMinutes: hour => (19 === hour ? _.range(0, 10, 1) : []),
disabledSeconds: (hour, minute) =>
(hour === 20 && minute === 20 ? _.range(0, 20, 1) : []),
}
: null)
}
/>
Month Picker
// [2019, 2018].includes(dateFns.getYear(date)) ||
2020 === dateFns.getYear(date) && [1, 2, 3].includes(dateFns.getMonth(date))
}
/>
{/* 开始时间不能早于当下时间+5min,结束时间要在7天内 */}
{
const deadDate = dateFns.addDays(now, 7);
return dateFns.isAfter(cur, deadDate) || dateFns.isBefore(cur, startDate);
}}
disabledTime={(dates, panelType) => {
if (dateFns.isToday(dates[0]) && panelType === 'left') {
return {
disabledHours: () => _.range(0, now.getHours(), 1),
disabledMinutes: hour =>
(now.getHours() === hour ? _.range(0, now.getMinutes() + 5, 1) : []),
};
}
}}
onChange={(...args) => console.log(...args)}
/>
{/* 动态禁止时间,禁止选中之前的Date */}
Disabled date before rangeStart
{
const { rangeStart } = options;
const startDate = dateFns.parseISO(rangeStart);
return dateFns.isBefore(cur, startDate);
}}
style={{ width: 240 }}
/>
type=month时禁止2020年且11月之前的日期 v1.10.0
{
if (cur.getFullYear() === 2020 && cur.getMonth() < 10) {
return true;
}
return false;
}}
style={{ width: 240 }}
/>
type=month时禁止2008年-2020年 v1.10.0
{
const currentYear = cur.getFullYear();
if (2008 <= currentYear && currentYear <= 2020) {
return true;
}
return false;
}}
/>
fix disabledDate callback timeZone bug
Select Time Zone:
{
const date = new Date(str);
const isDisabled = date.valueOf() === 1626274800000;
if (isDisabled) {
console.log(str);
}
return isDisabled;
}}
placeholder="禁用每月10号"
onChange={date => {
console.log('selected', date);
}}
/>
{
const date = new Date(str);
const localDate = utcToZonedTime(date, timeZone);
const isDisabled = localDate.getDate() === 15;
return isDisabled ? ({ disabledHours: () => [18] }) : null;
}}
defaultPickerValue={new Date('2021-07-15 17:00:00')}
placeholder="禁用7月15号下午6点"
onChange={date => {
console.log('selected', date);
}}
/>
);
}
export default Demo;