Fixing height of react-table useTable rows

Viewed 2135

I'm having a hard time trying to set the height of a react-table using the useTable API.

As seen in the code, I'm trying to force the height of the rows to be 30 here:

{...row.getRowProps({ style: { height: 30 } })}

But when the content of a cell is larger than 30px, the whole height is expanded. How can I force the height to remain at 30px?

import React from "react";
import styled from "styled-components";
import PropTypes from "prop-types";
import { useTable, useSortBy } from "react-table";
import * as colors from "styles/variables/colors";

const TH = styled.th`
  margin: 0;
  padding: 0.5rem;
  position: sticky;
  top: 0;
  z-index: 100;
  font-weight: normal;
  font-size: 1.2rem;
`;
const TR = styled.tr`
  cursor: ${props => props.hasClick && "pointer"};
  border-bottom: 1px solid ${colors.grey15};
  background: ${props => props.groupRow && colors.grey10};
  &:hover {
    background: ${props =>
      !props.selectedRow && props.hasClick && colors.grey10};
  }
`;
const HeaderRow = styled.tr`
  height: ${props => props.isTablet && 45}px;
  background-color: ${colors.grey15};
  font-weight: normal;
`;
const HeaderText = styled.div`
  height: 25px;
  display: flex;
  align-items: center;
  border-bottom: ${props =>
    props.isSortedDesc && `2px solid ${colors.mossgreen}`};
  border-top: ${props =>
    props.isSorted && !props.isSortedDesc && `2px solid ${colors.mossgreen}`};
`;
const Styles = styled.div`
  overflow: hidden;
  overflow-y: auto;
  height: 100%;
  margin-top: 8px;
  table {
    border-spacing: 0;
    border: 0;
    width: 100%;

    th,
    td {
      margin: 0;
      padding: 0.3rem;
      font-size: 14px;
    }
  }
`;

const NoData = styled.div`
  width: 100%;
  padding: 16px;
`;

const Table = ({
  columns,
  data,
  onRowClick,
  compact,
  selectedRow,
  selectedRowKey,
  isTablet,
  itemSize
}) => {
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    prepareRow,
    rows
  } = useTable(
    {
      columns,
      data
    },
    useSortBy
  );
  console.log("...getTableBodyProps()", getTableBodyProps());
  console.log("itemSize", itemSize);
  return (
    <Styles itemSize={itemSize} compact={compact}>
      <table {...getTableProps()}>
        <thead>
          {headerGroups.map((headerGroup, hgi) => (
            <HeaderRow
              key={`header-row-${hgi}`}
              isTablet={isTablet}
              {...headerGroup.getHeaderGroupProps()}
            >
              {headerGroup.headers.map((column, ci) => {
                const { width } = columns[ci];
                return (
                  <TH
                    key={`header-column-${ci}`}
                    {...column.getHeaderProps(column.getSortByToggleProps())}
                    style={width && { width: `${width}%` }}
                  >
                    <HeaderText
                      compact={compact}
                      isSorted={column.isSorted}
                      isSortedDesc={column.isSortedDesc}
                    >
                      {column.render("Header")}
                    </HeaderText>
                  </TH>
                );
              })}
            </HeaderRow>
          ))}
        </thead>
        <tbody {...getTableBodyProps()}>
          {rows.map((row, i) => {
            prepareRow(row);
            return (
              <TR
                key={`body-row-${i}`}
                groupRow={row.original.grouped}
                onClick={() => onRowClick && onRowClick(row.original)}
                {...row.getRowProps({ style: { height: 30 } })}
                selectedRow={
                  selectedRow &&
                  selectedRow[selectedRowKey] === row.original[selectedRowKey]
                }
              >
                {row.cells.map((cell, ci) => {
                  return (
                    <td
                      style={{
                        width: `${columns[ci].width}%`,
                        display: "flex"
                      }}
                      key={`body-row-column-${ci}`}
                      {...cell.getCellProps()}
                    >
                      <div id={`table-${cell.column.id}--${i}`}>
                        {cell.render("Cell")}
                      </div>
                    </td>
                  );
                })}
              </TR>
            );
          })}
        </tbody>
      </table>
      {!rows.length && <NoData>No data</NoData>}
    </Styles>
  );
};
export default Table;
Table.propTypes = {
  columns: PropTypes.array.isRequired,
  data: PropTypes.array.isRequired,
  onRowClick: PropTypes.func,
  compact: PropTypes.bool,
  selectedRow: PropTypes.object,
  selectedRowKey: PropTypes.string.isRequired
};

1 Answers

I know this is an old question, but just wanted to share my experience with column width in React-Table, in case this is similar. width on a column essentially acts as a minWidth value, I think because of the way that flex is applied to the container. As in, the column is able to grow past the width value supplied based on the flex-grow, but it will never go below the width value.

If that's the case for height as well, using a height and a maxHeight on your row using the same value should do the trick.

Related