Table
pdfx/tableFreeComponent
Composable table with Table, TableRow, TableCell
See it running
Open the demo on pdfx
pdfx.akashpise.dev/docs/components/tableInstall it
npx shadcn@latest add https://pdfx.akashpise.dev/r/table.jsonSource
1import { Text as PDFText, View } from '@react-pdf/renderer';2import type { Style } from '@react-pdf/types';3import { Children, type ReactElement, type ReactNode, cloneElement, isValidElement } from 'react';4import { usePdfxTheme, useSafeMemo } from '../lib/pdfx-theme-context';5import { createTableStyles } from './pdfx-table.styles';6import type {7 TableCellProps,8 TableProps,9 TableRowProps,10 TableSectionProps,11 TableVariant,12} from './pdfx-table.types';1314function processTableChildren(15 children: ReactNode,16 variant: TableVariant,17 zebraStripe: boolean18): ReactNode {19 let bodyRowIndex = 0;2021 return Children.map(children, (child) => {22 if (!isValidElement(child)) return child;2324 if (child.type === TableHeader || child.type === TableBody || child.type === TableFooter) {25 const isBody = child.type === TableBody;26 const sectionChild = child as ReactElement<TableSectionProps>;27 const sectionChildren = Children.map(sectionChild.props.children, (rowChild) => {28 if (isValidElement(rowChild) && rowChild.type === TableRow) {29 const rowProps: Partial<TableRowProps> = { variant };3031 if (isBody && zebraStripe) {32 const isStripe = bodyRowIndex % 2 === 1;33 bodyRowIndex++;34 if (isStripe) {35 rowProps.stripe = true;36 }37 }3839 return cloneElement(rowChild as ReactElement<TableRowProps>, rowProps);40 }41 return rowChild;42 });4344 return cloneElement(child, {}, sectionChildren);45 }4647 if (child.type === TableRow) {48 return cloneElement(child as ReactElement<TableRowProps>, { variant });49 }5051 return child;52 });53}5455export function TableHeader({ children, style }: TableSectionProps) {56 return (57 <View minPresenceAhead={60} style={style}>58 {children}59 </View>60 );61}6263export function TableBody({ children, style }: TableSectionProps) {64 return <View style={style}>{children}</View>;65}6667export function TableFooter({ children, style }: TableSectionProps) {68 return <View style={style}>{children}</View>;69}7071export function Table({72 children,73 style,74 variant = 'line',75 zebraStripe = false,76 noWrap = false,77}: TableProps) {78 const theme = usePdfxTheme();79 const styles = useSafeMemo(() => createTableStyles(theme), [theme]);80 const tableStyles: Style[] = [styles.table];81 const effectiveZebra = variant === 'striped' ? true : zebraStripe;8283 tableStyles.push(84 {85 grid: styles.tableGrid,86// … truncated
What it pulls in
npm packages
Files it writes
More from pdfx
All 34 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Alertalert | pdfx | Components | Free | 1 dep | |
| Badgebadge | pdfx | Components | Free | 1 dep | |
| Cardcard | pdfx | Components | Free | 1 dep | |
| DataTabledata-table | pdfx | Components | Free | 1 dep · 3 files | |
| Dividerdivider | pdfx | Components | Free | 1 dep | |
| Formform | pdfx | Components | Free | 1 dep · 3 files | |
| Graphgraph | pdfx | Components | Free | 1 dep · 4 files | |
| Headingheading | pdfx | Components | Free | 1 dep |
