Skip to content

Commit fa671eb

Browse files
Amber-Nan杨楠
andauthored
feat(Calendar):增加农历假期展示 & 文档更新 & 解决冲突 (#338)
* fix(Divider):优化 gutter属性不生效问题 * feat(Divider): 增加分割线标题的位置调整功能 & 更新文档 * feat(Rating):增加自定义每项的提示信息 & 更新文档 * feat(Rating):增加只读功能 & 文档更新 * feat(Timeline): 增加改变时间轴内容相对位置功能 & 文档更新 * style(Timeline):Update Readme.md img * feat(Timeline):增加自定义图标 & 文档更新 * feat(Timeline):优化自定义图标 & 文档更新 * feat:新增Calendar 日历组件 * doc(website): Update Calendar Readme.md * feat(Calendar):增加农历及假期展示 && 文档更新 * feat(Calendar):增加假日文字颜色 * feat(Calendar):左上角按钮增加自定义跳转功能&文档更新 * feat(Calendar):优化农历假日及文字排版 * feat(Calendar):处理文字长度 * fix(Calendar):优化安卓文字排版 Co-authored-by: 杨楠 <[email protected]>
1 parent e512eb5 commit fa671eb

File tree

6 files changed

+483
-48
lines changed

6 files changed

+483
-48
lines changed

example/examples/src/routes/Calendar/index.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ export interface CalendarViewProps extends ComProps {}
1010

1111
export default class CalendarView extends React.Component<CalendarViewProps> {
1212
render() {
13-
const {route} = this.props;
13+
const {route, navigation} = this.props;
1414
const description = route.params.description;
1515
const title = route.params.title;
1616
const barProps = {
17-
barRightText: '返回啦',
18-
title: '日历啦',
19-
onPressBarLeft: () => {},
20-
barLeftText: '今天啦',
17+
barRightText: '返回',
18+
title: '日历',
19+
onPressBarLeft: () => navigation.goBack(),
20+
barLeftText: '今天',
2121
};
2222
return (
2323
<Container>
2424
<Layout>
2525
<Header title={title} description={description} />
2626
<Body>
27-
<Calendar bar={barProps} />
27+
<Calendar />
2828
<Spacing size={'large'} />
29-
<Calendar color="red" bar={{}} />
29+
<Calendar color="red" lunarHoliday={true} />
30+
<Calendar bar={barProps} />
3031
</Body>
3132
<Footer />
3233
</Layout>

packages/core/src/Calendar/README.md

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Calendar 日历
33

44
展示日历
55

6-
![](https://user-images.githubusercontent.com/66067296/141421928-d46ffd84-2349-49ac-8b6f-4e8455a017eb.gif)<!--rehype:style=zoom: 33%;float: right; margin-left: 15px;-->
6+
![](https://user-images.githubusercontent.com/66067296/141939109-5fbd8b77-82fa-4eb4-bd6d-6670fd577878.gif)<!--rehype:style=zoom: 33%;float: right; margin-left: 15px;-->
77

88
### 基础示例
99

@@ -38,7 +38,40 @@ export default class CalendarView extends React.Component<CalendarViewProps> {
3838
}
3939
```
4040

41-
### 自定义日历颜色
41+
### 自定义日历颜色、农历及假日展示
42+
43+
```jsx
44+
import React from 'react';
45+
import { Text, View } from 'react-native';
46+
import { Calendar } from '@uiw/react-native';
47+
import { ComProps } from '../../routes';
48+
import Layout, { Container } from '../../Layout';
49+
50+
const { Header, Body, Card, Footer } = Layout;
51+
52+
export interface CalendarViewProps extends ComProps { }
53+
54+
export default class CalendarView extends React.Component<CalendarViewProps> {
55+
render() {
56+
const { route } = this.props;
57+
const description = route.params.description;
58+
const title = route.params.title;
59+
return (
60+
<Container>
61+
<Layout>
62+
<Header title={title} description={description} />
63+
<Body>
64+
<Calendar color="red" lunarHoliday={true}/>
65+
</Body>
66+
<Footer />
67+
</Layout>
68+
</Container>
69+
);
70+
}
71+
}
72+
```
73+
74+
### 自定义日历头部
4275

4376
```jsx
4477
import React from 'react';
@@ -57,17 +90,17 @@ export default class CalendarView extends React.Component<CalendarViewProps> {
5790
const description = route.params.description;
5891
const title = route.params.title;
5992
const barProps = {
60-
barRightText: "返回啦",
61-
title : "日历啦",
62-
onPressBarLeft: () => { },
63-
barLeftText: "今天啦"
93+
barRightText: "返回",
94+
title : "日历",
95+
onPressBarLeft: () => navigation.goBack(),
96+
barLeftText: "今天"
6497
}
6598
return (
6699
<Container>
67100
<Layout>
68101
<Header title={title} description={description} />
69102
<Body>
70-
<Calendar color="red" bar={barProps} />
103+
<Calendar color="red" bar={barProps} lunarHoliday={true}/>
71104
</Body>
72105
<Footer />
73106
</Layout>
@@ -84,6 +117,8 @@ export default class CalendarView extends React.Component<CalendarViewProps> {
84117
interface barState {
85118
// 导航栏标题
86119
title?: string
120+
//是否显示农历及假日
121+
lunarHoliday:Boolean
87122
// 导航左侧文本
88123
barRightText?: string;
89124
// 导航右侧文本
@@ -96,6 +131,8 @@ interface barState {
96131
export interface CalendarProps extends ViewProps {
97132
// 日历颜色
98133
color?: string;
134+
//是否显示农历及假日
135+
lunarHoliday: boolean;
99136
bar: barState ;
100137
}
101-
```
138+
```

packages/core/src/Calendar/index.tsx

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useEffect, useState } from 'react';
2-
import { View, Text, ViewProps, TextProps, StyleSheet, TouchableOpacity, Dimensions } from 'react-native';
2+
import { View, Text, ViewProps, TextProps, StyleSheet, TouchableOpacity, Dimensions, Platform } from 'react-native';
33
import { color } from 'src/utils';
44
import Icon from '../Icon';
5-
import { getMonths, getWeeksArray, daysArrProps } from './utils';
5+
import Ellipsis from '../Ellipsis';
6+
import { getMonths, getWeeksArray, daysArrProps, getType, getNameLen } from './utils';
67

78
export let MainWidth = Dimensions.get('window').width;
89
export let newDates = new Date();
@@ -19,14 +20,21 @@ interface barState {
1920
}
2021
export interface CalendarProps extends ViewProps {
2122
// 日历颜色
22-
color?: string;
23-
bar: barState;
23+
color: string;
24+
//是否显示农历及假日
25+
lunarHoliday: boolean;
26+
bar?: barState;
2427
}
2528
const Calendar = (props: CalendarProps) => {
26-
const {
27-
color = '#329BCB',
28-
bar: { render = null, barRightText = '返回', title = '日历', barLeftText = '今天', onPressBarLeft = null },
29-
} = props;
29+
const bars = {
30+
barRightText: 'Menu',
31+
title: 'Calendar',
32+
barLeftText: 'Today',
33+
onPressBarLeft: undefined,
34+
render: undefined,
35+
};
36+
const { color = '#329BCB', lunarHoliday = false, bar = bars } = props;
37+
const { barRightText, title, barLeftText, onPressBarLeft, render } = bar;
3038

3139
const [currentYear, setCurrentYear] = useState<number>(toYear);
3240
const [currentMonth, setCurrentMonth] = useState<number>(toMonth);
@@ -36,7 +44,7 @@ const Calendar = (props: CalendarProps) => {
3644
const [nextData, setNextData] = useState<number[]>([]);
3745

3846
useEffect(() => {
39-
let toMonths = getMonths(currentYear, currentMonth);
47+
let toMonths = getMonths(currentYear, currentMonth, currentDays);
4048
setLastData(toMonths[0]);
4149
setDayData(toMonths[1]);
4250
setNextData(toMonths[2]);
@@ -81,31 +89,27 @@ const Calendar = (props: CalendarProps) => {
8189
};
8290

8391
const renderWeeks = () => {
84-
let groupedDays = getWeeksArray(lastData, dayData, nextData);
92+
let groupedDays = getWeeksArray(lastData, dayData, nextData, currentYear, currentMonth);
8593
return groupedDays.map((weekDays, index) => {
8694
return (
87-
<View key={index} style={styles.weekDays}>
95+
<View key={index} style={styles.weekDay}>
8896
{renderDays(weekDays)}
8997
</View>
9098
);
9199
});
92100
};
93101
const renderDays = (weekDays: daysArrProps[]) => {
94102
return weekDays.map((day, index) => {
95-
let type = 0;
96-
if (day.type === 'last' || day.type === 'next') {
97-
type = 1;
98-
} else if (
99-
currentYear === toYear &&
100-
currentMonth === toMonth &&
101-
day.monthDays === currentDays &&
102-
currentDays === toDays
103-
) {
104-
type = 2;
105-
} else if (day.monthDays === currentDays) {
106-
type = 3;
103+
let type = getType(day, currentYear, currentMonth, currentDays, toYear, toMonth, toDays);
104+
let nameLen = getNameLen(day.lunarHolidays);
105+
let lineHeight =
106+
lunarHoliday === true && Platform.OS === 'ios' ? 0 : lunarHoliday === true ? 18 : MainWidth / 7 - 4.5;
107+
let paddingTop = lunarHoliday === true ? 4 : 0;
108+
let colorType = '';
109+
if (day.colorType === '') {
110+
colorType = '#828282';
107111
} else {
108-
type = 0;
112+
colorType = color;
109113
}
110114
return (
111115
<TouchableOpacity
@@ -121,9 +125,31 @@ const Calendar = (props: CalendarProps) => {
121125
}
122126
onPress={() => goSelectDay(day)}
123127
>
124-
<Text style={[styles.dayText, { color: type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : '#000' }]}>
128+
<Text
129+
style={[
130+
styles.dayText,
131+
{
132+
color: type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : '#000',
133+
lineHeight: lineHeight,
134+
paddingTop: paddingTop,
135+
},
136+
]}
137+
>
125138
{day.monthDays}
126139
</Text>
140+
{lunarHoliday === true && (
141+
<Text
142+
style={[
143+
styles.dayText,
144+
{
145+
color: type === 1 ? '#B5B5B5' : type === 2 ? '#fff' : colorType,
146+
fontSize: 13,
147+
},
148+
]}
149+
>
150+
{nameLen > 3 ? <Ellipsis maxLen={2}>{day.lunarHolidays}</Ellipsis> : day.lunarHolidays}
151+
</Text>
152+
)}
127153
</TouchableOpacity>
128154
);
129155
});
@@ -208,14 +234,17 @@ const Calendar = (props: CalendarProps) => {
208234
};
209235
const styles = StyleSheet.create({
210236
header: {
237+
flex: 1,
211238
backgroundColor: '#329BCB',
212239
flexDirection: 'row',
213240
padding: 10,
241+
alignItems: 'center',
214242
justifyContent: 'space-between',
215243
},
216244
headerBtn: {
217245
flexDirection: 'row',
218246
alignItems: 'center',
247+
width: 50,
219248
},
220249
headerText: {
221250
fontSize: 20,
@@ -249,11 +278,10 @@ const styles = StyleSheet.create({
249278
color: '#616161',
250279
textAlign: 'center',
251280
},
252-
253281
calendarDays: {
254282
marginVertical: 10,
255283
},
256-
weekDays: {
284+
weekDay: {
257285
flexDirection: 'row',
258286
paddingHorizontal: 2,
259287
},
@@ -286,7 +314,6 @@ const styles = StyleSheet.create({
286314
dayText: {
287315
textAlign: 'center',
288316
fontSize: 17,
289-
lineHeight: MainWidth / 7 - 4.5,
290317
fontWeight: '500',
291318
},
292319
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
export let lunarInfo = [
2+
0x4bd8, 0x4ae0, 0xa570, 0x54d5, 0xd260, 0xd950, 0x5554, 0x56af, 0x9ad0, 0x55d2, 0x4ae0, 0xa5b6, 0xa4d0, 0xd250,
3+
0xd255, 0xb54f, 0xd6a0, 0xada2, 0x95b0, 0x4977, 0x497f, 0xa4b0, 0xb4b5, 0x6a50, 0x6d40, 0xab54, 0x2b6f, 0x9570,
4+
0x52f2, 0x4970, 0x6566, 0xd4a0, 0xea50, 0x6a95, 0x5adf, 0x2b60, 0x86e3, 0x92ef, 0xc8d7, 0xc95f, 0xd4a0, 0xd8a6,
5+
0xb55f, 0x56a0, 0xa5b4, 0x25df, 0x92d0, 0xd2b2, 0xa950, 0xb557, 0x6ca0, 0xb550, 0x5355, 0x4daf, 0xa5b0, 0x4573,
6+
0x52bf, 0xa9a8, 0xe950, 0x6aa0, 0xaea6, 0xab50, 0x4b60, 0xaae4, 0xa570, 0x5260, 0xf263, 0xd950, 0x5b57, 0x56a0,
7+
0x96d0, 0x4dd5, 0x4ad0, 0xa4d0, 0xd4d4, 0xd250, 0xd558, 0xb540, 0xb6a0, 0x95a6, 0x95bf, 0x49b0, 0xa974, 0xa4b0,
8+
0xb27a, 0x6a50, 0x6d40, 0xaf46, 0xab60, 0x9570, 0x4af5, 0x4970, 0x64b0, 0x74a3, 0xea50, 0x6b58, 0x5ac0, 0xab60,
9+
0x96d5, 0x92e0, 0xc960, 0xd954, 0xd4a0, 0xda50, 0x7552, 0x56a0, 0xabb7, 0x25d0, 0x92d0, 0xcab5, 0xa950, 0xb4a0,
10+
0xbaa4, 0xad50, 0x55d9, 0x4ba0, 0xa5b0, 0x5176, 0x52bf, 0xa930, 0x7954, 0x6aa0, 0xad50, 0x5b52, 0x4b60, 0xa6e6,
11+
0xa4e0, 0xd260, 0xea65, 0xd530, 0x5aa0, 0x76a3, 0x96d0, 0x4afb, 0x4ad0, 0xa4d0, 0xd0b6, 0xd25f, 0xd520, 0xdd45,
12+
0xb5a0, 0x56d0, 0x55b2, 0x49b0, 0xa577, 0xa4b0, 0xaa50, 0xb255, 0x6d2f, 0xada0, 0x4b63, 0x937f, 0x49f8, 0x4970,
13+
0x64b0, 0x68a6, 0xea5f, 0x6b20, 0xa6c4, 0xaaef, 0x92e0, 0xd2e3, 0xc960, 0xd557, 0xd4a0, 0xda50, 0x5d55, 0x56a0,
14+
0xa6d0, 0x55d4, 0x52d0, 0xa9b8, 0xa950, 0xb4a0, 0xb6a6, 0xad50, 0x55a0, 0xaba4, 0xa5b0, 0x52b0, 0xb273, 0x6930,
15+
0x7337, 0x6aa0, 0xad50, 0x4b55, 0x4b6f, 0xa570, 0x54e4, 0xd260, 0xe968, 0xd520, 0xdaa0, 0x6aa6, 0x56df, 0x4ae0,
16+
0xa9d4, 0xa4d0, 0xd150, 0xf252, 0xd520,
17+
];
18+
19+
export let nStr1 = ['日', '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'];
20+
21+
export let nStr2 = ['初', '十', '廿', '三十'];
22+
23+
/**
24+
* 公历节日
25+
*/
26+
export let basejieri = {
27+
'0101': '元旦',
28+
'0214': '情人节',
29+
'0308': '妇女节',
30+
'0312': '植树节',
31+
'0401': '愚人节',
32+
'0501': '劳动节',
33+
'0504': '青年节',
34+
'0601': '儿童节',
35+
'0701': '建党节',
36+
'0801': '建军节',
37+
'0910': '教师节',
38+
'1001': '国庆节',
39+
'1031': '万圣夜',
40+
'1125': '感恩节',
41+
'1224': '平安夜',
42+
'1225': '圣诞节',
43+
};
44+
45+
/**
46+
* 农历节日
47+
*/
48+
export let lunarHoliday = {
49+
'0101': '春节',
50+
'0115': '元宵节',
51+
'0505': '端午节',
52+
'0707': '七夕节',
53+
'0715': '中元节',
54+
'0815': '中秋节',
55+
'0909': '重阳节',
56+
'1208': '腊八节',
57+
'1223': '北方小年',
58+
'1224': '南方小年',
59+
};
60+
61+
/**
62+
* 节气
63+
*/
64+
export let solarTerm = [
65+
'小寒',
66+
'大寒',
67+
'立春',
68+
'雨水',
69+
'惊蛰',
70+
'春分',
71+
'清明',
72+
'谷雨',
73+
'立夏',
74+
'小满',
75+
'芒种',
76+
'夏至',
77+
'小暑',
78+
'大暑',
79+
'立秋',
80+
'处暑',
81+
'白露',
82+
'秋分',
83+
'寒露',
84+
'霜降',
85+
'立冬',
86+
'小雪',
87+
'大雪',
88+
'冬至',
89+
];
90+
91+
export let sTermInfo = [
92+
0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033,
93+
353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758,
94+
];

0 commit comments

Comments
 (0)