DatePicker日期选择框

选择日期的控件。

何时使用

当用户需要输入一个日期,可以点击标准输入框,弹出日期面板进行选择。

代码演示

DatePicker 日期选择框 - 图1

基本使用

日期选择器。

  1. import { DatePicker, MonthPicker, DateTimePicker, YearPicker, TimePicker, WeekPicker, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[datepicker]', value && value.format(), '[oldValue]', oldValue && oldValue.format());
  4. }
  5. ReactDOM.render(
  6. <Row gutter={10}>
  7. <Col span={12}>
  8. <DatePicker placeholder="Select date" onChange={handleChange} />
  9. </Col>
  10. <Col span={12}>
  11. <MonthPicker placeholder="Select month" onChange={handleChange} />
  12. </Col>
  13. <Col span={12}>
  14. <YearPicker placeholder="Select year" onChange={handleChange} />
  15. </Col>
  16. <Col span={12}>
  17. <DateTimePicker mode="dateTime" placeholder="Select date time" onChange={handleChange} />
  18. </Col>
  19. <Col span={12}>
  20. <TimePicker placeholder="Select time" onChange={handleChange} />
  21. </Col>
  22. <Col span={12}>
  23. <WeekPicker placeholder="Select week" onChange={handleChange} />
  24. </Col>
  25. <Col span={12}>
  26. <DatePicker placeholder="禁用" disabled />
  27. </Col>
  28. </Row>,
  29. mountNode
  30. );

DatePicker 日期选择框 - 图2

DataSet

绑定数据源。

  1. import { DataSet, DatePicker, DateTimePicker, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ value, oldValue }) {
  3. console.log(
  4. '[dataset newValue]',
  5. value && value.format(),
  6. '[oldValue]',
  7. oldValue && oldValue.format(),
  8. );
  9. }
  10. const data = [
  11. {
  12. birth: '1984-11-22',
  13. creationTime: '2017-12-22 15:00:00',
  14. },
  15. ];
  16. class App extends React.Component {
  17. ds = new DataSet({
  18. data,
  19. fields: [{ name: 'birth', type: 'date' }, { name: 'creationTime', type: 'dateTime' }],
  20. events: {
  21. update: handleDataSetChange,
  22. },
  23. });
  24. render() {
  25. return (
  26. <Row gutter={10}>
  27. <Col span={12}>
  28. <DatePicker dataSet={this.ds} name="birth" />
  29. </Col>
  30. <Col span={12}>
  31. <DateTimePicker dataSet={this.ds} name="creationTime" />
  32. </Col>
  33. </Row>
  34. );
  35. }
  36. }
  37. ReactDOM.render(<App />, mountNode);

DatePicker 日期选择框 - 图3

单元格渲染器

单元格渲染器。

  1. import { DatePicker } from 'choerodon-ui/pro';
  2. function dayRenderer(props, text, currentDate) {
  3. const dayInWeek = currentDate.get('d');
  4. if (dayInWeek === 0 || dayInWeek === 1) {
  5. props.style = { color: 'red' };
  6. }
  7. return <td {...props} />;
  8. }
  9. function cellRenderer(view) {
  10. if (view === 'date') {
  11. return dayRenderer;
  12. }
  13. }
  14. ReactDOM.render(
  15. <DatePicker placeholder="Select date" cellRenderer={cellRenderer} />,
  16. mountNode
  17. );

DatePicker 日期选择框 - 图4

范围

范围。

  1. import {
  2. DataSet,
  3. DatePicker,
  4. MonthPicker,
  5. YearPicker,
  6. DateTimePicker,
  7. Row,
  8. Col,
  9. } from 'choerodon-ui/pro';
  10. function handleDataSetChange({ value, oldValue }) {
  11. console.log('[range dataset newValue]', value, '[oldValue]', oldValue);
  12. }
  13. function handleChange(value, oldValue) {
  14. console.log('[range newValue]', value, '[oldValue]', oldValue);
  15. }
  16. function rangeValidator(value, name) {
  17. console.log(`[validation ${name} value]`, value);
  18. return true;
  19. }
  20. class App extends React.Component {
  21. ds = new DataSet({
  22. autoCreate: true,
  23. fields: [
  24. {
  25. name: 'date',
  26. type: 'date',
  27. range: ['start', 'end'],
  28. defaultValue: { start: '1984-11-22', end: new Date() },
  29. required: true,
  30. validator: rangeValidator,
  31. },
  32. {
  33. name: 'date2',
  34. type: 'date',
  35. range: true,
  36. defaultValue: ['1984-11-22', new Date()],
  37. validator: rangeValidator,
  38. },
  39. { name: 'multipleDate', type: 'date', range: true, multiple: true, required: true },
  40. ],
  41. events: {
  42. update: handleDataSetChange,
  43. },
  44. });
  45. render() {
  46. return (
  47. <Row gutter={10}>
  48. <Col span={24}>
  49. <DatePicker dataSet={this.ds} name="date" placeholder={['Start Date', 'End Date']} />
  50. </Col>
  51. <Col span={24}>
  52. <DatePicker dataSet={this.ds} name="date2" placeholder={['Start Date', 'End Date']} />
  53. </Col>
  54. <Col span={24}>
  55. <DatePicker
  56. range
  57. defaultValue={['1984-11-22', new Date()]}
  58. placeholder={['Start Date', 'End Date']}
  59. onChange={handleChange}
  60. />
  61. </Col>
  62. <Col span={24}>
  63. <DatePicker
  64. range={['start', 'end']}
  65. defaultValue={{ start: '1984-11-22', end: new Date() }}
  66. placeholder={['Start Date', 'End Date']}
  67. onChange={handleChange}
  68. />
  69. </Col>
  70. <Col span={24}>
  71. <DatePicker dataSet={this.ds} name="multipleDate" placeholder="Choose Date" />
  72. </Col>
  73. </Row>
  74. );
  75. }
  76. }
  77. ReactDOM.render(<App />, mountNode);

