Calendar 日历
用于选择日期区间。
规则
代码演示
基本
import { List, Switch, Calendar } from 'antd-mobile';
import enUS from 'antd-mobile/lib/calendar/locale/en_US';
import zhCN from 'antd-mobile/lib/calendar/locale/zh_CN';
const extra = {
'2017/07/15': { info: 'Disable', disable: true },
};
const now = new Date();
extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 5)] = { info: 'Disable', disable: true };
extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 6)] = { info: 'Disable', disable: true };
extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7)] = { info: 'Disable', disable: true };
extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 8)] = { info: 'Disable', disable: true };
Object.keys(extra).forEach((key) => {
const info = extra[key];
const date = new Date(key);
if (!Number.isNaN(+date) && !extra[+date]) {
extra[+date] = info;
}
});
class Test extends React.Component {
originbodyScrollY = document.getElementsByTagName('body')[0].style.overflowY;
constructor(props) {
super(props);
this.state = {
en: false,
show: false,
config: {},
};
}
renderBtn(zh, en, config = {}) {
config.locale = this.state.en ? enUS : zhCN;
return (
<List.Item arrow="horizontal"
onClick={() => {
document.getElementsByTagName('body')[0].style.overflowY = 'hidden';
this.setState({
show: true,
config,
});
}}
>
{this.state.en ? en : zh}
</List.Item>
);
}
changeLanguage = () => {
this.setState({
en: !this.state.en,
});
}
onSelectHasDisableDate = (dates) => {
console.warn('onSelectHasDisableDate', dates);
}
onConfirm = (startTime, endTime) => {
document.getElementsByTagName('body')[0].style.overflowY = this.originbodyScrollY;
this.setState({
show: false,
startTime,
endTime,
});
}
onCancel = () => {
document.getElementsByTagName('body')[0].style.overflowY = this.originbodyScrollY;
this.setState({
show: false,
startTime: undefined,
endTime: undefined,
});
}
getDateExtra = date => extra[+date];
render() {
return (
<div>
<List className="calendar-list" style={{ backgroundColor: 'white' }}>
<List.Item className="item" extra={<Switch className="right" checked={!this.state.en} onChange={this.changeLanguage} />}>
{this.state.en ? 'Chinese' : '中文'}
</List.Item>
{this.renderBtn('选择日期区间', 'Select Date Range')}
{this.renderBtn('选择日期时间区间', 'Select DateTime Range', { pickTime: true })}
{this.renderBtn('选择日期', 'Select Date', { type: 'one' })}
{this.renderBtn('选择日期时间', 'Select DateTime', { type: 'one', pickTime: true })}
{this.renderBtn('选择日期区间(快捷)', 'Select Date Range (Shortcut)', { showShortcut: true })}
{this.renderBtn('选择日期时间区间(快捷)', 'Select DateTime Range (Shortcut)', { pickTime: true, showShortcut: true })}
{this.renderBtn('大行距', 'XL row size', { rowSize: 'xl' })}
{this.renderBtn('不无限滚动', 'infinite: false', { infinite: false })}
{this.renderBtn('水平进入', 'Horizontal enter', { enterDirection: 'horizontal' })}
{this.renderBtn('默认选择范围', 'Selected Date Range', { defaultValue: [new Date(+now - 86400000), new Date(+now - 345600000)] })}
{this.renderBtn('onSelect API', 'onSelect API', {
onSelect: (date, state) => {
console.log('onSelect', date, state);
return [date, new Date(+now - 604800000)];
},
})}
{
this.state.startTime &&
<List.Item>Time1: {this.state.startTime.toLocaleString()}</List.Item>
}
{
this.state.endTime &&
<List.Item>Time2: {this.state.endTime.toLocaleString()}</List.Item>
}
</List>
<Calendar
{...this.state.config}
visible={this.state.show}
onCancel={this.onCancel}
onConfirm={this.onConfirm}
onSelectHasDisableDate={this.onSelectHasDisableDate}
getDateExtra={this.getDateExtra}
defaultDate={now}
minDate={new Date(+now - 5184000000)}
maxDate={new Date(+now + 31536000000)}
/>
</div>
);
}
}
ReactDOM.render(<Test />, mountNode);
.am-list-item .am-list-line .am-list-content {
display: flex;
}
.calendar-list .right {
float: right;
}
API
属性 | 说明 | 类型 | 默认值 | 必选 |
---|
enterDirection | 入场方向 vertical: 垂直 horizontal: 水平 | 'horizontal' | 'vertical' | vertical | false |
locale | 本地化 | Models.Locale | | false |
onCancel | 关闭时回调 | () => void | | false |
onConfirm | 确认时回调 | (startDateTime?: Date, endDateTime?: Date) => void | | false |
pickTime | 是否选择时间 | boolean | false | false |
prefixCls | 样式前缀 | string | rmc-calendar | false |
renderShortcut | 替换快捷选择栏 需要设置showShortcut: true | (select: (startDate?: Date, endDate?: Date) => void) => React.ReactNode | | false |
renderHeader | 替换标题栏 | () => React.ReactNode | | false |
showShortcut | 快捷日期选择 | boolean | false | false |
title | header title | string | {locale.title} | false |
type | 选择类型 one: 单日 range: 日期区间 | 'one' | 'range' | range | false |
visible | 是否显示 | boolean | false | false |
defaultDate | 显示开始日期 | Date | today | false |
getDateExtra | 日期扩展数据 | (date: Date) => Models.ExtraData | | false |
infiniteOpt | 无限滚动优化(大范围选择) | boolean | false | false |
initalMonths | 初始化月个数 | number | 6 | false |
maxDate | 最大日期 | Date | | false |
minDate | 最小日期 | Date | | false |
onSelect | 选择区间回调 | (date: Date, state?: [Date | undefined, Date | undefined]) => [Date, Date]|[Date]| void | | false |
onSelectHasDisableDate | 选择区间包含不可用日期 | (date: Date[]) => void | | false |
rowSize | 行大小 | 'normal' | 'xl' | | false |
defaultValue | 默认日历选择范围 | [Date, Date]|[Date] | | false |
defaultTimeValue | 默认时间选择值 | Date | | false |