Need to set focus on newly created row in react bootstrap table 2
I need to set focus on the newly created table as the content data in the form of an array and below the table, I have the button of Add new row which inserts a new item in the array and render the table with a blank row. The issue is I need to set focus on the first cell of the newly added row, I am using ref to set the focus as I can see the only way to set the ref to the table is ref={n=>this.node=n} and while adding a new row I am using this.setState({table: this.state.table, item},()=>{this.node.current.focus();})
But the error I am getting is can't set focus to null
Code I am using: const columns = [....some columns here, footer herewith add new row button footerEvents:{ onClick:()=>{ const tableData = [...this.state.tableData]; const item = newly created row this.setState({tableData: [...this.state.tableData, item],} , () => {this.reactTableNode.focus(); } ); <BootstrapTable keyField="id" data={this.state.tableData} columns={columns} cellEdit={this.cellEdit} hover ref={(node: any) => (this.reactTableNode = node)} />
Is there any way we can use focus in the react-bootstrap table which can help us in focus and scrolling the table div features.
@AllenFang needs your insight in this.
Issue resolved we can do:
formatter: ( cell, row, rowIndex ) => {
if (any condition) {
return (
<div className="edited">
<Form.Group>
<Form.Control
ref={this.newRow}
type="text"
onBlur={(e: any) => this.saveNewRow(e, row)}
/>
</Form.Group>
</div>
);
}
}
this will create the ref on the new created row and if you want to set focus you can do: this.newServiceLevelRow.current.focus();