2020-09-07 19:46:37 +02:00
|
|
|
import React, { useState, useRef } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { View, Text, TouchableOpacity, StyleSheet, Dimensions, PixelRatio } from 'react-native';
|
|
|
|
|
|
|
|
import { BlueCurrentTheme } from './themes';
|
|
|
|
|
|
|
|
const BORDER_RADIUS = 30;
|
2020-10-02 11:57:25 +02:00
|
|
|
const PADDINGS = 8;
|
2020-09-20 11:44:04 +02:00
|
|
|
const ICON_MARGIN = 7;
|
2020-09-07 19:46:37 +02:00
|
|
|
|
|
|
|
const cStyles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
position: 'absolute',
|
|
|
|
alignSelf: 'center',
|
|
|
|
height: '6.3%',
|
|
|
|
minHeight: 44,
|
|
|
|
},
|
|
|
|
rootPre: {
|
|
|
|
bottom: -1000,
|
|
|
|
},
|
|
|
|
rootPost: {
|
|
|
|
bottom: 30,
|
|
|
|
borderRadius: BORDER_RADIUS,
|
|
|
|
flexDirection: 'row',
|
2020-09-20 11:24:00 +02:00
|
|
|
overflow: 'hidden',
|
2020-09-07 19:46:37 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const FContainer = ({ children }) => {
|
|
|
|
const [newWidth, setNewWidth] = useState();
|
2020-09-21 12:16:09 +02:00
|
|
|
const layoutCalculated = useRef(false);
|
2020-09-07 19:46:37 +02:00
|
|
|
|
|
|
|
const onLayout = event => {
|
2020-09-21 12:16:09 +02:00
|
|
|
if (layoutCalculated.current) return;
|
2020-10-02 11:57:25 +02:00
|
|
|
const maxWidth = Dimensions.get('window').width - BORDER_RADIUS - 20;
|
2020-09-07 19:46:37 +02:00
|
|
|
const { width } = event.nativeEvent.layout;
|
2020-10-02 11:57:25 +02:00
|
|
|
const withPaddings = Math.ceil(width + PADDINGS * 2);
|
2020-09-21 12:16:09 +02:00
|
|
|
const len = React.Children.toArray(children).filter(Boolean).length;
|
2020-10-02 11:57:25 +02:00
|
|
|
let newWidth = withPaddings * len > maxWidth ? Math.floor(maxWidth / len) : withPaddings;
|
2020-09-21 11:58:21 +02:00
|
|
|
if (len === 1 && newWidth < 90) newWidth = 90; // to add Paddings for lonely small button, like Scan on main screen
|
2020-09-07 19:46:37 +02:00
|
|
|
setNewWidth(newWidth);
|
2020-09-21 12:16:09 +02:00
|
|
|
layoutCalculated.current = true;
|
2020-09-07 19:46:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View onLayout={onLayout} style={[cStyles.root, newWidth ? cStyles.rootPost : cStyles.rootPre]}>
|
|
|
|
{newWidth
|
2020-09-21 12:16:09 +02:00
|
|
|
? React.Children.toArray(children)
|
|
|
|
.filter(Boolean)
|
|
|
|
.map((c, index, array) =>
|
|
|
|
React.cloneElement(c, {
|
|
|
|
width: newWidth,
|
|
|
|
key: index,
|
|
|
|
first: index === 0,
|
|
|
|
last: index === array.length - 1,
|
|
|
|
}),
|
|
|
|
)
|
2020-09-07 19:46:37 +02:00
|
|
|
: children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
FContainer.propTypes = {
|
|
|
|
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.element), PropTypes.element]),
|
|
|
|
};
|
|
|
|
|
|
|
|
const buttonFontSize =
|
|
|
|
PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26) > 22
|
|
|
|
? 22
|
|
|
|
: PixelRatio.roundToNearestPixel(Dimensions.get('window').width / 26);
|
|
|
|
|
|
|
|
const bStyles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
overflow: 'hidden',
|
2020-09-20 11:24:00 +02:00
|
|
|
backgroundColor: BlueCurrentTheme.colors.buttonBackgroundColor,
|
2020-09-07 19:46:37 +02:00
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
color: BlueCurrentTheme.colors.buttonAlternativeTextColor,
|
|
|
|
fontSize: buttonFontSize,
|
|
|
|
fontWeight: '600',
|
2020-09-20 11:24:00 +02:00
|
|
|
marginLeft: ICON_MARGIN,
|
2020-09-07 19:46:37 +02:00
|
|
|
backgroundColor: 'transparent',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const FButton = ({ text, icon, width, first, last, ...props }) => {
|
|
|
|
const style = {};
|
|
|
|
if (width) {
|
2020-10-02 11:57:25 +02:00
|
|
|
const paddingLeft = first ? BORDER_RADIUS / 2 : PADDINGS;
|
|
|
|
const paddingRight = last ? BORDER_RADIUS / 2 : PADDINGS;
|
|
|
|
style.paddingRight = paddingRight;
|
|
|
|
style.paddingLeft = paddingLeft;
|
|
|
|
style.width = width + paddingRight + paddingLeft;
|
2020-09-07 19:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableOpacity style={[bStyles.root, style]} {...props}>
|
|
|
|
<View style={bStyles.icon}>{icon}</View>
|
2020-10-02 11:57:25 +02:00
|
|
|
<Text numberOfLines={1} style={bStyles.text}>
|
|
|
|
{text}
|
|
|
|
</Text>
|
2020-09-07 19:46:37 +02:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
FButton.propTypes = {
|
|
|
|
text: PropTypes.string,
|
|
|
|
icon: PropTypes.element,
|
|
|
|
width: PropTypes.number,
|
|
|
|
first: PropTypes.bool,
|
|
|
|
last: PropTypes.bool,
|
|
|
|
};
|