|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; |
| 12 | + |
| 13 | +import * as React from 'react'; |
| 14 | +import {Modal, Pressable, StyleSheet, Text, View} from 'react-native'; |
| 15 | + |
| 16 | +function ModalOnShowOnDismiss(): React.Node { |
| 17 | + const [modalShowComponent, setModalShowComponent] = React.useState(true); |
| 18 | + const [modalVisible, setModalVisible] = React.useState(false); |
| 19 | + const [onShowCount, setOnShowCount] = React.useState(0); |
| 20 | + const [onDismissCount, setOnDismissCount] = React.useState(0); |
| 21 | + |
| 22 | + return ( |
| 23 | + <View style={styles.container}> |
| 24 | + {modalShowComponent && ( |
| 25 | + <Modal |
| 26 | + animationType="slide" |
| 27 | + transparent={true} |
| 28 | + visible={modalVisible} |
| 29 | + onShow={() => { |
| 30 | + setOnShowCount(onShowCount + 1); |
| 31 | + }} |
| 32 | + onDismiss={() => { |
| 33 | + setOnDismissCount(onDismissCount + 1); |
| 34 | + }} |
| 35 | + onRequestClose={() => { |
| 36 | + setModalVisible(false); |
| 37 | + }}> |
| 38 | + <View |
| 39 | + style={[ |
| 40 | + styles.centeredView, |
| 41 | + styles.modalBackdrop, |
| 42 | + styles.widthHeight, |
| 43 | + ]}> |
| 44 | + <View style={styles.modalView}> |
| 45 | + <Text testID="modal-on-show-count"> |
| 46 | + onShow is called {onShowCount} times |
| 47 | + </Text> |
| 48 | + <Text testID="modal-on-dismiss-count"> |
| 49 | + onDismiss is called {onDismissCount} times |
| 50 | + </Text> |
| 51 | + <Pressable |
| 52 | + style={[styles.button, styles.buttonClose]} |
| 53 | + onPress={() => setModalVisible(false)}> |
| 54 | + <Text testID="dismiss-modal" style={styles.textStyle}> |
| 55 | + Hide modal by setting visible to false |
| 56 | + </Text> |
| 57 | + </Pressable> |
| 58 | + <Pressable |
| 59 | + style={[styles.button, styles.buttonClose]} |
| 60 | + onPress={() => setModalShowComponent(false)}> |
| 61 | + <Text |
| 62 | + testID="dismiss-modal-by-removing-component" |
| 63 | + style={styles.textStyle}> |
| 64 | + Hide modal by removing component |
| 65 | + </Text> |
| 66 | + </Pressable> |
| 67 | + </View> |
| 68 | + </View> |
| 69 | + </Modal> |
| 70 | + )} |
| 71 | + <Text testID="on-show-count">onShow is called {onShowCount} times</Text> |
| 72 | + <Text testID="on-dismiss-count"> |
| 73 | + onDismiss is called {onDismissCount} times |
| 74 | + </Text> |
| 75 | + <Pressable |
| 76 | + style={[styles.button, styles.buttonOpen]} |
| 77 | + onPress={() => { |
| 78 | + setModalShowComponent(true); |
| 79 | + setModalVisible(true); |
| 80 | + }}> |
| 81 | + <Text testID="open-modal" style={styles.textStyle}> |
| 82 | + Show Modal |
| 83 | + </Text> |
| 84 | + </Pressable> |
| 85 | + </View> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +const styles = StyleSheet.create({ |
| 90 | + container: { |
| 91 | + display: 'flex', |
| 92 | + alignItems: 'center', |
| 93 | + paddingVertical: 30, |
| 94 | + }, |
| 95 | + centeredView: { |
| 96 | + // flex: 1, [Windows] |
| 97 | + justifyContent: 'center', |
| 98 | + alignItems: 'center', |
| 99 | + }, |
| 100 | + modalBackdrop: { |
| 101 | + backgroundColor: 'rgba(0, 0, 0, 0.5)', |
| 102 | + }, |
| 103 | + modalView: { |
| 104 | + margin: 20, |
| 105 | + backgroundColor: 'white', |
| 106 | + borderRadius: 20, |
| 107 | + padding: 35, |
| 108 | + alignItems: 'center', |
| 109 | + shadowColor: '#000', |
| 110 | + shadowOffset: { |
| 111 | + width: 0, |
| 112 | + height: 2, |
| 113 | + }, |
| 114 | + shadowOpacity: 0.25, |
| 115 | + shadowRadius: 4, |
| 116 | + elevation: 5, |
| 117 | + }, |
| 118 | + button: { |
| 119 | + borderRadius: 20, |
| 120 | + padding: 10, |
| 121 | + marginVertical: 20, |
| 122 | + elevation: 2, |
| 123 | + }, |
| 124 | + buttonOpen: { |
| 125 | + backgroundColor: '#F194FF', |
| 126 | + }, |
| 127 | + buttonClose: { |
| 128 | + backgroundColor: '#2196F3', |
| 129 | + }, |
| 130 | + textStyle: { |
| 131 | + color: 'white', |
| 132 | + fontWeight: 'bold', |
| 133 | + textAlign: 'center', |
| 134 | + }, |
| 135 | + // [Windows |
| 136 | + widthHeight: { |
| 137 | + width: 300, |
| 138 | + height: 400, |
| 139 | + }, |
| 140 | + // Windows] |
| 141 | +}); |
| 142 | + |
| 143 | +export default ({ |
| 144 | + title: "Modal's onShow/onDismiss", |
| 145 | + name: 'onShow', |
| 146 | + description: |
| 147 | + 'onShow and onDismiss (iOS only) callbacks are called when a modal is shown/dismissed', |
| 148 | + render: (): React.Node => <ModalOnShowOnDismiss />, |
| 149 | +}: RNTesterModuleExample); |
0 commit comments