diff --git a/example/src/SheetJSExample.tsx b/example/src/SheetJSExample.tsx index 8a20db0..6ee0259 100644 --- a/example/src/SheetJSExample.tsx +++ b/example/src/SheetJSExample.tsx @@ -1,6 +1,6 @@ /* sheetjs (C) SheetJS -- https://sheetjs.com */ import { useState, useCallback } from 'react'; -import { StyleSheet, Text, Button, Alert, ScrollView, Image } from 'react-native'; +import { StyleSheet, Text, Button, Alert, ScrollView } from 'react-native'; import { Table, Row, Rows, TableWrapper } from 'react-native-tabeller'; import { read, utils, WorkSheet } from 'xlsx'; @@ -25,6 +25,7 @@ function SheetJSExample(): JSX.Element { const [widths, setWidths] = useState( Array.from({ length: 7 }, () => 20) ); + const importFile = useCallback(async () => { try { const ab = await ( @@ -60,7 +61,7 @@ function SheetJSExample(): JSX.Element { data={data[0]} style={styles.thead} textStyle={styles.text} - widthArr={widths.map((x) => x * 1.2)} + widthArr={widths} /> @@ -69,7 +70,7 @@ function SheetJSExample(): JSX.Element { data={data.slice(1)} style={styles.tr} textStyle={styles.text} - widthArr={widths.map((x) => x * 1.2)} + widthArr={widths} /> @@ -80,7 +81,12 @@ function SheetJSExample(): JSX.Element { } const styles = StyleSheet.create({ - container: { backgroundColor: '#F5FCFF' }, + container: { + flex: 1, + justifyContent: 'center', + alignItems: 'center', + backgroundColor: '#F5FCFF', + }, welcome: { fontSize: 20, textAlign: 'center', margin: 10 }, bolded: { textAlign: 'center', @@ -88,8 +94,17 @@ const styles = StyleSheet.create({ marginBottom: 5, fontWeight: 'bold', }, - thead: { height: 40, backgroundColor: '#f1f8ff' }, - tr: { height: 30 }, + thead: { + height: 40, + backgroundColor: '#f1f8ff', + borderWidth: 0.5, + justifyContent: 'center', + }, + tr: { + height: 30, + borderWidth: 0.5, + justifyContent: 'center', + }, text: { marginLeft: 5 }, table: { width: '100%' }, image: { height: 16, width: 16 }, diff --git a/react-native-tabeller-0.1.0.tgz b/react-native-tabeller-0.1.0.tgz index 7540ee1..01683e0 100644 Binary files a/react-native-tabeller-0.1.0.tgz and b/react-native-tabeller-0.1.0.tgz differ diff --git a/src/components/cell.tsx b/src/components/cell.tsx index a9b995a..1e130d2 100644 --- a/src/components/cell.tsx +++ b/src/components/cell.tsx @@ -1,8 +1,7 @@ import { useMemo } from 'react'; import type { FC, PropsWithChildren } from 'react'; -import { View, Text, StyleSheet, TouchableWithoutFeedback } from 'react-native'; +import { View, Text, StyleSheet, Pressable } from 'react-native'; import type { ViewStyle } from 'react-native'; - import type { CellProps } from '../types'; /** @@ -18,7 +17,7 @@ export const Cell: FC> = ({ borderStyle, children, onPress, - cellContainerProps = {}, + cellContainer = {}, ...props }) => { const textDom = children ?? ( @@ -34,16 +33,27 @@ export const Cell: FC> = ({ const composedStyles = useMemo(() => { const styles: ViewStyle = {}; if (width) styles.width = width; + if (height) styles.height = height; + if (flex) styles.flex = flex; + if (!width && !flex && !height && !style) styles.flex = 1; return styles; }, [width, height, flex, style]); + // determine whether to apply flex: 1 to cell style + const cellStyle = useMemo(() => { + if (width) { + return {}; + } + return styles.cell; + }, [width]); + return ( > = ({ }, composedStyles, style, - cellContainerProps.style, - styles.cell, + cellContainer.style, + cellStyle, ])} > - onPress(data) : undefined} - disabled={!onPress} - > + onPress?.(data)}> {textDom} - + ); }; diff --git a/src/components/cols.tsx b/src/components/cols.tsx index 4a6e40d..15a7e11 100644 --- a/src/components/cols.tsx +++ b/src/components/cols.tsx @@ -11,12 +11,9 @@ export const Col: FC = ({ heightArr, flex, textStyle, - borderStyle, ...props }) => { - if (!data) return null; - - return ( + return data ? ( = ({ width={width} height={height} textStyle={textStyle} - borderStyle={borderStyle} {...props} /> ); })} - ); + ) : null; }; export const Cols: FC = ({ @@ -48,14 +44,11 @@ export const Cols: FC = ({ heightArr, flexArr, textStyle, - borderStyle, ...props }) => { - if (!data) return null; - const width = widthArr ? sum(widthArr) : 0; - return ( + return data ? ( {data.map((item, i) => { const flex = flexArr?.[i]; @@ -69,13 +62,12 @@ export const Cols: FC = ({ flex={flex} style={style} textStyle={textStyle} - borderStyle={borderStyle} {...props} /> ); })} - ); + ) : null; }; const styles = StyleSheet.create({ diff --git a/src/components/rows.tsx b/src/components/rows.tsx index 700298f..fe96863 100644 --- a/src/components/rows.tsx +++ b/src/components/rows.tsx @@ -15,43 +15,40 @@ export const Row: FC = ({ height, flexArr, textStyle, - borderStyle, onPress, cellTextStyle, ...props }) => { - // calc total width based on width array - const totalWidth = widthArr ? sum(widthArr) : 0; - - // calc row styles based on props - const rowStyle = useMemo((): ViewStyle => { + const width = widthArr ? sum(widthArr) : 0; + const composedStyle = useMemo(() => { const styles: ViewStyle = {}; - if (totalWidth) styles.width = totalWidth; - if (height) styles.height = height; - return styles; - }, [totalWidth, height]); + if (width) styles.width = width; - if (!data || !data.length) return null; + if (height) styles.height = height; + + return styles; + }, [width, height]); + + if (!data) return null; return ( - - {data.map((item, index) => { - const cellFlex = flexArr?.[index]; - const cellWidth = widthArr?.[index]; - const customTextStyle = cellTextStyle - ? cellTextStyle(item, index) - : undefined; - + + {data.map((item, i) => { + const flex = flexArr?.[i]; + const wth = widthArr?.[i]; return ( ); @@ -60,7 +57,6 @@ export const Row: FC = ({ ); }; -/** Rows component - Renders multiple rows*/ export const Rows: FC = ({ data, style, @@ -68,44 +64,39 @@ export const Rows: FC = ({ heightArr, flexArr, textStyle, - borderStyle, ...props }) => { - // calc total flex and width - const totalFlex = flexArr ? sum(flexArr) : 0; - const totalWidth = widthArr ? sum(widthArr) : 0; + const flex = flexArr ? sum(flexArr) : 0; + const width = widthArr ? sum(widthArr) : 0; - // calc container styles - const containerStyle = useMemo((): ViewStyle => { + const composedStyle = useMemo(() => { const styles: ViewStyle = {}; - if (totalFlex) styles.flex = totalFlex; - if (totalWidth) styles.width = totalWidth; + if (flex) styles.flex = flex; + + if (width) styles.width = width; + return styles; - }, [totalFlex, totalWidth]); - - if (!data || !data.length) return null; - - return ( - - {data.map((rowData, index) => { - const rowHeight = heightArr?.[index]; + }, [flex, width]); + return data ? ( + + {data.map((item, i) => { + const height = heightArr?.[i]; return ( ); })} - ); + ) : null; }; const styles = StyleSheet.create({ diff --git a/src/components/table.tsx b/src/components/table.tsx index db6f657..4fa9582 100644 --- a/src/components/table.tsx +++ b/src/components/table.tsx @@ -1,6 +1,6 @@ import type { FC } from 'react'; import React from 'react'; -import { View, StyleSheet } from 'react-native'; +import { View } from 'react-native'; import type { TableProps, TableWrapperProps } from '../types'; @@ -10,35 +10,25 @@ export const Table: FC = ({ style, borderStyle, children }) => { const borderColor = borderStyle?.borderColor ?? '#000'; const renderChildren = () => - React.Children.map(children, (child) => { - if (!React.isValidElement(child)) return child; - - // check if we should add the border style | skip known type names - const elementType = child.type as any; - const isScrollView = - elementType?.displayName === 'ScrollView' || - elementType?.name === 'ScrollView'; - - if (borderStyle && !isScrollView) { - return React.cloneElement(child, { - borderStyle, - ...child.props, - }); - } - - return child; - }); + React.Children.map(children, (child: any) => + React.cloneElement( + child, + borderStyle && child.type.displayName !== 'ScrollView' + ? { borderStyle } + : {} + ) + ); return ( {renderChildren()} @@ -51,28 +41,9 @@ export const TableWrapper: FC = ({ children, }) => { const renderChildren = () => - React.Children.map(children, (child) => { - if (!React.isValidElement(child)) return child; + React.Children.map(children, (child: any) => + React.cloneElement(child, borderStyle ? { borderStyle } : {}) + ); - if (borderStyle) { - return React.cloneElement(child, { - borderStyle, - ...child.props, - }); - } - - return child; - }); - - return ( - - {renderChildren()} - - ); + return {renderChildren()}; }; - -const styles = StyleSheet.create({ - wrapper: { - flex: 1, - }, -}); diff --git a/src/types.ts b/src/types.ts index a54c33f..4d98591 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,7 +28,7 @@ export interface CellProps extends BaseTableProps { /** text style for cell content */ textStyle?: StyleProp; /** props passed to the cell container */ - cellContainerProps?: ViewProps; + cellContainer?: ViewProps; /** callback when cell is pressed */ onPress?: (data: any) => void; /** children to render inside the cell */ @@ -48,7 +48,7 @@ export interface RowProps extends BaseTableProps { /** text style applied to all cell in the row */ textStyle?: StyleProp; /** function to generate custom text styles for individual cell */ - cellTextStyle?: (item: any, index: number) => StyleProp; + cellTextStyle?: (item: any) => StyleProp; /** callback when a cell is pressed */ onPress?: (item: any) => void; } @@ -65,6 +65,7 @@ export interface RowsProps extends BaseTableProps { flexArr?: number[]; /** text style applied to all cells */ textStyle?: StyleProp; + cellTextStyle?: (item: any) => StyleProp; /** callback when a cell is pressed */ onPress?: (item: any) => void; }