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

Add support to Table to allow components that extend Column #748

Merged
merged 1 commit into from
Jul 20, 2017
Merged

Add support to Table to allow components that extend Column #748

merged 1 commit into from
Jul 20, 2017

Conversation

CptLemming
Copy link
Contributor

This change allows the Table component to accept children that directly extend the Column component.

This allows me to create many Column types I can use directly in a Table, making code reuse simpler.

Creating custom columns

// Changing the default cellDataGetter to use lodash's `get` function
const cellDataGetter = ({ dataKey, rowData }) => {
  return get(rowData, dataKey);
};

export class GetColumn extends Column {
  static defaultProps = {
    ...Column.defaultProps,
    cellDataGetter: cellDataGetter,
  };
}

// Adding a column to render links using `react-router`
const cellLinkRenderer = ({ cellData, rowData }) => {
  if ('to' in rowData) {
    let linkUrl = rowData.to;
    if (typeof rowData.to === 'function') {
      linkUrl = rowData.to({ rowData, cellData });
    }
    return (<Link to={ linkUrl }>{ cellData }</Link>);
  }
  return cellData;
};

export class LinkColumn extends Column {
  static defaultProps = {
    ...Column.defaultProps,
    cellRenderer: cellLinkRenderer,
  };
}

Using custom columns

const list = [{ node: { name: 'foo' }, link: '/bar' }];

<Table
  height={100}
  width={100}
  rowCount={list.length}
  rowGetter={({ index }) => list[index]}
>
  <GetColumn
    label="Name"
    dataKey="node.name"
    width={50}
  />
  <LinkColumn
    label="More info"
    dataKey="link"
    width={50}
  />
</Table>

Copy link
Owner

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

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

This (custom columns) was already possible, using a different syntax, but I think this is a reasonable simplifying change. 😄 Thanks.

@bvaughn bvaughn merged commit 8bc2fa4 into bvaughn:master Jul 20, 2017
@CptLemming CptLemming deleted the support-column-subclasses branch July 20, 2017 10:35
@bvaughn
Copy link
Owner

bvaughn commented Sep 18, 2017

This feature has been released in version 9.10.0. Thank you for contributing!

@genomics-geek
Copy link

Is there a way to do this with composition instead of inheritance? Seems like Table component is very strict on only accepting instance of Column

@bvaughn
Copy link
Owner

bvaughn commented Dec 13, 2017

@genomics-geek
Copy link

genomics-geek commented Dec 13, 2017

@bvaughn - Would you be against changing this to PropTypes.node ? Is there something preventing making this change?

@bvaughn
Copy link
Owner

bvaughn commented Dec 13, 2017

It just doesn't feel super compelling to me. I guess I'm not really against the idea but I don't intend to make the change b'c I don't see it as a pressing need and I'm super strapped for time.

@genomics-geek
Copy link

that's fair - It's already an awesome package :).

I will submit a PR for this, you can decide later if this makes sense to include it or not in a future release. Thanks for this package!

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.

3 participants