DatePicker 日期选择

用于选择日期或者时间。

规则

  • 最多展示 5 个独立滚轮,每个滚轮表示一个不同的值。

代码演示

基本

日期时间选择示例 (rc-form 文档)

  1. import { DatePicker, List } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. import moment from 'moment';
  4. import 'moment/locale/zh-cn';
  5. import enUs from 'antd-mobile/lib/date-picker/locale/en_US';
  6. const zhNow = moment().locale('zh-cn').utcOffset(8);
  7. const maxDate = moment('2016-12-03 +0800', 'YYYY-MM-DD Z').utcOffset(8);
  8. const minDate = moment('2015-08-06 +0800', 'YYYY-MM-DD Z').utcOffset(8);
  9. const maxTime = moment('22:00 +0800', 'HH:mm Z').utcOffset(8);
  10. const minTime = moment('08:30 +0800', 'HH:mm Z').utcOffset(8);
  11. const gmtNow = moment().utcOffset(0);
  12. // 如果不是使用 List.Item 作为 children
  13. const CustomChildren = props => (
  14. <div
  15. onClick={props.onClick}
  16. style={{ backgroundColor: '#fff', height: '0.9rem', lineHeight: '0.9rem', padding: '0 0.3rem' }}
  17. >
  18. {props.children}
  19. <span style={{ float: 'right', color: '#888' }}>{props.extra}</span>
  20. </div>
  21. );
  22. class Test extends React.Component {
  23. state = {
  24. date: zhNow,
  25. dpValue: null,
  26. visible: false,
  27. }
  28. onChange = (date) => {
  29. // console.log('onChange', date);
  30. this.setState({
  31. date,
  32. });
  33. }
  34. render() {
  35. const { getFieldProps } = this.props.form;
  36. return (<div>
  37. <List
  38. className="date-picker-list"
  39. style={{ backgroundColor: 'white' }}
  40. >
  41. <DatePicker
  42. mode="date"
  43. title="选择日期"
  44. extra="可选,小于结束日期"
  45. {...getFieldProps('date1', {
  46. })}
  47. minDate={minDate}
  48. maxDate={maxDate}
  49. >
  50. <List.Item arrow="horizontal">日期(CST)</List.Item>
  51. </DatePicker>
  52. <DatePicker mode="time" {...getFieldProps('time1')} minuteStep={5}>
  53. <List.Item arrow="horizontal">时间(local time)</List.Item>
  54. </DatePicker>
  55. <DatePicker
  56. mode="time"
  57. {...getFieldProps('time', {
  58. initialValue: zhNow,
  59. })}
  60. minDate={minTime}
  61. maxDate={maxTime}
  62. >
  63. <List.Item arrow="horizontal">时间(CST),限定上下限</List.Item>
  64. </DatePicker>
  65. <DatePicker className="forss"
  66. mode="datetime"
  67. onChange={this.onChange}
  68. value={this.state.date}
  69. >
  70. <List.Item arrow="horizontal">日期+时间</List.Item>
  71. </DatePicker>
  72. <DatePicker
  73. mode="time"
  74. format={val => val.format('HH:mm Z')}
  75. okText="OK"
  76. dismissText="Cancel"
  77. locale={enUs}
  78. {...getFieldProps('customformat', {
  79. initialValue: gmtNow,
  80. })}
  81. >
  82. <List.Item arrow="horizontal">time(utc, UK time)</List.Item>
  83. </DatePicker>
  84. <List.Item extra={this.state.dpValue && this.state.dpValue.format('HH:mm Z')}>
  85. <div onClick={() => this.setState({ visible: true })}>自定义控制显示/隐藏的元素</div>
  86. </List.Item>
  87. <DatePicker
  88. visible={this.state.visible}
  89. mode="date"
  90. title={<span onClick={() => this.setState({ visible: false })}>点击可以关闭</span>}
  91. extra="请选择(可选)"
  92. onOk={() => console.log('onOk')}
  93. onDismiss={() => console.log('onDismiss')}
  94. value={this.state.dpValue}
  95. onChange={v => this.setState({ dpValue: v, visible: false })}
  96. />
  97. <DatePicker
  98. mode="date"
  99. title="选择日期"
  100. extra="请选择(可选)"
  101. value={this.state.dpValue}
  102. onChange={v => this.setState({ dpValue: v })}
  103. >
  104. <CustomChildren>时间选择(自定义 children)</CustomChildren>
  105. </DatePicker>
  106. </List>
  107. </div>);
  108. }
  109. }
  110. const TestWrapper = createForm()(Test);
  111. ReactDOM.render(<TestWrapper />, mountNode);
  1. .date-picker-list .am-list-item .am-list-line .am-list-extra {
  2. flex-basis: initial;
  3. }

DatePicker日期选择 - 图1

API

适用平台:WEB、React-Native
属性说明类型默认值
mode日期选择的类型, 可以是日期date、时间time、日期+时间datetime 、年year, 月 monthStringdate
value当前选中时间, 对应 mode 下格式分别为:YYYY-MM-DDHH:mmYYYY-MM-DD HH:mmmoment
minDate最小可选日期, 格式同 valuemoment-
maxDate最大可选日期, 格式同 valuemoment-
onChange时间发生变化的回调函数(date: Object): void
locale国际化,可覆盖全局LocaleProvider的配置Object: {DatePickerLocale: {year, month, day, hour, minute}, okText, dismissText }
title弹框的标题string/React.ReactElement
format格式化选中的值(value:moment) => string/string(val) => { return val; }
extraDatePicker children 建议是 List.Item, 如果不是,需要是自定义组件(组件内需处理onClick/extra属性)String请选择
disabled是否不可用Booleanfalse
prefixCls (WEB only)class前缀stringam-picker
className (WEB only)样式类名string-
minuteStepThe amount of time, in minutes, between each minute item.Number1
更多参考:https://github.com/react-component/m-date-picker