|
| 1 | +import * as React from 'react' |
| 2 | +import { TouchableHighlight, View } from 'react-native' |
| 3 | + |
| 4 | +import { useHandler } from '../../hooks/useHandler' |
| 5 | +import { triggerHaptic } from '../../util/haptic' |
| 6 | +import { showError } from '../services/AirshipInstance' |
| 7 | +import { cacheStyles, Theme, useTheme } from '../services/ThemeContext' |
| 8 | +import { SectionView } from './SectionView' |
| 9 | + |
| 10 | +export type CardType = 'default' | 'warning' | 'error' |
| 11 | + |
| 12 | +interface Props { |
| 13 | + children: React.ReactNode | React.ReactNode[] |
| 14 | + icon?: React.ReactNode |
| 15 | + onLongPress?: () => Promise<void> | void |
| 16 | + onPress?: () => Promise<void> | void |
| 17 | + // cardType?: CardType // TODO |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Rounded card that automatically adds horizontal dividers between each child, |
| 22 | + * aligned in a column layout. Adds no dividers if only one child is given. |
| 23 | + */ |
| 24 | +export const CardUi4 = (props: Props) => { |
| 25 | + const { children, icon, onLongPress, onPress } = props |
| 26 | + const theme = useTheme() |
| 27 | + const styles = getStyles(theme) |
| 28 | + |
| 29 | + const handlePress = useHandler(async () => { |
| 30 | + if (onPress != null) { |
| 31 | + triggerHaptic('impactLight') |
| 32 | + try { |
| 33 | + await onPress() |
| 34 | + } catch (err) { |
| 35 | + showError(err) |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + const handleLongPress = useHandler(async () => { |
| 41 | + if (onLongPress != null) { |
| 42 | + triggerHaptic('impactLight') |
| 43 | + try { |
| 44 | + await onLongPress() |
| 45 | + } catch (err) { |
| 46 | + showError(err) |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + return ( |
| 52 | + <TouchableHighlight |
| 53 | + accessible={false} |
| 54 | + onPress={handlePress} |
| 55 | + onLongPress={handleLongPress} |
| 56 | + disabled={handlePress == null && handleLongPress == null} |
| 57 | + underlayColor={theme.touchHighlightUi4} |
| 58 | + style={styles.cardContainer} |
| 59 | + > |
| 60 | + <> |
| 61 | + {icon == null ? null : <View style={styles.iconContainer}>{icon}</View>} |
| 62 | + <SectionView>{children}</SectionView> |
| 63 | + </> |
| 64 | + </TouchableHighlight> |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +// TODO: Adjust margin/padding so everything combines with correct layout no |
| 69 | +// matter the combination of UI4 components. |
| 70 | +const getStyles = cacheStyles((theme: Theme) => ({ |
| 71 | + cardContainer: { |
| 72 | + backgroundColor: theme.cardBackgroundUi4, |
| 73 | + borderRadius: theme.rem(theme.cardRadiusRemUi4), |
| 74 | + margin: theme.rem(0.5), |
| 75 | + padding: theme.rem(0.5), |
| 76 | + flexDirection: 'row' |
| 77 | + }, |
| 78 | + iconContainer: { |
| 79 | + margin: theme.rem(0.25), |
| 80 | + justifyContent: 'center', |
| 81 | + alignContent: 'center' |
| 82 | + }, |
| 83 | + warning: { |
| 84 | + borderColor: theme.warningIcon |
| 85 | + } |
| 86 | +})) |
0 commit comments