Created
December 26, 2018 17:49
-
-
Save iinsta/1a97f5b26ad08a287bf227d13b30cc3a to your computer and use it in GitHub Desktop.
react simple table component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
export default class Table extends Component { | |
render() { | |
const { keyMapper, records, ...rest } = this.props; | |
return ( | |
<table {...rest}> | |
<thead> | |
<tr> | |
{/* 渲染头部 */} | |
{keyMapper.map((item, i) => { | |
const { filter, hFilter, key, title, thProps } = item; | |
return ( | |
<th key={key || i} {...thProps}> | |
<div>{hFilter ? hFilter(key, item) : title}</div> | |
</th> | |
); | |
})} | |
</tr> | |
</thead> | |
{/* 渲染记录 */} | |
<tbody> | |
{records.map((record, i) => { | |
return ( | |
<tr key={i}> | |
{keyMapper.map((item, idx) => { | |
const { filter, key, title, tdProps } = item; | |
return ( | |
<td key={key || idx} {...tdProps}> | |
<div> | |
{filter ? filter(record[key], item) : record[key]} | |
</div> | |
</td> | |
); | |
})} | |
</tr> | |
); | |
})} | |
</tbody> | |
</table> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment