|
| 1 | +import _ from 'lodash'; |
| 2 | +import React, { ReactNode, useMemo } from 'react'; |
| 3 | +import { |
| 4 | + StyleSheet, |
| 5 | + ViewStyle, |
| 6 | + View, |
| 7 | + ScrollView, |
| 8 | + TouchableOpacity, |
| 9 | + Text, |
| 10 | + TextStyle, |
| 11 | + SafeAreaView, |
| 12 | + Platform, |
| 13 | +} from 'react-native'; |
| 14 | + |
| 15 | +export type ActionBarProps = { |
| 16 | + height?: number; |
| 17 | + backgroundColor?: string; |
| 18 | + actions: Array<{ |
| 19 | + label?: string; |
| 20 | + onPress?: () => void; |
| 21 | + fontStyle?: TextStyle; |
| 22 | + render?: ReactNode; |
| 23 | + }>; |
| 24 | + keepAbsoulte?: boolean; |
| 25 | + style?: ViewStyle; |
| 26 | + scroll?: boolean; |
| 27 | + useSafeArea?: boolean; |
| 28 | +}; |
| 29 | + |
| 30 | +const Container = (props: { scroll: boolean; children: ReactNode; useSafeArea: boolean; style: any }) => { |
| 31 | + const { scroll, children, useSafeArea, ...others } = props; |
| 32 | + const renderDom = useMemo(() => { |
| 33 | + if (scroll) { |
| 34 | + return ( |
| 35 | + <ScrollView horizontal={true} showsHorizontalScrollIndicator={false} {...others}> |
| 36 | + {children} |
| 37 | + </ScrollView> |
| 38 | + ); |
| 39 | + } |
| 40 | + if (!scroll && useSafeArea && Platform.OS === 'ios') { |
| 41 | + return <SafeAreaView {...others}>{children}</SafeAreaView>; |
| 42 | + } |
| 43 | + return <View {...others}>{children}</View>; |
| 44 | + }, [scroll, useSafeArea, children]); |
| 45 | + return renderDom; |
| 46 | +}; |
| 47 | + |
| 48 | +function ActionBar(props: ActionBarProps) { |
| 49 | + const { |
| 50 | + actions, |
| 51 | + style, |
| 52 | + keepAbsoulte = true, |
| 53 | + height = 48, |
| 54 | + backgroundColor = '#fff', |
| 55 | + scroll = true, |
| 56 | + useSafeArea = true, |
| 57 | + } = props; |
| 58 | + |
| 59 | + const styles = createStyles({ height: height, backgroundColor: backgroundColor }); |
| 60 | + |
| 61 | + // 按钮长度 |
| 62 | + const actionLeg = actions.length || 0; |
| 63 | + |
| 64 | + // 是否是数组最后一个 |
| 65 | + const last = (i: number) => actionLeg - 1 === i; |
| 66 | + |
| 67 | + return ( |
| 68 | + <Container scroll={scroll} useSafeArea={useSafeArea} style={[keepAbsoulte && styles.absoluteContainer]}> |
| 69 | + <View style={[styles.container, { justifyContent: 'space-between', ...style }]}> |
| 70 | + {_.map(actions, ({ label = '', onPress, render, fontStyle }, i) => { |
| 71 | + return ( |
| 72 | + <TouchableOpacity key={i} onPress={onPress && onPress} style={{ marginRight: scroll && !last(i) ? 20 : 0 }}> |
| 73 | + {render ? render : <Text style={{ ...fontStyle }}>{label}</Text>} |
| 74 | + </TouchableOpacity> |
| 75 | + ); |
| 76 | + })} |
| 77 | + </View> |
| 78 | + </Container> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +function createStyles({ height, backgroundColor }: any) { |
| 83 | + return StyleSheet.create({ |
| 84 | + container: { |
| 85 | + height, |
| 86 | + paddingHorizontal: 20, |
| 87 | + flexDirection: 'row', |
| 88 | + alignItems: 'center', |
| 89 | + }, |
| 90 | + absoluteContainer: { |
| 91 | + ...StyleSheet.absoluteFillObject, |
| 92 | + top: undefined, |
| 93 | + backgroundColor, |
| 94 | + shadowColor: '#43515C', |
| 95 | + shadowOpacity: 0.06, |
| 96 | + shadowRadius: 18.5, |
| 97 | + }, |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +export default ActionBar; |
0 commit comments