forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialog.js
152 lines (143 loc) · 4.17 KB
/
Dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* @flow */
import * as React from 'react';
import { StyleSheet, Platform } from 'react-native';
import Modal from '../Modal';
import Surface from '../Surface';
import DialogContent from './DialogContent';
import DialogActions from './DialogActions';
import DialogTitle from './DialogTitle';
import DialogScrollArea from './DialogScrollArea';
import { withTheme } from '../../core/theming';
import type { Theme } from '../../types';
type Props = {|
/**
* Determines whether clicking outside the dialog dismiss it.
*/
dismissable?: boolean,
/**
* Callback that is called when the user dismisses the dialog.
*/
onDismiss: () => mixed,
/**
* Determines Whether the dialog is visible.
*/
visible: boolean,
/**
* Content of the `Dialog`.
*/
children: React.Node,
style?: any,
/**
* @optional
*/
theme: Theme,
|};
/**
* Dialogs inform users about a specific task and may contain critical information, require decisions, or involve multiple tasks.
* To render the `Dialog` above other components, you'll need to wrap it with the [`Portal`](portal.html) component.
*
* <div class="screenshots">
* <img class="medium" src="screenshots/dialog-1.png" />
* <img class="medium" src="screenshots/dialog-2.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { Button, Paragraph, Dialog, Portal } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* visible: false,
* };
*
* _showDialog = () => this.setState({ visible: true });
*
* _hideDialog = () => this.setState({ visible: false });
*
* render() {
* return (
* <View>
* <Button onPress={this._showDialog}>Show Dialog</Button>
* <Portal>
* <Dialog
* visible={this.state.visible}
* onDismiss={this._hideDialog}>
* <Dialog.Title>Alert</Dialog.Title>
* <Dialog.Content>
* <Paragraph>This is simple dialog</Paragraph>
* </Dialog.Content>
* <Dialog.Actions>
* <Button onPress={this._hideDialog}>Done</Button>
* </Dialog.Actions>
* </Dialog>
* </Portal>
* </View>
* );
* }
* }
* ```
*/
class Dialog extends React.Component<Props, void> {
// @component ./DialogContent.js
static Content = DialogContent;
// @component ./DialogActions.js
static Actions = DialogActions;
// @component ./DialogTitle.js
static Title = DialogTitle;
// @component ./DialogScrollArea.js
static ScrollArea = DialogScrollArea;
static defaultProps = {
dismissable: true,
visible: false,
};
render() {
const {
children,
dismissable,
onDismiss,
visible,
style,
theme,
} = this.props;
return (
<Modal dismissable={dismissable} onDismiss={onDismiss} visible={visible}>
<Surface
style={[styles.container, { borderRadius: theme.roundness }, style]}
>
{React.Children.toArray(children)
.filter(child => child != null && typeof child !== 'boolean')
.map((child, i) => {
if (
i === 0 &&
React.isValidElement(child) &&
child.type === DialogContent
) {
// Dialog content is the first item, so we add a top padding
return React.cloneElement(child, {
style: [{ paddingTop: 24 }, child.props.style],
});
}
return child;
})}
</Surface>
</Modal>
);
}
}
const styles = StyleSheet.create({
container: {
/**
* This prevents the shadow from being clipped on Android since Android
* doesn't support `overflow: visible`.
* One downside for this fix is that it will disable clicks on the area
* of the shadow around the dialog, consequently, if you click around the
* dialog (44 pixel from the top and bottom) it won't be dismissed.
*/
marginVertical: Platform.OS === 'android' ? 44 : 0,
marginHorizontal: 26,
elevation: 24,
},
});
export default withTheme(Dialog);