question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Table] - HeaderCell onClick prop is overwritten

See original GitHub issue

Issue Description

Consumers wanting to implement sorting need to be able to pass an onClick to a Table’s HeaderCell. Currently the HeaderCell’s onClick is always overwritten by the click event of the Table.Header

https://github.com/cerner/terra-core/blob/master/packages/terra-table/src/TableHeader.jsx#L33

Issue Type

  • New Feature
  • Enhancement
  • Bug
  • Other

Expected Behavior

A Table.HeaderCell should honor an onClick directly passed.

Current Behavior

A Table.HeaderCell’s onClick is always overwritten by the onClick of the Table.Header.

 <Table.Header>
    <Table.HeaderCell content={'Name'} minWidth="small" sort="desc" onClick={() => console.log('This onClick will never be invoked')} />
    <Table.HeaderCell content={'Address'} key={'ADDRESS'} minWidth={'medium'} />
    <Table.HeaderCell content={'Phone Number'} key={'PHONE_NUMBER'} minWidth={'large'} />
 </Table.Header>

Steps to Reproduce

  1. Create a Table
  2. Add an onClick to a Table.HeaderCell
  3. Observe the onClick is always overwritten,

Environment

  • All environments

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
StephenEssercommented, Feb 15, 2018

We’ll need to determine why the code was implemented this way to uncover if this was intentional functionality or an accidental bug. My assumption is this was written as a workaround to avoid a lint error when adding an onClick to a <thead /> tag.

I would expect both onClicks to be fired if the user added an onClick to both the Table.Header and the Table.HeaderCell. ( Although I think this is a rare use case, but one the consumer can handle )

Click events should bubble. Clicking any child of the Table.Header will invoke an onClick on the Table.Header naturally through event bubbling / propagation without doing the cloning logic here.

Something along these lines is what I had in mind for this change.

const TableHeader = ({ children, ...customProps }) => {

  let childrenArray = React.Children.toArray(children);
  if (childrenArray.length > 16) {
    // eslint-disable-next-line no-console
    console.log(`Number of Columns are ${childrenArray.length}. This is more than columns limit`);
    childrenArray = childrenArray.slice(0, 16);
  }

  return (
    <thead {...customProps}>
      <tr>
        {childrenArray}
      </tr>
    </thead>
  );
};
0reactions
bjankordcommented, Mar 30, 2018

This is available in terra-table@2.3.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Js: How to pass table row data as props to another ...
I want to pass the table row data as props to another page by clicking the buttons and display the data. But I...
Read more >
Columns Props · react-bootstrap-table2
This prop allow you to replace the value when table sorting. ... It allows to customize the style of header cell when this...
Read more >
A server side external sorting and filtering example #2033
In my case react-table is a reusable component, written in .tsx . So, in the Table component a function was passed as an...
Read more >
Documentation - jsGrid
headercss is a string representing css classes to be attached to the table header cell. If not specified, then css is attached instead....
Read more >
How to Replace Redux with React Hooks and the Context API
One of the biggest problems was “prop drilling”, which was common ... a single setValue() function for overwriting existing state values.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found