|
| 1 | +import { useMemo } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + MantineReactTable, |
| 5 | + type MRT_ColumnDef, |
| 6 | + useMantineReactTable, |
| 7 | +} from '../../src'; |
| 8 | + |
| 9 | +import { type Meta } from '@storybook/react'; |
| 10 | + |
| 11 | +const meta: Meta = { |
| 12 | + title: 'Fixed Bugs/Cell Alignment', |
| 13 | +}; |
| 14 | + |
| 15 | +export default meta; |
| 16 | + |
| 17 | +type Person = { |
| 18 | + address: string; |
| 19 | + city: string; |
| 20 | + name: { |
| 21 | + firstName: string; |
| 22 | + lastName: string; |
| 23 | + }; |
| 24 | + state: string; |
| 25 | +}; |
| 26 | + |
| 27 | +//nested data is ok, see accessorKeys in ColumnDef below |
| 28 | +const data: Person[] = [ |
| 29 | + { |
| 30 | + address: '261 Battle Ford', |
| 31 | + city: 'Columbus', |
| 32 | + name: { |
| 33 | + firstName: 'Zachary', |
| 34 | + lastName: 'Davis', |
| 35 | + }, |
| 36 | + state: 'Ohio', |
| 37 | + }, |
| 38 | + { |
| 39 | + address: '566 Brakus Inlet', |
| 40 | + city: 'Westerville', |
| 41 | + name: { |
| 42 | + firstName: 'Robert', |
| 43 | + lastName: 'Smith', |
| 44 | + }, |
| 45 | + state: 'West Virginia', |
| 46 | + }, |
| 47 | + { |
| 48 | + address: '7777 Kuhic Knoll', |
| 49 | + city: 'South Linda', |
| 50 | + name: { |
| 51 | + firstName: 'Kevin', |
| 52 | + lastName: 'Yan', |
| 53 | + }, |
| 54 | + state: 'West Virginia', |
| 55 | + }, |
| 56 | + { |
| 57 | + address: '722 Emie Stream', |
| 58 | + city: 'Huntington', |
| 59 | + name: { |
| 60 | + firstName: 'John', |
| 61 | + lastName: 'Upton', |
| 62 | + }, |
| 63 | + state: 'Washington', |
| 64 | + }, |
| 65 | + { |
| 66 | + address: '1 Kuhic Knoll', |
| 67 | + city: 'Ohiowa', |
| 68 | + name: { |
| 69 | + firstName: 'Nathan', |
| 70 | + lastName: 'Harris', |
| 71 | + }, |
| 72 | + state: 'Nebraska', |
| 73 | + }, |
| 74 | +]; |
| 75 | + |
| 76 | +// Shared columns definition with centered alignment |
| 77 | +const createColumns = (): MRT_ColumnDef<Person>[] => [ |
| 78 | + { |
| 79 | + accessorKey: 'name.firstName', //access nested data with dot notation |
| 80 | + header: 'First Name', |
| 81 | + mantineTableBodyCellProps: { |
| 82 | + align: 'center', |
| 83 | + }, |
| 84 | + mantineTableHeadCellProps: { |
| 85 | + align: 'center', |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + accessorKey: 'name.lastName', |
| 90 | + header: 'Last Name', |
| 91 | + mantineTableBodyCellProps: { |
| 92 | + align: 'center', |
| 93 | + }, |
| 94 | + mantineTableHeadCellProps: { |
| 95 | + align: 'center', |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + accessorKey: 'address', //normal accessorKey |
| 100 | + header: 'Address', |
| 101 | + mantineTableBodyCellProps: { |
| 102 | + align: 'center', |
| 103 | + }, |
| 104 | + mantineTableHeadCellProps: { |
| 105 | + align: 'center', |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + accessorKey: 'city', |
| 110 | + header: 'City', |
| 111 | + mantineTableBodyCellProps: { |
| 112 | + align: 'center', |
| 113 | + }, |
| 114 | + mantineTableHeadCellProps: { |
| 115 | + align: 'center', |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + accessorKey: 'state', |
| 120 | + header: 'State', |
| 121 | + mantineTableBodyCellProps: { |
| 122 | + align: 'center', |
| 123 | + }, |
| 124 | + mantineTableHeadCellProps: { |
| 125 | + align: 'center', |
| 126 | + }, |
| 127 | + }, |
| 128 | +]; |
| 129 | + |
| 130 | +export const CellAlignment = () => { |
| 131 | + //should be memoized or stable |
| 132 | + const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []); |
| 133 | + |
| 134 | + const table = useMantineReactTable({ |
| 135 | + columns, |
| 136 | + data, |
| 137 | + enableColumnActions: false, |
| 138 | + enableSorting: false, |
| 139 | + // enableHeaderActionsHoverReveal: true, |
| 140 | + mantineTableProps: { |
| 141 | + withColumnBorders: true, |
| 142 | + withRowBorders: true, |
| 143 | + withTableBorder: true, |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + return <MantineReactTable table={table} />; |
| 148 | +}; |
| 149 | + |
| 150 | +export const CellAlignmentWithSorting = () => { |
| 151 | + const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []); |
| 152 | + |
| 153 | + const table = useMantineReactTable({ |
| 154 | + columns, |
| 155 | + data, |
| 156 | + enableColumnActions: false, |
| 157 | + enableSorting: true, // Enable sorting |
| 158 | + mantineTableProps: { |
| 159 | + withColumnBorders: true, |
| 160 | + withRowBorders: true, |
| 161 | + withTableBorder: true, |
| 162 | + }, |
| 163 | + }); |
| 164 | + |
| 165 | + return <MantineReactTable table={table} />; |
| 166 | +}; |
| 167 | + |
| 168 | +export const CellAlignmentWithColumnDragging = () => { |
| 169 | + const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []); |
| 170 | + |
| 171 | + const table = useMantineReactTable({ |
| 172 | + columns, |
| 173 | + data, |
| 174 | + enableColumnActions: true, |
| 175 | + enableColumnDragging: true, // Enable column dragging |
| 176 | + enableSorting: false, |
| 177 | + mantineTableProps: { |
| 178 | + withColumnBorders: true, |
| 179 | + withRowBorders: true, |
| 180 | + withTableBorder: true, |
| 181 | + }, |
| 182 | + }); |
| 183 | + |
| 184 | + return <MantineReactTable table={table} />; |
| 185 | +}; |
0 commit comments