import React, { useState } from 'react';
import {
View,
Text,
FlatList,
TouchableOpacity,
StyleSheet,
} from 'react-native';
import Icon from 'react-native-vector-icons/Entypo';
import { TableProps, TableItemProps } from './types';
import { Fonts } from '../../styles';
import { useColors } from '../../themes';
const Table = (props: TableProps) => {
const [active, setActive] = useState(0);
const {
customLeftItemComponents,
customRightItemComponents,
data,
flatlistKey,
leftKey,
leftKeyOnPress,
leftItemTextStyle,
option,
optionTextStyle,
optionLabel,
rightKeyOnPress,
rightItemTextStyle,
title,
titleTextStyle,
topContainerStyle,
itemSeparator,
tableItemStyle,
rightOptionIconName,
} = props;
const Colors = useColors();
const TableItem = (itemProps: TableItemProps) => {
const { item } = itemProps;
return (
{renderLeftOptionItem(item)}
{renderRightOptionItem(item)}
);
};
// to render the left part of each row of a table
const renderLeftOptionItem = (item: any) => {
const mainLeftItemTextStyle = {
...styles.leftText,
color: Colors.font_1,
...leftItemTextStyle,
};
// first check if there exist any customLeftItemComponents
// and then also check if customLeftItemComponent exist for
// the active option
if (customLeftItemComponents && customLeftItemComponents[leftKey]) {
const CustomLeftItem = customLeftItemComponents[leftKey];
return (
onLeftKeyPress(item)} />
);
}
return (
onLeftKeyPress(item)}
>
{extractResponseFromApiKey(item, leftKey.split('.'))}
);
};
const renderRightOptionItem = (item: any) => {
const mainRightItemTextStyle = {
...styles.rightText,
color: Colors.font_1,
...rightItemTextStyle,
};
// first check if there exist any customRightItemComponents
// and then also check if customRightItemComponent exist for
// the active option
if (
customRightItemComponents &&
customRightItemComponents[option[active]]
) {
const CustomRightItem = customRightItemComponents[option[active]];
return (
onRightKeyPress(item)} />
);
}
return (
onRightKeyPress(item)}
style={{ flex: 1, paddingVertical: 16 }}
>
{extractResponseFromApiKey(item, option[active].split('.'))}
);
};
const onRightKeyPress = (item?: any) => {
// if there has been some action needed to be
// done when user will click right item of table
// then this callback will be passed & called from here
if (rightKeyOnPress) rightKeyOnPress(optionLabel[active], item);
if (typeof option === 'object') {
setActive((active + 1) % option.length);
}
};
const onLeftKeyPress = (item: any) => {
// if there has been some action needed to be
// done when user will click left item of table
// then this callback will be passed & called from here
if (leftKeyOnPress) leftKeyOnPress(item);
};
const extractResponseFromApiKey = (
item: any,
depthObjArray: Array,
): string => {
// to handle nested case Ex: data.class.student
if (depthObjArray.length === 1) {
return item[depthObjArray[0]];
}
return extractResponseFromApiKey(
item[depthObjArray[0]],
depthObjArray.slice(1),
);
};
const mainTitleTextStyle = {
...styles.titleText,
color: Colors.font_1,
...titleTextStyle,
};
const mainOptionTitleTextStyle = {
...styles.titleText,
color: Colors.primary,
...optionTextStyle,
};
return (
{title}
{optionLabel[active]}
(
)}
ItemSeparatorComponent={itemSeparator}
showsVerticalScrollIndicator={false}
initialNumToRender={5}
keyExtractor={(item) =>
extractResponseFromApiKey(item, flatlistKey.split('.'))
}
/>
);
};
const styles = StyleSheet.create({
leftText: {
fontFamily: Fonts.type.gotham_medium,
fontSize: Fonts.size.small_13,
fontWeight: '800',
},
rightText: {
flex: 1,
textAlign: 'right',
fontFamily: Fonts.type.gotham_medium,
fontSize: Fonts.size.small_13,
fontWeight: '800',
},
titleText: {
fontFamily: Fonts.type.gotham_medium,
fontSize: Fonts.size.small_13,
fontWeight: '800',
},
});
export default Table;