Skip to content

Commit f386eb7

Browse files
authored
feat(Calendar): add month calendar
close #45
1 parent b32e39c commit f386eb7

File tree

9 files changed

+616
-5
lines changed

9 files changed

+616
-5
lines changed

src/components/Calendar/Calendar.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ moment()
1313
.locale('zh-cn')
1414
.utcOffset(8);
1515

16-
@localeConsumerDecorator({ defaultLocale: LOCALE, localeName: 'Calendar' })
16+
@localeConsumerDecorator({ defaultLocale: LOCALE, localeName: 'Calendar', publicFn: ['focus'] })
1717
class Calendar extends Component {
1818
static propTypes = {
1919
/** 选中的时间,受控,Moment 类型 */
@@ -44,11 +44,21 @@ class Calendar extends Component {
4444
onSelect(value);
4545
}
4646
};
47+
focus = () => {
48+
this.calendar && this.calendar.focus();
49+
};
4750
render() {
4851
/* eslint-disable-next-line no-unused-vars */
4952
const { rules = {}, onSelect, ...rest } = this.props;
5053

51-
return <CalendarWrap onSelect={this.onSelect} {...calCalendarProps({ rules })} {...rest} />;
54+
return (
55+
<CalendarWrap
56+
innerRef={ref => (this.calendar = ref)}
57+
onSelect={this.onSelect}
58+
{...calCalendarProps({ rules })}
59+
{...rest}
60+
/>
61+
);
5262
}
5363
}
5464

src/components/Calendar/Calendar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### 说明
22

3-
* 这是 Calendar 组件
3+
* 这是 日历 组件
44

55
### 演示
66

src/components/Calendar/Month.jsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import moment from 'moment';
4+
5+
import localeConsumerDecorator from 'src/components/LocaleProvider/localeConsumerDecorator';
6+
7+
import { getValidDate } from './utils';
8+
import { MonthCalendarWrap } from './style';
9+
import LOCALE from './locale/zh_CN';
10+
import { calCalendarProps } from './Calendar';
11+
12+
import 'moment/locale/zh-cn';
13+
moment()
14+
.locale('zh-cn')
15+
.utcOffset(8);
16+
17+
@localeConsumerDecorator({ defaultLocale: LOCALE, localeName: 'Calendar', publicFn: ['focus'] })
18+
class Month extends Component {
19+
static propTypes = {
20+
/** 选中的时间,受控,Moment 类型 */
21+
value: PropTypes.object,
22+
/** 默认选中的时间,非受控,Moment 类型 */
23+
defaultValue: PropTypes.object,
24+
/** 实际选中时间修改的回调 */
25+
onSelect: PropTypes.func,
26+
/** 变化时的回调,键盘操作快速切换等操作时触发 */
27+
onChange: PropTypes.func,
28+
/** 自定义规则 */
29+
rules: PropTypes.shape({
30+
/** 可选时间范围,格式为[start, end],数据格式可以为moment实例、Date实例、时间戳 */
31+
range: PropTypes.array,
32+
/**
33+
* 自定义禁用日期函数,返回true时该日期不可选
34+
* @param current - 日期
35+
* @param value - 日历当前选中值
36+
*/
37+
custom: PropTypes.func
38+
})
39+
};
40+
static defaultProps = {};
41+
onSelect = value => {
42+
const { rules, onSelect } = this.props;
43+
value = getValidDate(value, rules);
44+
if (onSelect) {
45+
onSelect(value);
46+
}
47+
};
48+
focus = () => {
49+
this.calendar && this.calendar.focus();
50+
};
51+
render() {
52+
/* eslint-disable-next-line no-unused-vars */
53+
const { rules = {}, onSelect, ...rest } = this.props;
54+
55+
return (
56+
<MonthCalendarWrap
57+
innerRef={ref => (this.calendar = ref)}
58+
onSelect={this.onSelect}
59+
{...calCalendarProps({ rules })}
60+
{...rest}
61+
/>
62+
);
63+
}
64+
}
65+
66+
export default Month;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import Calendar from 'components/Calendar';
3+
4+
// demo start
5+
const Demo = () => (
6+
<div>
7+
<Calendar.Month
8+
onSelect={v => console.log('select', v)}
9+
onChange={v => console.log('change', v)}
10+
rules={{ range: [Date.now() - 3 * 30 * 24 * 60 * 60 * 1000, Date.now() + 3 * 30 * 24 * 60 * 60 * 1000] }}
11+
/>
12+
</div>
13+
);
14+
// demo end
15+
16+
export default Demo;

0 commit comments

Comments
 (0)