import React, { useState } from 'react'; import { Screen, View, Text, Badge, Button } from '@idealyst/components'; import Table from '../Table'; import type { TableColumn, SortDirection } from '../Table/types'; interface User { id: number; name: string; email: string; role: string; status: 'active' | 'inactive'; } interface Product { id: number; name: string; category: string; price: number; stock: number; } export const TableExamples: React.FC = () => { const users: User[] = [ { id: 1, name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'active' }, { id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'active' }, { id: 3, name: 'Bob Johnson', email: 'bob@example.com', role: 'User', status: 'inactive' }, { id: 4, name: 'Alice Williams', email: 'alice@example.com', role: 'Manager', status: 'active' }, ]; const products: Product[] = [ { id: 1, name: 'Laptop', category: 'Electronics', price: 999.99, stock: 15 }, { id: 2, name: 'Mouse', category: 'Electronics', price: 29.99, stock: 150 }, { id: 3, name: 'Keyboard', category: 'Electronics', price: 79.99, stock: 75 }, { id: 4, name: 'Monitor', category: 'Electronics', price: 299.99, stock: 30 }, { id: 5, name: 'Desk', category: 'Furniture', price: 399.99, stock: 10 }, ]; const userColumns: TableColumn[] = [ { key: 'name', title: 'Name', dataIndex: 'name', width: '200px', }, { key: 'email', title: 'Email', dataIndex: 'email', }, { key: 'role', title: 'Role', dataIndex: 'role', width: '120px', }, { key: 'status', title: 'Status', dataIndex: 'status', width: '100px', render: (status: string) => ( {status} ), }, ]; const productColumns: TableColumn[] = [ { key: 'name', title: 'Product', dataIndex: 'name', }, { key: 'category', title: 'Category', dataIndex: 'category', width: '150px', }, { key: 'price', title: 'Price', dataIndex: 'price', width: '120px', align: 'right', render: (price: number) => `$${price.toFixed(2)}`, }, { key: 'stock', title: 'Stock', dataIndex: 'stock', width: '100px', align: 'center', render: (stock: number) => ( 50 ? 'green' : stock > 20 ? 'yellow' : 'red'} > {stock} ), }, ]; const actionColumns: TableColumn[] = [ { key: 'name', title: 'Name', dataIndex: 'name', }, { key: 'email', title: 'Email', dataIndex: 'email', }, { key: 'actions', title: 'Actions', width: '200px', render: (_, user) => ( ), }, ]; return ( Table Examples Basic Table Variants Default
Striped
With Dividers Standard + Dividers
Striped + Dividers
Sizes Small
Medium (default)
Large
Custom Rendering
Clickable Rows
console.log('Clicked user:', user)} /> Click any row to see console output With Actions
Column Alignment
`$${price.toFixed(2)}` }, ]} data={products.slice(0, 3)} type="bordered" /> Fixed Column Widths
Empty Table
No data to display ); }; const SortableTableExample: React.FC = () => { const [sortedProducts, setSortedProducts] = useState([ { id: 1, name: 'Laptop', category: 'Electronics', price: 999.99, stock: 15 }, { id: 2, name: 'Mouse', category: 'Electronics', price: 29.99, stock: 150 }, { id: 3, name: 'Keyboard', category: 'Electronics', price: 79.99, stock: 75 }, { id: 4, name: 'Monitor', category: 'Electronics', price: 299.99, stock: 30 }, { id: 5, name: 'Desk', category: 'Furniture', price: 399.99, stock: 10 }, ]); const handleSort = (columnKey: string, direction: SortDirection) => { if (!direction) { setSortedProducts([...sortedProducts]); return; } const sorted = [...sortedProducts].sort((a, b) => { const aVal = (a as any)[columnKey]; const bVal = (b as any)[columnKey]; if (typeof aVal === 'number') return direction === 'asc' ? aVal - bVal : bVal - aVal; return direction === 'asc' ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal)); }); setSortedProducts(sorted); }; const columns: TableColumn[] = [ { key: 'name', title: 'Product', dataIndex: 'name', sortable: true }, { key: 'category', title: 'Category', dataIndex: 'category', width: '150px', sortable: true }, { key: 'price', title: 'Price', dataIndex: 'price', width: '120px', align: 'right', sortable: true, render: (price: number) => `$${price.toFixed(2)}` }, { key: 'stock', title: 'Stock', dataIndex: 'stock', width: '100px', align: 'center', sortable: true }, ]; return ( Sortable Table
Click column headers to cycle sort: unsorted, ascending, descending ); }; const OptionsTableExample: React.FC = () => { const data = [ { id: 1, name: 'John Doe', email: 'john@example.com', role: 'Admin' }, { id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'User' }, { id: 3, name: 'Bob Johnson', email: 'bob@example.com', role: 'User' }, ]; const columns: TableColumn[] = [ { key: 'name', title: 'Name', dataIndex: 'name', sortable: true, options: [ { id: 'sort-asc', label: 'Sort A-Z', icon: 'sort-ascending', onClick: () => console.log('Sort A-Z') }, { id: 'sort-desc', label: 'Sort Z-A', icon: 'sort-descending', onClick: () => console.log('Sort Z-A') }, { id: 'sep', label: '', separator: true }, { id: 'hide', label: 'Hide Column', icon: 'eye-off', onClick: () => console.log('Hide column') }, ], }, { key: 'email', title: 'Email', dataIndex: 'email', options: [ { id: 'copy', label: 'Copy All Emails', icon: 'content-copy', onClick: () => console.log('Copy emails') }, ], }, { key: 'role', title: 'Role', dataIndex: 'role', width: '120px', }, ]; return ( Column Options Menu
Columns with options show a kebab menu icon in the header ); }; export default TableExamples;