Skip to content

Commit f1c2c64

Browse files
committed
Merge branch 'master' into infra/refactorTimeline_2
2 parents 1929093 + f4cb153 commit f1c2c64

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

example/src/screens/calendars.tsx

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState, Fragment} from 'react';
1+
import React, {useState, Fragment, useCallback} from 'react';
22
import {StyleSheet, View, ScrollView, Text, TouchableOpacity, Switch} from 'react-native';
33
import {Calendar, CalendarProps} from 'react-native-calendars';
44
import testIDs from '../testIDs';
@@ -358,6 +358,56 @@ const CalendarsScreen = () => {
358358
);
359359
};
360360

361+
const renderCalendarWithCustomHeaderTitle = () => {
362+
const [selectedValue, setSelectedValue] = useState(new Date());
363+
364+
const getNewSelectedDate = useCallback(
365+
(date, shouldAdd) => {
366+
const newMonth = new Date(date).getMonth();
367+
const month = shouldAdd ? newMonth + 1 : newMonth - 1;
368+
const newDate = new Date(selectedValue.setMonth(month));
369+
const newSelected = new Date(newDate.setDate(1));
370+
return newSelected;
371+
},
372+
[selectedValue]
373+
);
374+
const onPressArrowLeft = useCallback(
375+
(subtract, month) => {
376+
const newDate = getNewSelectedDate(month, false);
377+
setSelectedValue(newDate);
378+
subtract();
379+
},
380+
[getNewSelectedDate]
381+
);
382+
383+
const onPressArrowRight = useCallback(
384+
(add, month) => {
385+
const newDate = getNewSelectedDate(month, true);
386+
setSelectedValue(newDate);
387+
add();
388+
},
389+
[getNewSelectedDate]
390+
);
391+
392+
const CustomHeaderTitle = (
393+
<TouchableOpacity style={styles.customTitleContainer} onPress={() => console.warn('Tapped!')}>
394+
<Text style={styles.customTitle}>{selectedValue.getMonth() + 1}-{selectedValue.getFullYear()}</Text>
395+
</TouchableOpacity>
396+
);
397+
398+
return (
399+
<Fragment>
400+
<Text style={styles.text}>Calendar with custom header title</Text>
401+
<Calendar
402+
style={styles.calendar}
403+
customHeaderTitle={CustomHeaderTitle}
404+
onPressArrowLeft={onPressArrowLeft}
405+
onPressArrowRight={onPressArrowRight}
406+
/>
407+
</Fragment>
408+
);
409+
};
410+
361411
const renderCalendarWithCustomHeader = () => {
362412
const CustomHeader = React.forwardRef((props, ref) => {
363413
return (
@@ -424,8 +474,9 @@ const CalendarsScreen = () => {
424474
{renderCalendarWithWeekNumbers()}
425475
{renderCalendarWithMinAndMaxDates()}
426476
{renderCalendarWithCustomDay()}
427-
{renderCalendarWithCustomHeader()}
428477
{renderCalendarWithInactiveDays()}
478+
{renderCalendarWithCustomHeaderTitle()}
479+
{renderCalendarWithCustomHeader()}
429480
</Fragment>
430481
);
431482
};
@@ -494,5 +545,15 @@ const styles = StyleSheet.create({
494545
justifyContent: 'space-around',
495546
marginHorizontal: -4,
496547
padding: 8
548+
},
549+
customTitleContainer: {
550+
flexDirection: 'row',
551+
alignItems: 'center',
552+
padding: 10
553+
},
554+
customTitle: {
555+
fontSize: 16,
556+
fontWeight: 'bold',
557+
color: '#00BBF2'
497558
}
498559
});

src/calendar/header/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ export interface CalendarHeaderProps {
5454
disableArrowRight?: boolean;
5555
/** Apply custom disable color to selected day indexes */
5656
disabledDaysIndexes?: number[];
57-
/** Replace default month and year title with custom one. the function receive a date as parameter */
57+
/** Replace default title with custom one. the function receive a date as parameter */
5858
renderHeader?: (date?: XDate) => ReactNode;
59+
/** Replace default title with custom element */
60+
customHeaderTitle?: JSX.Element;
5961
/** Provide aria-level for calendar heading for proper accessibility when used with web (react-native-web) */
6062
webAriaLevel?: number;
6163
testID?: string;
@@ -92,8 +94,10 @@ class CalendarHeader extends Component<CalendarHeaderProps> {
9294
disableArrowRight: PropTypes.bool,
9395
/** Apply custom disable color to selected day indexes */
9496
disabledDaysIndexes: PropTypes.arrayOf(PropTypes.number),
95-
/** Replace default month and year title with custom one. the function receive a date as parameter. */
97+
/** Replace default title with custom one. the function receive a date as parameter */
9698
renderHeader: PropTypes.any,
99+
/** Replace default title with custom element */
100+
customHeaderTitle: PropTypes.any,
97101
/** Provide aria-level for calendar heading for proper accessibility when used with web (react-native-web) */
98102
webAriaLevel: PropTypes.number
99103
};
@@ -123,7 +127,8 @@ class CalendarHeader extends Component<CalendarHeaderProps> {
123127
'renderArrow',
124128
'disableArrowLeft',
125129
'disableArrowRight',
126-
'renderHeader'
130+
'renderHeader',
131+
'customHeaderTitle'
127132
]);
128133
}
129134

@@ -180,13 +185,17 @@ class CalendarHeader extends Component<CalendarHeaderProps> {
180185
});
181186

182187
renderHeader = () => {
183-
const {renderHeader, month, monthFormat, testID, webAriaLevel} = this.props;
188+
const {customHeaderTitle, renderHeader, month, monthFormat, testID, webAriaLevel} = this.props;
184189
const webProps = Platform.OS === 'web' ? {'aria-level': webAriaLevel} : {};
185190

186191
if (renderHeader) {
187192
return renderHeader(month);
188193
}
189194

195+
if (customHeaderTitle) {
196+
return customHeaderTitle;
197+
}
198+
190199
return (
191200
<Fragment>
192201
<Text

src/calendar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface CalendarProps extends CalendarHeaderProps, DayProps {
6666
disabledByDefault?: boolean;
6767
/** Style passed to the header */
6868
headerStyle?: ViewStyle;
69-
/** Allow rendering of a totally custom header */
69+
/** Allow rendering a totally custom header */
7070
customHeader?: any;
7171
/** Allow selection of dates before minDate or after maxDate */
7272
allowSelectionOutOfRange?: boolean;
@@ -127,7 +127,7 @@ class Calendar extends Component<CalendarProps, State> {
127127
disabledByDefault: PropTypes.bool,
128128
/** Style passed to the header */
129129
headerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
130-
/** Allow rendering of a totally custom header */
130+
/** Allow rendering a totally custom header */
131131
customHeader: PropTypes.any,
132132
/** Allow selection of dates before minDate or after maxDate */
133133
allowSelectionOutOfRange: PropTypes.bool

0 commit comments

Comments
 (0)