Giter Club home page Giter Club logo

Comments (3)

dayvista avatar dayvista commented on May 25, 2024 2

For anyone searching for a fix to this, as I was...

I figured out a work-around to combine this library with react-table without throwing an error. I basically copied react-super-responsive-table's CSS, analyzed how it applies classNames to a table's individual elements once rendered in the browser, and applied that method to a table made with react-table and Chakra UI instead of the react-super-responsive-table components.

You can either copy the CSS from the react-super-responsive-table repo here into a CSS file of your own (as I did) or install the library and import only the CSS, like so:

import 'react-super-responsive-table/dist/SuperResponsiveTableStyle.css';

Here's the full code example:
(keep in mind that I'm using Next.js, CSS modules, and Chakra UI)

import { Table, Thead, Tbody, Tr, Th, Td, Box } from "@chakra-ui/react";
import { useTable } from "react-table";
import styles from "src/theme/css/tables.module.css";
import { chakra } from "@chakra-ui/react";
import { FaAngleDown, FaAngleUp } from "react-icons/fa";
const ChakraDownArrow = chakra(FaAngleDown);
const ChakraUpArrow = chakra(FaAngleUp);
import { useMemo } from "react";

const ResponsiveTableExample = () => {
  // dummy data
  const data = useMemo(
    () => [
      { testData1: "hello", testData2: "world" },
      { testData1: "hello again", testData2: "old friend" },
    ],
    []
  );

  // dummy headers for the dummy data
  const columns = useMemo(
    () => [
      {
        Header: "Test1",
        accessor: "testData1",
      },
      {
        Header: "Test2",
        accessor: "testData2",
      },
    ],
    []
  );

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    page,
    prepareRow,
  } = useTable();

  return (
    // apply the appropriate class from the
    // react-super-responsive-table CSS
    <Table
        {...getTableProps()}
        className={styles.responsiveTable}
        borderRadius="10px"
      >
        <Thead>
          {headerGroups.map((headerGroup) => (
            <Tr
              {...headerGroup.getHeaderGroupProps()}
              key={`table-row-${JSON.stringify(headerGroup)}`}
            >
              {headerGroup.headers.map((column, i) => (
                <Th
                  {...column.getHeaderProps(column.getSortByToggleProps())}
                  key={`table-head-${column.Header}-${i}`}
                >
                  {column.render("Header")}
                  <chakra.span pl="4%" key={`span-${column.Header}-${i}`}>
                    {column.isSorted ? (
                      column.isSortedDesc ? (
                        <ChakraDownArrow
                          aria-label="sorted descending"
                          key={`down-arrow-${column.Header}-${i}`}
                        />
                      ) : (
                        <ChakraUpArrow
                          aria-label="sorted ascending"
                          key={`up-arrow-${column.Header}-${i}`}
                        />
                      )
                    ) : null}
                  </chakra.span>
                </Th>
              ))}
            </Tr>
          ))}
        </Thead>
        <Tbody {...getTableBodyProps()}>
          {page.map((row, i) => {
            prepareRow(row);

            return (
              <Tr {...row.getRowProps()} key={`table-row-${row.id}-${i}`}>
                {row.cells.map((cell, i) => {
                  return (
                    <Td
                      {...cell.getCellProps()}
                      key={`table-data-${cell.value}-${i}`}
                       // apply the appropriate class from the
                       // react-super-responsive-table CSS
                      className={styles.pivoted}
                    >
                      {/* map through headerGroups once again and only render
                           a header if it corresponds with the correct column */}
                      {headerGroups.map((headerGroup) => {
                        return (
                          <Box key={`${JSON.stringify(headerGroup)}-container`}>
                            {headerGroup.headers.map((column, i) => {
                              return cell.column.Header === column.Header ? (
                                <Box
                                  key={`mobile-table-header-${column.Header}-${i}`}
                                  // apply the appropriate class from the
                                  // react-super-responsive-table CSS
                                  className={styles.tdBefore}
                                >
                                  {column.render("Header")}
                                </Box>
                              ) : null;
                            })}
                          </Box>
                        );
                      })}
                      {cell.render("Cell")}
                    </Td>
                  );
                })}
              </Tr>
            );
          })}
        </Tbody>
      </Table>
  );
};

export default ResponsiveTableExample;

from react-super-responsive-table.

coston avatar coston commented on May 25, 2024 1

Thanks for posting this @dayvista 💯

from react-super-responsive-table.

coston avatar coston commented on May 25, 2024

Hey @stephyswe! Thanks for creating this issue. It's a good idea. Is this an enhancement that you would like to contribute?

from react-super-responsive-table.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.