|
| 1 | +/** |
| 2 | + * Created by leon<[email protected]> on 22/3/10. |
| 3 | + */ |
| 4 | +import { Button } from '@src/components' |
| 5 | +import { translate } from '@src/i18n' |
| 6 | +import { SylCommon, useTheme } from '@src/theme' |
| 7 | +import { ITheme } from '@src/types' |
| 8 | +import React, { useRef } from 'react' |
| 9 | +import { Text, TextStyle, View, ViewStyle } from 'react-native' |
| 10 | +import ActionSheet, { ActionSheetRef, SheetManager, SheetProps } from 'react-native-actions-sheet' |
| 11 | + |
| 12 | +/* usage: |
| 13 | + SheetManager.show('confirm-sheet', { |
| 14 | + onClose: (data: any) => { |
| 15 | + console.log('onClose', data) |
| 16 | + showToast(data) |
| 17 | + }, |
| 18 | + payload: { |
| 19 | + title: '请确认', |
| 20 | + description: '确定进行此操作吗?', |
| 21 | + confirmText: '确定', |
| 22 | + cancelText: '取消' |
| 23 | + } |
| 24 | + }) |
| 25 | +**/ |
| 26 | + |
| 27 | +const ConfirmActionSheet = (props: SheetProps) => { |
| 28 | + const { theme } = useTheme() |
| 29 | + const { |
| 30 | + sheetId, |
| 31 | + payload: { |
| 32 | + title = translate('brand.name'), |
| 33 | + description, |
| 34 | + confirmText = translate('common.confirm'), |
| 35 | + cancelText = translate('common.cancel') |
| 36 | + } |
| 37 | + } = props |
| 38 | + const actionSheetRef = useRef<ActionSheetRef>(null) |
| 39 | + |
| 40 | + const buttonConfirm = (yes: boolean) => { |
| 41 | + console.log('buttonConfirm', yes, sheetId) |
| 42 | + SheetManager.hide(sheetId, { |
| 43 | + payload: yes, |
| 44 | + context: 'global' |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <ActionSheet |
| 50 | + id={sheetId} |
| 51 | + springOffset={1} |
| 52 | + onBeforeShow={(data) => console.log(data)} |
| 53 | + ref={actionSheetRef} |
| 54 | + statusBarTranslucent |
| 55 | + drawUnderStatusBar={true} |
| 56 | + gestureEnabled={true} |
| 57 | + defaultOverlayOpacity={0.5} |
| 58 | + containerStyle={{ |
| 59 | + paddingTop: theme.spacing.medium, |
| 60 | + backgroundColor: theme.colors.surface, |
| 61 | + borderTopLeftRadius: 20, |
| 62 | + borderTopRightRadius: 20 |
| 63 | + }}> |
| 64 | + <View style={[styles.container(theme), SylCommon.Card.container(theme)]}> |
| 65 | + {title && <Text style={styles.title(theme)}>{title}</Text>} |
| 66 | + {description && <Text style={styles.text(theme)}>{description}</Text>} |
| 67 | + <View style={styles.buttonContainer(theme)}> |
| 68 | + <Button |
| 69 | + type="large" |
| 70 | + onPress={() => buttonConfirm(false)} |
| 71 | + textColor={theme.colors.titleText} |
| 72 | + style={[ |
| 73 | + styles.button(theme), |
| 74 | + { |
| 75 | + backgroundColor: theme.colors.transparent |
| 76 | + } |
| 77 | + ]}> |
| 78 | + {cancelText || translate('common.cancel')} |
| 79 | + </Button> |
| 80 | + <Button type="large" onPress={() => buttonConfirm(true)} style={styles.button(theme)}> |
| 81 | + {confirmText || translate('common.confirm')} |
| 82 | + </Button> |
| 83 | + </View> |
| 84 | + </View> |
| 85 | + </ActionSheet> |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +const styles = { |
| 90 | + safeareview: (theme: ITheme): ViewStyle => ({}), |
| 91 | + container: (theme: ITheme): ViewStyle => ({ |
| 92 | + paddingBottom: theme.spacing.extraLarge, |
| 93 | + width: '100%', |
| 94 | + display: 'flex', |
| 95 | + flexDirection: 'column', |
| 96 | + justifyContent: 'flex-start', |
| 97 | + alignItems: 'center', |
| 98 | + backgroundColor: theme.colors.surface |
| 99 | + }), |
| 100 | + title: (theme: ITheme): TextStyle => ({ |
| 101 | + ...theme.typography.headingTextBold, |
| 102 | + paddingVertical: theme.spacing.small |
| 103 | + }), |
| 104 | + text: (theme: ITheme): TextStyle => ({ |
| 105 | + ...theme.typography.labelText, |
| 106 | + width: '100%', |
| 107 | + textAlign: 'left', |
| 108 | + paddingVertical: theme.spacing.small |
| 109 | + }), |
| 110 | + buttonContainer: (theme: ITheme): ViewStyle => ({ |
| 111 | + paddingVertical: theme.spacing.small, |
| 112 | + flexDirection: 'row', |
| 113 | + display: 'flex', |
| 114 | + width: '100%', |
| 115 | + justifyContent: 'space-between' |
| 116 | + }), |
| 117 | + button: (theme: ITheme): ViewStyle => ({ |
| 118 | + width: '48%', |
| 119 | + borderRadius: 20 |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +export default ConfirmActionSheet |
0 commit comments