|
1 | | -import React, {useState, Fragment} from 'react'; |
| 1 | +import React, {useState, Fragment, useCallback} from 'react'; |
2 | 2 | import {StyleSheet, View, ScrollView, Text, TouchableOpacity, Switch} from 'react-native'; |
3 | 3 | import {Calendar, CalendarProps} from 'react-native-calendars'; |
4 | 4 | import testIDs from '../testIDs'; |
@@ -358,6 +358,56 @@ const CalendarsScreen = () => { |
358 | 358 | ); |
359 | 359 | }; |
360 | 360 |
|
| 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 | + |
361 | 411 | const renderCalendarWithCustomHeader = () => { |
362 | 412 | const CustomHeader = React.forwardRef((props, ref) => { |
363 | 413 | return ( |
@@ -424,8 +474,9 @@ const CalendarsScreen = () => { |
424 | 474 | {renderCalendarWithWeekNumbers()} |
425 | 475 | {renderCalendarWithMinAndMaxDates()} |
426 | 476 | {renderCalendarWithCustomDay()} |
427 | | - {renderCalendarWithCustomHeader()} |
428 | 477 | {renderCalendarWithInactiveDays()} |
| 478 | + {renderCalendarWithCustomHeaderTitle()} |
| 479 | + {renderCalendarWithCustomHeader()} |
429 | 480 | </Fragment> |
430 | 481 | ); |
431 | 482 | }; |
@@ -494,5 +545,15 @@ const styles = StyleSheet.create({ |
494 | 545 | justifyContent: 'space-around', |
495 | 546 | marginHorizontal: -4, |
496 | 547 | 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' |
497 | 558 | } |
498 | 559 | }); |
0 commit comments