import React, { Component } from 'react' import { View, Text, StyleSheet, TouchableOpacity, Image, ScrollView, DeviceEventEmitter, Keyboard, ActionSheetIOS } from 'react-native'; import BottomSheet from 'reanimated-bottom-sheet' import Animated from 'react-native-reanimated'; import { px2dp, height, size, onePx, colors, toast, isIos } from './../../utils' // import redux from './../../redux/store'; import { BjTypeSheetOption } from './../../utils/types'; interface Props { options: Array; isInTab?: boolean; onChange: Function; themeColor?: string; headerText?: string; disableReason?: string; onShow?: Function onHide?: Function }; export default class ActionSheetPanel extends Component { constructor(props: Props) { super(props); // const state = redux.getState(); this.state = { height: height, }; this.renderInner = this.renderInner.bind(this) this._onCloseEnd = this._onCloseEnd.bind(this); }; public actionSheetHeaderHeight = px2dp(60); public decButtomOutLineWidth = px2dp(8) public sheetContainerRef = null; public sheetRef = null; public delayShow = null; public fall = new Animated.Value(1); public delayTimer = null; public delayHideTimer = null; componentWillUnmount() { this.delayTimer && clearTimeout(this.delayTimer); this.delayHideTimer && clearTimeout(this.delayHideTimer); this.setState = (state, callback) => { return; } }; public handleHide = () => { // console.log("handleHide") this.sheetRef.snapTo(0); this.sheetRef.snapTo(0); this.delayHideTimer = setTimeout(() => { this._onCloseEnd(); }, 250); }; public show = () => { //打开下拉框 Keyboard.dismiss(); const { onShow, options, headerText, onChange, onHide } = this.props; onShow && onShow(); if(isIos()){ const iosOptions = options.map(option => { return option.name; }) iosOptions.push("取消"); ActionSheetIOS.showActionSheetWithOptions( { title: headerText, options: iosOptions, cancelButtonIndex: iosOptions.length - 1, // tintColor: colors.redColor }, buttonIndex => { if(buttonIndex !== iosOptions.length - 1){ onChange(options[buttonIndex]); onHide && onHide(); } } ) }else { this.delayHideTimer && clearTimeout(this.delayHideTimer); this.sheetContainerRef.setNativeProps({ style: { display: 'flex', zIndex: 9999, width: '100%', height: '100%' } }); this.delayTimer = setTimeout(() => { this.sheetRef.snapTo(1); this.sheetRef.snapTo(1); // DeviceEventEmitter.emit('app_keyboard_did_show'); }, 50); } }; public _onCloseEnd = () => { const { onHide } = this.props; onHide && onHide(); // console.log("_onCloseEnd") this.sheetContainerRef.setNativeProps({ style: { display: 'none', zIndex: -1, width: 0, height: 0, } }); // DeviceEventEmitter.emit('app_keyboard_did_hide'); }; calculatSheetHeight() { const { isInTab = true, options = [{ name: '暂无数据', key: 1 }] } = this.props; //null safety //@ts-ignore const { height } = this.state; let maxHeight = height * 0.9 if (isInTab) { maxHeight = height * 0.9 - size.tab_height; } let contentHeight = this.actionSheetHeaderHeight + options.length * px2dp(53) + this.decButtomOutLineWidth + px2dp(60) if (contentHeight > maxHeight) { let callBackData = { sheetHeight: maxHeight, dargable: false } return callBackData } else { let callBackData = { sheetHeight: contentHeight, dargable: true } // console.log('callBackData', callBackData) return callBackData }; }; handleChange = (option, index) => { this.handleHide(); const { onChange, disableReason } = this.props; if (option.disable) { return toast(disableReason); } this.delayTimer = setTimeout(() => { onChange && onChange(option, index); }, 100); DeviceEventEmitter.emit('app_keyboard_did_hide'); }; public render_option = (option: BjTypeSheetOption, index: number, len: number) => { const { onChange, themeColor = '#333' } = this.props; return ( this.handleChange(option, index)} style={[styles.optionLine, index === len - 1 ? { borderBottomColor: '#fff' } : null]} key={index + ''}> {option.name} ) }; renderInner() { const { options, headerText } = this.props; const { sheetHeight, dargable } = this.calculatSheetHeight(); return ( {headerText ? headerText : '请选择'} { dargable ? options && options.map((option, index) => this.render_option(option, index, options.length)) : {options && options.map((option, index) => this.render_option(option, index, options.length))} } this.handleHide()} style={styles.decBtn}> 取消 ) }; render() { const { isInTab = true, options = [{ name: '暂无数据', key: 1 }] } = this.props; //null safety const { sheetHeight, dargable } = this.calculatSheetHeight(); if(isIos()){ return <> } return ( this.sheetContainerRef = c} style={styles.sheetContainer}> {/* this.handleHide()} style={styles.touchOverlayContent}> */} { this.sheetRef = c }} borderRadius={px2dp(12)} onCloseEnd={this._onCloseEnd} /> ) } }; const styles = StyleSheet.create({ sheetContainer: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, height: 0, width: 0, display: 'none', zIndex: -1, }, touchOverlayContent: { width: '100%', height: '100%', }, overlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, width: '100%', height: '100%', backgroundColor: 'rgba(0,0,0,0.7)', // zIndex: 0 }, sheetheader: { width: '100%', height: px2dp(60), borderBottomColor: '#d2d2d2', borderBottomWidth: onePx, justifyContent: 'center', alignItems: 'center' }, decBtn: { width: '100%', height: px2dp(60), justifyContent: 'center', alignItems: 'center', borderTopColor: "#d2d2d2", // backgroundColor: 'red' }, optionLine: { width: '100%', height: px2dp(52), justifyContent: 'center', alignItems: 'center', borderBottomColor: '#d2d2d2', borderBottomWidth: onePx, marginBottom: 1 } })