import React from 'react'; import { View, ScrollView, Text, TouchableOpacity, Dimensions, SafeAreaView, } from 'react-native'; import {SlideModal, SlideModalProps} from '../SlideModal'; import actionsheetStyles from './styles'; export {actionsheetStyles}; const window = Dimensions.get('window'); interface DataItem { label: string; [propName: string]: any; } interface ActionsheetProps extends SlideModalProps { header?: any; footer?: any; data?: DataItem[] | any; maxShowNum?: number | null | undefined; renderItem?: Function; onPressCancel?: Function; onPressConfirm?: Function; useSafeAreaView?: boolean; } export class Actionsheet extends SlideModal { static defaultProps = { ...SlideModal.defaultProps, cancelable: true, maxShowNum: null, header: '标题', footer: '取消', useSafeAreaView: true, data: [], renderItem: null, onPressCancel: null, onPressConfirm: null, }; constructor(props) { super(props); } getHeader() { const styles = actionsheetStyles; const {header} = this.props; return React.isValidElement(header) ? ( header ) : ( {header} ); } getBody() { const {data, maxShowNum, renderItem} = this.props; const styles = actionsheetStyles; return ( {data.map((item, index) => { const tmpStyle = index === data.length - 1 ? {borderBottomWidth: 0} : {}; return ( { this.handlePress('confirm', item, index); }}> {renderItem ? ( renderItem(item, index) ) : ( {typeof item === 'object' ? item.label : item} )} ); })} ); } handlePress(type: string, item?, index?) { const callbackName = 'onPress' + type.slice(0, 1).toUpperCase() + type.slice(1); this.close() .then(() => { this.props[callbackName] && this.props[callbackName](item, index); }) .catch((e) => { console.log(e); }); } getFooter() { const {footer} = this.props; const styles = actionsheetStyles; return ( { this.handlePress('cancel'); }}> {footer && React.isValidElement(footer) ? ( footer ) : ( {footer} )} ); } getContent() { const styles = actionsheetStyles; const inner = ( {this.getHeader()} {this.getBody()} {this.getFooter()} {this.props.useSafeAreaView ? ( { // const { height } = e.nativeEvent.layout // console.log('Actionsheet SafeAreaView height: ', height) }}> ) : null} ); return SlideModal.prototype.getContent.call(this, inner); } }