DatePicker 日期选择框 - 图5

受控日期控件

最简单的用法。

  1. import { DatePicker } from 'choerodon-ui/pro';
  2. class App extends React.Component {
  3. state = {}
  4. handleChange = (moment) => {
  5. console.log('[controlled]', moment && moment.format());
  6. this.setState({
  7. moment,
  8. });
  9. }
  10. render() {
  11. return (
  12. <DatePicker value={this.state.moment} placeholder="controlled" onChange={this.handleChange} />
  13. );
  14. }
  15. }
  16. ReactDOM.render(
  17. <App />,
  18. mountNode
  19. );

DatePicker 日期选择框 - 图6

最大最小值

指定最大最小值。

  1. import { DataSet, DatePicker, MonthPicker, YearPicker, DateTimePicker, Row, Col } from 'choerodon-ui/pro';
  2. import moment from 'moment';
  3. function filterDate(currentDate) {
  4. const dayInWeek = currentDate.get('d');
  5. return dayInWeek !== 0 && dayInWeek !== 1;
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. autoCreate: true,
  10. fields: [
  11. { name: 'startDate', type: 'date', max: 'endDate' },
  12. { name: 'endDate', type: 'date', min: 'startDate' },
  13. ],
  14. });
  15. render() {
  16. return (
  17. <Row gutter={10}>
  18. <Col span={12}>
  19. <DatePicker dataSet={this.ds} name="startDate" placeholder="start date" />
  20. </Col>
  21. <Col span={12}>
  22. <DatePicker dataSet={this.ds} name="endDate" placeholder="end date" />
  23. </Col>
  24. <Col span={12}>
  25. <DatePicker min={moment()} filter={filterDate} placeholder="Moment min & filter" />
  26. </Col>
  27. <Col span={12}>
  28. <MonthPicker min={new Date()} placeholder="Date min" />
  29. </Col>
  30. <Col span={12}>
  31. <YearPicker max="2019-02-10" placeholder="string max" />
  32. </Col>
  33. <Col span={12}>
  34. <DateTimePicker min={moment()} placeholder="Select date time" />
  35. </Col>
  36. </Row>
  37. );
  38. }
  39. }
  40. ReactDOM.render(
  41. <App />,
  42. mountNode
  43. );

DatePicker 日期选择框 - 图7

多值

通过属性multiple设置为多值。

  1. import { DataSet, DatePicker, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[multiple]', value, '[oldValue]', oldValue);
  4. }
  5. function handleDataSetChange({ record, name, value, oldValue }) {
  6. console.log('[dataset multiple]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  7. }
  8. class App extends React.Component {
  9. ds = new DataSet({
  10. autoCreate: true,
  11. fields: [
  12. { name: 'date', type: 'date', defaultValue: '1984-11-22,2019-08-09', required: true, multiple: ',' },
  13. ],
  14. events: {
  15. update: handleDataSetChange,
  16. },
  17. });
  18. render() {
  19. return (
  20. <Row gutter={10}>
  21. <Col span={12}>
  22. <DatePicker dataSet={this.ds} name="date" placeholder="数据源多选" />
  23. </Col>
  24. <Col span={12}>
  25. <DatePicker multiple onChange={handleChange} placeholder="多选" defaultValue={new Date()} />
  26. </Col>
  27. </Row>
  28. );
  29. }
  30. }
  31. ReactDOM.render(
  32. <App />,
  33. mountNode
  34. );

API

DatePicker

参数说明类型默认值
min最小日期MomentInput
max最大日期MomentInput
format日期格式,如 YYYY-MM-DD HH:mm:ssstring
filter日期过滤(currentDate, selected) => boolean
cellRenderer单元格渲染, view可选值:date dateTime time week month year decade(view) => (props, text, currentDate, selected) => ReactNode

更多属性请参考 TriggerField