Skip to content

[BUG] - Table has a Spacer causing issue when Wanting to Create a custom Table Design #2108

@Hurly77

Description

@Hurly77

NextUI Version

2.2.9

Describe the bug

I'm customizing the <Table/> component and attempting to apply my own classNames to alter the header appearance to a more standard style. However, after overriding the default classNames, I'm observing a gap following the headers, as shown in the attached screenshot. This seems to be caused by the <Spacer as="tr" tabIndex={-1} y={1} /> component that is part of the Table component's structure. https://github.com/nextui-org/nextui/blob/main/packages/components/table/src/table.tsx.

I'm wondering if it would be possible to add an additional Prop like withoutSpacer

It seems like it would be pretty simple to implement

{props?.withoutSpacer ? null :  <Spacer as="tr" tabIndex={-1} y={1} />}

Here is an Example of my Custom Table:
image

Here is a full Code Example

import { faker } from "@faker-js/faker";
import { Table, TableBody, TableCell, TableColumn, TableHeader, TableRow } from "@nextui-org/react";
import React from "react";
type TableColType = {
  uid: string;
  name: string;
};

type TableRowType = {
  id: string;
  name: string;
  age: number;
  address: string;
};

export default function NextUICustomTable() {
  const fakeTableColumns: TableColType[] = [
    { uid: "name", name: "Name" },
    { uid: "age", name: "Age" },
    { uid: "address", name: "Address" },
  ];

  const tableRows: TableRowType[] = React.useMemo(() => {
    return Array.from({ length: 5 }, () => ({
      id: faker.string.uuid(),
      name: faker.person.fullName(),
      age: faker.number.int({ min: 18, max: 72 }),
      address: faker.location.streetAddress(),
    }));
  }, []);

  const renderTableColumn = React.useCallback((col: TableColType) => {
    switch (col.uid) {
      default:
        return col.name;
    }
  }, []);

  const renderTableCell = React.useCallback((row: TableRowType, key: React.Key) => {
    const value = row[key as keyof TableRowType];
    switch (key) {
      default:
        return value;
    }
  }, []);

  return (
    <Table
      radius="sm"
      classNames={{
        table: "p-0 m-0",
        thead: "[&>tr]:first:shadow-none [&>tr]:first:rounded-none border-b-2 border-default-200",
        th: "bg-transparent font-bold text-lg text-green-500 border-r-2 border-default-200 last:border-none pb-0 rounded-none first:rounded-t-none first:rounded-b-none last:rounded-t-none last:rounded-b-none last:rounded-t-none",
        base: "h-full max-w-5xl h-fit",
        td: "text-lg border-r-2 last:border-none border-default-200",
        wrapper: "bg-background dark:bg-default-100 max-w-5xl",
        tbody: "border-t",
        tr: "even:bg-green-500 even:bg-opacity-50 data-[selected=true]:bg-default-100",
      }}
    >
      <TableHeader columns={fakeTableColumns}>
        {(col) => <TableColumn key={col.uid}>{renderTableColumn(col)}</TableColumn>}
      </TableHeader>
      {
        <TableBody items={tableRows}>
          {(tableRow) => (
            <TableRow key={tableRow.id}>
              {(columnKey) => <TableCell>{renderTableCell(tableRow, columnKey)}</TableCell>}
            </TableRow>
          )}
        </TableBody>
      }
    </Table>
  );
}

There is a work around how ever it is not Ideal.

React.useEffect(() => {
    // removes an odd spacer that appears in the table header
    if (typeof window !== "undefined") {
      const elm = document.querySelector("table > thead > tr[tabindex='-1']");
      if (elm) {
        elm.remove();
      }
    }
  }, []);

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  1. Create a new NextJS Project
  2. npm i @nextui-org/react framer-motion
  3. Create a Table with @nextui-org/react
  4. use the code snippet in the issue if you need
  5. Observer

I don't think it is necessarily an issue you'd need to try and reproduce as it is already clear to me what the problem is.

Expected behavior

Spacer would be optional

Screenshots or Videos

image

Operating System Version

Ubuntu 22.04.3 LTS

Browser

Chrome

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions