Skip to content

Resizable columns render with equal width instead of auto-fitting content #736

@pfo-omicsstudio

Description

@pfo-omicsstudio

Describe the bug
When setting the resizable prop of columns to true, the initial column width calculation results in table where each column is evenly spaced:

Image

In contrast, with resizable is not enabled, column widths are fit to the content, as seen here:

Image

To Reproduce
Just set resizable to true in all columns in a Datatable.

You can use this modified code from ResizingExample.tsx to easily replicate it (i.e. just replace ResizingExample.tsx contents with this code):

'use client';

import { Button, Group, Stack, Switch } from '@mantine/core';
import { DataTable, useDataTableColumns } from '__PACKAGE__';
import { useState } from 'react';
import { companies, type Company } from '~/data';

export default function ResizingExample() {
  const key = 'resize-example';

  const [resizable, setResizable] = useState<boolean>(true);
  const [withTableBorder, setWithTableBorder] = useState<boolean>(true);
  const [withColumnBorders, setWithColumnBorders] = useState<boolean>(true);

  const { effectiveColumns, resetColumnsWidth } = useDataTableColumns<Company>({
    key,
    columns: [
      { accessor: 'name', resizable },
      { accessor: 'streetAddress', resizable },
      { accessor: 'city', resizable },
      { accessor: 'state', resizable },
      { accessor: 'missionStatement', resizable },
    ],
  });

  return (
    <Stack>
      <DataTable
        withTableBorder={withTableBorder}
        withColumnBorders={withColumnBorders}
        storeColumnsKey={key}
        records={companies}
        columns={effectiveColumns}
      />
      <Group grow justify="space-between">
        <Group justify="flex-start">
          <Switch
            checked={resizable}
            onChange={(event) => setResizable(event.currentTarget.checked)}
            labelPosition="left"
            label="Resizable"
          />
          <Switch
            checked={withTableBorder}
            onChange={(event) => setWithTableBorder(event.currentTarget.checked)}
            labelPosition="left"
            label="Table Border"
          />
          <Switch
            checked={withColumnBorders}
            onChange={(event) => setWithColumnBorders(event.currentTarget.checked)}
            labelPosition="left"
            label="Column Borders"
          />
        </Group>
        <Group justify="right">
          <Button onClick={resetColumnsWidth}>Reset Column Width</Button>
        </Group>
      </Group>
    </Stack>
  );
}

Expected behavior
Initial column widths should be consistent and independent of the resizable prop. Columns should always fit their content by default, to avoid rows with excessive empty space.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions