|
| 1 | +import React from 'react'; |
| 2 | +import { View, Dimensions, StyleSheet, Text, TouchableOpacity, Modal, ModalProps, Animated } from 'react-native'; |
| 3 | +import Item from './item' |
| 4 | +export { default as ActionSheetItem } from './item'; |
| 5 | + |
| 6 | +let MainWidth = Dimensions.get('window').width; |
| 7 | +let MainHeight = Dimensions.get('window').height; |
| 8 | +export interface ActionSheetProps extends ModalProps { |
| 9 | + /** 点击蒙层是否关闭 */ |
| 10 | + onCancel?: Boolean, |
| 11 | + /** 取消的文本 */ |
| 12 | + cancelText?: React.ReactNode |
| 13 | +} |
| 14 | + |
| 15 | +interface ActionSheetState { |
| 16 | + animatedHeight: number, |
| 17 | + stateVisible: boolean |
| 18 | +} |
| 19 | + |
| 20 | +export default class ActionSheet extends React.Component<ActionSheetProps, ActionSheetState> { |
| 21 | + private fadeAnim: Animated.Value = new Animated.Value(0); |
| 22 | + private animatedRef: React.RefObject<View> = React.createRef() |
| 23 | + constructor(props: ActionSheetProps) { |
| 24 | + super(props) |
| 25 | + this.state = { |
| 26 | + animatedHeight: 0, |
| 27 | + stateVisible: false |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + onClose = () => { |
| 32 | + Animated.timing(this.fadeAnim, { |
| 33 | + toValue: 0, |
| 34 | + duration: 150, |
| 35 | + useNativeDriver: true, |
| 36 | + }).start(({ finished }) => { |
| 37 | + this.setState({ stateVisible: false }) |
| 38 | + }); |
| 39 | + } |
| 40 | + UNSAFE_componentWillReceiveProps(nextProps: ActionSheetProps) { |
| 41 | + if (nextProps.visible) { |
| 42 | + this.setState({ stateVisible: true }) |
| 43 | + Animated.timing(this.fadeAnim, { |
| 44 | + toValue: 0, |
| 45 | + duration: 0, |
| 46 | + useNativeDriver: true, |
| 47 | + }).start(({ finished }) => { |
| 48 | + this.animatedRef.current && |
| 49 | + this.animatedRef.current.measure((_frameOffsetX, _frameOffsetY, _width, _height, pageOffsetX, pageOffsetY) => { |
| 50 | + this.setState({ animatedHeight: _height }, () => { |
| 51 | + Animated.timing(this.fadeAnim, { |
| 52 | + toValue: -_height, |
| 53 | + duration: 150, |
| 54 | + useNativeDriver: true, |
| 55 | + }).start(); |
| 56 | + }) |
| 57 | + }) |
| 58 | + }); |
| 59 | + }else { |
| 60 | + this.onClose() |
| 61 | + } |
| 62 | + } |
| 63 | + render() { |
| 64 | + const { children, visible, cancelText = '取消', onCancel, ...other } = this.props |
| 65 | + const { stateVisible } = this.state |
| 66 | + if(!stateVisible) { |
| 67 | + return null |
| 68 | + } |
| 69 | + return ( |
| 70 | + <Modal |
| 71 | + animationType="fade" // slide none fade |
| 72 | + transparent={true} |
| 73 | + visible={stateVisible} |
| 74 | + onRequestClose={this.onClose} |
| 75 | + {...other} |
| 76 | + > |
| 77 | + |
| 78 | + <TouchableOpacity activeOpacity={1} style={[styles.position, styles.spread]} onPress={()=>onCancel&&this.onClose()}> |
| 79 | + <Animated.View style={[styles.spread, styles.backdrop]}> |
| 80 | + </Animated.View> |
| 81 | + </TouchableOpacity> |
| 82 | + <Animated.View |
| 83 | + style={[ |
| 84 | + styles.actionSheet, |
| 85 | + { bottom: -this.state.animatedHeight, transform: [{ translateY: this.fadeAnim }] } |
| 86 | + ]} |
| 87 | + ref={this.animatedRef} |
| 88 | + > |
| 89 | + { |
| 90 | + React.Children.toArray(children).map((item, index) => (<View key={index}> |
| 91 | + {index !== 0 && <View style={styles.actionSheetItemDivider} />}{item} |
| 92 | + </View>)) |
| 93 | + } |
| 94 | + <View style={styles.divider} /> |
| 95 | + {typeof cancelText !== 'object' ? <TouchableOpacity activeOpacity={1} onPress={this.onClose}> |
| 96 | + <View style={styles.actionSheetItem}> |
| 97 | + <Text style={styles.actionSheetItemText}>{cancelText}</Text> |
| 98 | + </View> |
| 99 | + </TouchableOpacity> : <View>{cancelText}</View> |
| 100 | + } |
| 101 | + </Animated.View> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +const styles = StyleSheet.create({ |
| 108 | + position: { |
| 109 | + position: 'absolute', |
| 110 | + backgroundColor: 'transparent', |
| 111 | + top: 0, |
| 112 | + bottom: 0, |
| 113 | + left: 0, |
| 114 | + right: 0, |
| 115 | + zIndex: 9998, |
| 116 | + }, |
| 117 | + backdrop: { |
| 118 | + backgroundColor: '#000', |
| 119 | + opacity: .2 |
| 120 | + }, |
| 121 | + spread: { |
| 122 | + width: MainWidth, |
| 123 | + height: MainHeight |
| 124 | + }, |
| 125 | + actionSheet: { |
| 126 | + width: MainWidth, |
| 127 | + position: 'absolute', |
| 128 | + left: 0, |
| 129 | + right: 0, |
| 130 | + backgroundColor: '#fff', |
| 131 | + zIndex: 9999 |
| 132 | + }, |
| 133 | + divider: { |
| 134 | + backgroundColor: 'rgba(197,217,232,.3)', |
| 135 | + width: MainWidth, |
| 136 | + height: 6 |
| 137 | + }, |
| 138 | + actionSheetItemDivider: { |
| 139 | + borderBottomColor: 'rgba(197,217,232,.3)', |
| 140 | + borderBottomWidth: 2, |
| 141 | + width: MainWidth, |
| 142 | + }, |
| 143 | + actionSheetItem: { |
| 144 | + width: MainWidth, |
| 145 | + height: 50, |
| 146 | + justifyContent: 'center', |
| 147 | + alignItems: 'center', |
| 148 | + }, |
| 149 | + actionSheetItemText: { |
| 150 | + fontSize: 20, |
| 151 | + fontWeight: '400', |
| 152 | + } |
| 153 | +}); |
0 commit comments