import React from 'react'
import CodeHighlight from './components/codeHighlight'
import ReactTable from '../src/index'
class MyTable extends React.Component {
constructor (props, context) {
super(props, context)
this.renderEditable = this.renderEditable.bind(this)
this.state = {
data: [
{ firstName: 'Lucy', lastName: 'Marks' },
{ firstName: 'Bejamin', lastName: 'Pike' }
]
}
this.columns = [
{
Header: 'First Name',
accessor: 'firstName',
Cell: this.renderEditable
},
{
Header: 'Last Name',
accessor: 'lastName',
Cell: this.renderEditable
},
{
Header: 'Full Name',
id: 'full',
accessor: d => d.firstName + ' ' + d.lastName
}
]
}
renderEditable (cellInfo) {
return (
{
const data = [...this.state.data]
data[cellInfo.index][cellInfo.column.id] = e.target.textContent
this.setState({data: data})
}}>{this.state.data[cellInfo.index][cellInfo.column.id]}
)
}
render () {
return ()
}
}
function getCode () {
return `
class MyTable extends React.Component {
constructor(props, context) {
super(props, context);
this.renderEditable = this.renderEditable.bind(this);
this.state = {
data: [
{ firstName: 'Lucy', lastName: 'Marks' },
{ firstName: 'Bejamin', lastName: 'Pike' }
]
};
this.columns = [
{
Header: 'First Name',
accessor: 'firstName',
Cell: this.renderEditable
},
{
Header: 'Last Name',
accessor: 'lastName',
Cell: this.renderEditable
},
{
Header: 'Full Name',
id: 'full',
accessor: d => d.firstName + ' ' + d.lastName
}
];
}
renderEditable(cellInfo) {
return ( {
const data = [...this.state.data];
data[cellInfo.index][cellInfo.column.id] = e.target.textContent;
this.setState({data: data});
}}>{this.state.data[cellInfo.index][cellInfo.column.id]}
);
}
render() {
return ();
}
}
`
}
export default () => {
return (
First two columns are editable just by clicking into them using the contentEditable attribute. Last column (Full Name) is computed from the first two.
{() => getCode()}
)
}