I'm currently working on a table in React and need to have a select status on a column when clicking the top cell and being able to toggle that between the columns.
At this stage I roughly put together something like this:
const types = {
one: 'one',
two: 'two',
}
class Test extends Component {
constructor(props) {
super(props)
this.onOneClick = this.onClick.bind(this, types.one)
this.onTwoClick = this.onClick.bind(this, types.two)
}
onClick = column => {
this.props.onColumnChange(column)
}
render() {
const { column } = this.props
const classOne = classnames(styles.one, column === types.one ? styles.active : null)
const classTwo = classnames(styles.two, column === types.two ? styles.active : null)
return (
<section className={styles.columnSelection}>
<table className={styles.columns}>
<thead>
<tr>
<th>Select column</th>
<td className={classOne} onClick={this.onOneClick}>one</td>
<td className={classTwo} onClick={this.onTwoClick}>two</td>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<td className={classOne}>one</td>
<td className={classTwo}>two</td>
</tr>
<tr>
<th></th>
<td className={classOne}>one</td>
<td className={classTwo}>two</td>
</tr>
</tbody>
</table>
</section>
)
}
}
Test.propTypes = {
column: PropTypes.oneOf(Object.values(types)),
onColumnChange: PropTypes.func.isRequired,
}
I think this will probably achieve what I want but wondering if there are better ways to do that? also I don't think there is any nth-child selector like in jQuery to help me. There are few different reasons on why I need the table, so I'm not looking to change the HTML structure.