|
| 1 | +import { useState } from 'react'; |
| 2 | +import DataTable from '../ThemedDataTable'; |
| 3 | +import { type TableColumn, type FooterComponentProps } from 'react-data-table-component'; |
| 4 | + |
| 5 | +interface Row { |
| 6 | + id: number; |
| 7 | + name: string; |
| 8 | + department: string; |
| 9 | + salary: number; |
| 10 | + bonus: number; |
| 11 | +} |
| 12 | + |
| 13 | +const ALL_DATA: Row[] = [ |
| 14 | + { id: 1, name: 'Aria Chen', department: 'Engineering', salary: 155000, bonus: 12000 }, |
| 15 | + { id: 2, name: 'Marcus Webb', department: 'Product', salary: 132000, bonus: 9500 }, |
| 16 | + { id: 3, name: 'Priya Kapoor', department: 'Design', salary: 118000, bonus: 7800 }, |
| 17 | + { id: 4, name: 'Jordan Ellis', department: 'Analytics', salary: 143000, bonus: 11200 }, |
| 18 | + { id: 5, name: 'Sam Rivera', department: 'Engineering', salary: 128000, bonus: 9000 }, |
| 19 | + { id: 6, name: 'Taylor Brooks', department: 'Engineering', salary: 122000, bonus: 8400 }, |
| 20 | + { id: 7, name: 'Casey Morgan', department: 'Product', salary: 108000, bonus: 6600 }, |
| 21 | + { id: 8, name: 'Alex Kim', department: 'Analytics', salary: 137000, bonus: 10100 }, |
| 22 | + { id: 9, name: 'Morgan Lee', department: 'Design', salary: 114000, bonus: 7200 }, |
| 23 | + { id: 10, name: 'Drew Park', department: 'Engineering', salary: 141000, bonus: 10800 }, |
| 24 | +]; |
| 25 | + |
| 26 | +function SummaryFooter({ rows }: FooterComponentProps<Row>) { |
| 27 | + const totalSalary = rows.reduce((s, r) => s + r.salary, 0); |
| 28 | + const totalBonus = rows.reduce((s, r) => s + r.bonus, 0); |
| 29 | + const avgSalary = rows.length ? Math.round(totalSalary / rows.length) : 0; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div |
| 33 | + style={{ |
| 34 | + display: 'flex', |
| 35 | + flexWrap: 'wrap', |
| 36 | + gap: '16px 32px', |
| 37 | + padding: '10px 16px', |
| 38 | + borderTop: '1px solid #e5e7eb', |
| 39 | + backgroundColor: '#f8fafc', |
| 40 | + fontSize: 13, |
| 41 | + color: '#374151', |
| 42 | + }} |
| 43 | + > |
| 44 | + <span> |
| 45 | + <strong>{rows.length}</strong> {rows.length === 1 ? 'employee' : 'employees'} |
| 46 | + </span> |
| 47 | + <span> |
| 48 | + Salary total: <strong>${totalSalary.toLocaleString()}</strong> |
| 49 | + </span> |
| 50 | + <span> |
| 51 | + Salary avg: <strong>${avgSalary.toLocaleString()}</strong> |
| 52 | + </span> |
| 53 | + <span> |
| 54 | + Bonus total: <strong>${totalBonus.toLocaleString()}</strong> |
| 55 | + </span> |
| 56 | + <span> |
| 57 | + Total comp: <strong>${(totalSalary + totalBonus).toLocaleString()}</strong> |
| 58 | + </span> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +const columns: TableColumn<Row>[] = [ |
| 64 | + { id: 'name', name: 'Name', selector: r => r.name, sortable: true }, |
| 65 | + { id: 'department', name: 'Department', selector: r => r.department, sortable: true }, |
| 66 | + { |
| 67 | + id: 'salary', |
| 68 | + name: 'Salary', |
| 69 | + selector: r => r.salary, |
| 70 | + sortable: true, |
| 71 | + right: true, |
| 72 | + format: r => `$${r.salary.toLocaleString()}`, |
| 73 | + }, |
| 74 | + { |
| 75 | + id: 'bonus', |
| 76 | + name: 'Bonus', |
| 77 | + selector: r => r.bonus, |
| 78 | + sortable: true, |
| 79 | + right: true, |
| 80 | + format: r => `$${r.bonus.toLocaleString()}`, |
| 81 | + }, |
| 82 | +]; |
| 83 | + |
| 84 | +export default function FooterCustomDemo() { |
| 85 | + const [deptFilter, setDeptFilter] = useState('All'); |
| 86 | + const departments = ['All', 'Engineering', 'Product', 'Design', 'Analytics']; |
| 87 | + const data = deptFilter === 'All' ? ALL_DATA : ALL_DATA.filter(r => r.department === deptFilter); |
| 88 | + |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <div style={{ marginBottom: 12, display: 'flex', alignItems: 'center', gap: 8 }}> |
| 92 | + <span style={{ fontSize: 13, color: '#6b7280' }}>Filter by department:</span> |
| 93 | + {departments.map(d => ( |
| 94 | + <button |
| 95 | + key={d} |
| 96 | + onClick={() => setDeptFilter(d)} |
| 97 | + style={{ |
| 98 | + fontSize: 12, |
| 99 | + padding: '3px 10px', |
| 100 | + borderRadius: 9999, |
| 101 | + border: '1px solid', |
| 102 | + cursor: 'pointer', |
| 103 | + borderColor: deptFilter === d ? '#6366f1' : '#d1d5db', |
| 104 | + backgroundColor: deptFilter === d ? '#6366f1' : 'transparent', |
| 105 | + color: deptFilter === d ? '#fff' : '#374151', |
| 106 | + }} |
| 107 | + > |
| 108 | + {d} |
| 109 | + </button> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + <DataTable |
| 113 | + columns={columns} |
| 114 | + data={data} |
| 115 | + highlightOnHover |
| 116 | + dense |
| 117 | + footerComponent={SummaryFooter} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments