Skip to content

Commit a8fe373

Browse files
committed
fix(Calendar): 增加onChange & showBar api
1 parent 40a8113 commit a8fe373

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

packages/core/src/Calendar/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ export default CalendarView
9191
|------|------|-----|------|
9292
| `color` | 日历颜色 | string | - |
9393
| `lunarHoliday` | 是否显示农历及假日 | Boolean | `false` |
94-
| `showLunar` | 农历详情 | Boolean | `false` |
94+
| `showLunar` | 是否显示农历详情 | Boolean | `false` |
95+
| `showBar` | 是否显示导航栏 | Boolean | `true` |
9596
| `bar` | 导航栏 | React.ReactNode | - |
97+
| `onChange` | 选择时间事件 | (value: string) => void | - |
9698

9799
### bar 参数
98100
| 参数 | 说明 | 类型 | 默认值 |

packages/core/src/Calendar/index.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2-
import { View, Text, ViewProps, TextProps, StyleSheet, TouchableOpacity, Dimensions, Platform } from 'react-native';
2+
import { View, Text, ViewProps, StyleSheet, TouchableOpacity, Dimensions, Platform } from 'react-native';
33
import Icon from '../Icon';
44
import Ellipsis from '../Ellipsis';
55
import { getMonths, getWeeksArray, daysArrProps, getType, getNameLen } from './utils';
@@ -22,11 +22,14 @@ interface BarState {
2222
export interface CalendarProps extends ViewProps {
2323
// 日历颜色
2424
color: string;
25-
//是否显示农历及假日
25+
// 是否显示农历及假日
2626
lunarHoliday: boolean;
2727
bar?: BarState;
28-
//农历详情
28+
// 是否显示农历详情
2929
showLunar: boolean;
30+
onChange?: (value: string) => void;
31+
// 是否显示头部导航栏
32+
showBar?: boolean;
3033
}
3134

3235
const Calendar = (props: CalendarProps) => {
@@ -37,7 +40,7 @@ const Calendar = (props: CalendarProps) => {
3740
onPressBarLeft: undefined,
3841
render: undefined,
3942
};
40-
const { color = '#329BCB', lunarHoliday = false, bar = bars, showLunar = false } = props;
43+
const { color = '#329BCB', lunarHoliday = false, bar = bars, showLunar = false, onChange, showBar = true } = props;
4144
const { barRightText, title, barLeftText, onPressBarLeft, render } = bar;
4245
const [currentYear, setCurrentYear] = useState<number>(toYear);
4346
const [currentMonth, setCurrentMonth] = useState<number>(toMonth);
@@ -52,7 +55,9 @@ const Calendar = (props: CalendarProps) => {
5255
setLastData(toMonths[0]);
5356
setDayData(toMonths[1]);
5457
setNextData(toMonths[2]);
55-
}, [currentYear, currentMonth]);
58+
const date = currentYear + '-' + (currentMonth + 1) + '-' + currentDays;
59+
onChange?.(date);
60+
}, [currentYear, currentMonth, currentDays]);
5661

5762
/**
5863
* 头部导航栏
@@ -127,10 +132,10 @@ const Calendar = (props: CalendarProps) => {
127132
lunarAll.type === 1
128133
? [styles.dateBase, styles.otherMonth]
129134
: lunarAll.type === 2
130-
? [styles.currentMonth, styles.dateBase, { backgroundColor: color }]
131-
: lunarAll.type === 3
132-
? [styles.selectMonth, styles.dateBase, { borderColor: color }]
133-
: styles.dateBase
135+
? [styles.currentMonth, styles.dateBase, { backgroundColor: color }]
136+
: lunarAll.type === 3
137+
? [styles.selectMonth, styles.dateBase, { borderColor: color }]
138+
: styles.dateBase
134139
}
135140
onPress={() => goSelectDay(day)}
136141
>
@@ -218,7 +223,7 @@ const Calendar = (props: CalendarProps) => {
218223
return (
219224
<View style={{ flex: 1, position: 'relative' }}>
220225
<View style={{ flex: 1, backgroundColor: '#fff' }}>
221-
{renderBar}
226+
{showBar && renderBar}
222227
<View style={styles.calendarHeader}>
223228
<View style={styles.calendarHeaderItem}>
224229
<TouchableOpacity onPress={() => getCurrentYear('last')}>

0 commit comments

Comments
 (0)