|
1 | 1 | /* @flow */
|
2 | 2 |
|
3 |
| -import React, { Children } from 'react'; |
4 |
| -import { StyleSheet, Platform, Animated } from 'react-native'; |
5 |
| -import Modal from '../Modal'; |
6 |
| -import { black, white } from '../../styles/colors'; |
7 |
| -import Paper from '../Paper'; |
8 |
| -import DialogActions from './Actions'; |
9 |
| -import DialogTitle from './Title'; |
10 |
| -import DialogContent from './Content'; |
11 |
| -import DialogScrollArea from './ScrollArea'; |
12 |
| - |
13 |
| -const AnimatedPaper = Animated.createAnimatedComponent(Paper); |
14 |
| - |
15 |
| -type Props = { |
16 |
| - children?: any, |
17 |
| - /** |
18 |
| - * Determines whether clicking outside the dialog dismiss it, true by default |
19 |
| - */ |
20 |
| - dismissable?: boolean, |
21 |
| - /** |
22 |
| - * Callback that is called when the user dismisses the dialog |
23 |
| - */ |
24 |
| - onRequestClose?: Function, |
25 |
| - style?: any, |
26 |
| - /** |
27 |
| - * Determines Whether the dialog is visible |
28 |
| - */ |
29 |
| - visible: boolean, |
30 |
| -}; |
31 |
| - |
32 |
| -/** |
33 |
| - * Dialogs inform users about a specific task and may contain critical information, require decisions, or involve multiple tasks. |
34 |
| - * |
35 |
| - * ``` |
36 |
| - * export default class MyComponent extends Component { |
37 |
| - * state = { |
38 |
| - * visible: false, |
39 |
| - * }; |
40 |
| - * |
41 |
| - * _showDialog = () => this.setState({ visble: true }); |
42 |
| - * _hideDialog = () => this.setState({ visble: false }); |
43 |
| - * |
44 |
| - * render() { |
45 |
| - * const { visible } = this.state; |
46 |
| - * return ( |
47 |
| - * <View> |
48 |
| - * <Button onPress={this._showDialog}>Show Dialog</Button> |
49 |
| - * <Dialog |
50 |
| - * visible={visible} |
51 |
| - * onRequestClose={this._hideDialog} |
52 |
| - * > |
53 |
| - * <Dialog.Title>Alert</Dialog.Title> |
54 |
| - * <Dialog.Content> |
55 |
| - * <Paragraph>This is simple dialog</Paragraph> |
56 |
| - * </Dialog.Content> |
57 |
| - * <Dialog.Actions> |
58 |
| - * <Button onPress={this._hideDialog}>Done</Button> |
59 |
| - * </Dialog.Actions> |
60 |
| - * </Dialog> |
61 |
| - * </View> |
62 |
| - * ); |
63 |
| - * } |
64 |
| - * } |
65 |
| - * ``` |
66 |
| - */ |
67 |
| - |
68 |
| -const Dialog = (props: Props) => { |
69 |
| - const { children, dismissable, onRequestClose, visible, style } = props; |
70 |
| - const childrenArray = Children.toArray(children); |
71 |
| - const title = childrenArray.find(child => child.type === DialogTitle); |
72 |
| - const actionBtnsChildren = childrenArray.filter( |
73 |
| - child => child.type === DialogActions |
74 |
| - ); |
75 |
| - const restOfChildren = childrenArray.filter( |
76 |
| - child => child.type !== DialogActions && child.type !== DialogTitle |
77 |
| - ); |
78 |
| - let restOfChildrenWithoutTitle = restOfChildren; |
79 |
| - if (!title) { |
80 |
| - let found = false; |
81 |
| - restOfChildrenWithoutTitle = restOfChildren.map(child => { |
82 |
| - if (child.type === DialogContent && !found) { |
83 |
| - found = true; |
84 |
| - return React.cloneElement(child, { |
85 |
| - style: { paddingTop: 24 }, |
86 |
| - }); |
87 |
| - } else { |
88 |
| - return child; |
89 |
| - } |
90 |
| - }); |
91 |
| - } |
92 |
| - return ( |
93 |
| - <Modal |
94 |
| - dismissable={dismissable} |
95 |
| - onRequestClose={onRequestClose} |
96 |
| - visible={visible} |
97 |
| - > |
98 |
| - <AnimatedPaper style={[styles.container, style]} elevation={24}> |
99 |
| - {title} |
100 |
| - {restOfChildrenWithoutTitle} |
101 |
| - {actionBtnsChildren} |
102 |
| - </AnimatedPaper> |
103 |
| - </Modal> |
104 |
| - ); |
105 |
| -}; |
106 |
| - |
107 |
| -Dialog.Actions = DialogActions; |
108 |
| -Dialog.Title = DialogTitle; |
109 |
| -Dialog.Content = DialogContent; |
110 |
| -Dialog.ScrollArea = DialogScrollArea; |
111 |
| - |
112 |
| -Dialog.defaultProps = { |
113 |
| - dismissable: true, |
114 |
| - titleColor: black, |
115 |
| - visible: false, |
116 |
| -}; |
117 |
| - |
118 |
| -export default Dialog; |
119 |
| - |
120 |
| -const styles = StyleSheet.create({ |
121 |
| - container: { |
122 |
| - /** |
123 |
| - * This prevents the shadow from being clipped on Android since Android |
124 |
| - * doesn't support `overflow: visible`. |
125 |
| - * One downside for this fix is that it will disable clicks on the area |
126 |
| - * of the shadow around the dialog, consequently, if you click around the |
127 |
| - * dialog (44 pixel from the top and bottom) it won't be dismissed. |
128 |
| - */ |
129 |
| - marginVertical: Platform.OS === 'android' ? 44 : 0, |
130 |
| - marginHorizontal: 26, |
131 |
| - borderRadius: 2, |
132 |
| - backgroundColor: white, |
133 |
| - }, |
134 |
| -}); |
| 3 | +export { default } from './Dialog'; |
0 commit comments