forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.tsx
116 lines (105 loc) · 3.35 KB
/
Modal.tsx
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
import * as React from 'react';
import { View, Text, Modal, TouchableWithoutFeedback, TouchableOpacity } from 'react-native';
import modalStyle from './style/index';
import ModalPropsType from './ModalPropsType';
class AntmModal extends React.Component<ModalPropsType, any> {
static defaultProps = {
visible: false,
closable: false,
maskClosable: false,
transparent: false,
animated: true,
style: {},
onClose() {},
onShow() {},
footer: [],
};
constructor(props) {
super(props);
this.state = {
visible: props.visible || false,
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.visible !== this.state.visible) {
this.setState({
visible: nextProps.visible,
});
}
}
onRequestClose = () => {
this.setState({
visible: false,
});
};
onClosePress = () => {
this.props.onClose();
this.setState({
visible: false,
});
}
render() {
const {
title, closable, maskClosable, footer, animated, onShow, transparent, children, style,
onRequestClose,
} = this.props;
let showModal = this.state.visible;
const animationType: 'fade' | 'slide' | 'none' = animated ? (transparent ? 'fade' : 'slide') : 'none';
const innerStyle = transparent ? {backgroundColor: 'white'} : {backgroundColor: 'transparent'};
const btnGroupStyle = footer.length === 2 ? modalStyle.buttnGroupH : modalStyle.buttnGroupV;
const buttonWrapStyle = footer.length === 2 ? modalStyle.buttnWrapH : modalStyle.buttnWrapV;
const footerDom = footer.length ? (
<View style={[btnGroupStyle]}>
{
footer.map((button: any, i) => {
return (
<View key={i} style={[buttonWrapStyle]}>
<TouchableOpacity onPress={() => {
if (button.onPress) {
button.onPress();
}
this.onClosePress();
}}>
<Text style={[modalStyle.buttonText]}>{button.text || `按钮${i}`}</Text>
</TouchableOpacity>
</View>
);
})
}
</View>
) : null;
return (
<Modal
animationType={animationType}
onRequestClose={onRequestClose || this.onRequestClose}
onShow={onShow}
transparent={transparent}
visible={showModal}
>
{ transparent ? (
<View style={[modalStyle.container]}>
{maskClosable ? <TouchableWithoutFeedback onPress={this.onClosePress}>
<View style={[modalStyle.maskClosable]}></View>
</TouchableWithoutFeedback> : null}
<View style={[modalStyle.innerContainer, innerStyle, style]}>
{title ? <Text style={[modalStyle.header]}>{title}</Text> : null}
<View style={modalStyle.body}>{children}</View>
{footer ? <View>{footerDom}</View> : null}
{closable ? <TouchableWithoutFeedback onPress={this.onClosePress}>
<View style={[modalStyle.closeWrap]}>
<Text style={[modalStyle.close]}>×</Text>
</View>
</TouchableWithoutFeedback> : null}
</View>
</View>
) : (
<View style={style}>
{children}
</View>
)
}
</Modal>
);
}
}
export default AntmModal;