Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable table component and example #149

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

robertardeleanu
Copy link

What does it fix?

Implemented a generic and configurable table component and example for it
Closes #135

How has it been tested?

Manually tested with the example itself

@vercel
Copy link

vercel bot commented Apr 30, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
teacher-workout-client ✅ Ready (Inspect) Visit Preview Apr 30, 2022 at 2:23PM (UTC)

}

function stableSort(array, comparator) {
const stabilizedThis = array.map((el, index) => [el, index]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to chain these methods, such as (also added an alternative to the double return)

array
  .map((el, index) => [el, index])
  .sort((a, b) => {
    const elementOrder = comparator(a[0], b[0]);
    const decisiveOrder = elementOrder === 0 ? a[1] - b[1] : elementOrder;

    return decisiveOrder;
  })
  .map((el) => el[0]);

}

function getComparator(order, orderBy) {
return order === "desc"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there is a code repetition, it could be shortened to:

Suggested change
return order === "desc"
const scalar = order === "desc" ? 1 : -1;
return scalar * descendingComparator(a.original, b.original, orderBy);

};

function descendingComparator(a, b, orderBy) {
if (b[orderBy] < a[orderBy]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic could be simplified to

Suggested change
if (b[orderBy] < a[orderBy]) {
return b[orderBy] - a[orderBy];

But further, the below function could be simplified to

const scalar = order === "desc" ? 1 : -1;
return scalar * (b.original[orderBy] - a.original[orderBy]);

And this logic as they are pure functions, they could be entirely moved to a helper file with some test suite

</TableCell>
);

const singleSelectCell = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if by assigning this component straight to the const React will execute (not necessary render) code that is inside of it, it is worth to check.

Also the code <TableCell padding="checkbox"> is repeated, it could be returned regardless and then composed with Checkbox / Radio


const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - processedData.length) : 0;

const renderTableHead = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of these functions could be placed in a separate file, as it clogs this whole file

});
}, [data]);

const processedColumns = useMemo(() => columns.filter(({ show = true }) => show));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hook will always run because it has no dependencies, making memoizing not useful


const handleRequestSort = (event, property) => {
const isAsc = orderBy === property && order === 'asc';
setOrder(isAsc ? 'desc' : 'asc');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there be an "enum" for asc/desc to be used across this component? Thinking of benefit of auto complete and prevent typos

const orderType = {
  asc: 'asc',
  desc: 'desc'
};



return Template ? (
<TableCell>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TableCell could wrap around the actual alternatives

Suggested change
<TableCell>
<TableCell>
{Template ? <Template row={row} /> : row.original[column.accessor]}
</TableCell>


const isSelected = (id) => selected.findIndex((entry) => entry._id === id) !== -1;

const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - processedData.length) : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Math.max is already going establish a lower limit to 0, no?

Suggested change
const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - processedData.length) : 0;
const emptyRows = Math.max(0, (1 + page) * rowsPerPage - processedData.length);

@@ -0,0 +1,268 @@
import React, { useState, useMemo } from 'react';
import PropTypes from 'prop-types'
import _ from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible we could import functions individually, so that it can be tree-shaked

Suggested change
import _ from 'lodash';
import { noop } from 'lodash';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reuse table component
3 participants