Material-table: Adding textfield in toolbar only allows single char typing

Created on 28 Apr 2019  路  4Comments  路  Source: mbrn/material-table

Describe the bug
A clear and concise description of what the bug is.

When adding a textfield to the toolbar component, typing into it allows allows 1 character before it renders the whole component and focus is lost.

To Reproduce
Steps to reproduce the behavior:

  1. Add a textfield to the toolbar component
    Link it鈥檚 value to this.state and attach a change handler
    Run
    Type into the text field

Expected behavior
Text should be input as typed, beyond 1 char, and maintain focus

Desktop (please complete the following information):

  • os 10.14.3
    Chrome 58
bug

Most helpful comment

Actually this is not about material-table. If you use functional component to override toolbar it will rerender your textfield on every change. So it will be another textfield and loses focus.

But you can use it in a component like this.

 <MaterialTable
                  ref={this.tableRef}
                  columns={this.state.columns}
                  data={this.state.data}
                  title="Demo Title"
                  options={{
                  }}
                  components={{
                    Toolbar: NewBar
                  }}
                />

class NewBar extends React.Component {
  state = {
    text: 'Abc'
  }

  render() {
    return (
      <div>
        <MTableToolbar {...this.props} />
        <TextField value={this.state.text}
          onChange={event => {
            this.setState({ text: event.target.value });
          }}
        />
      </div>
    )
  }
}

All 4 comments

Actually this is not about material-table. If you use functional component to override toolbar it will rerender your textfield on every change. So it will be another textfield and loses focus.

But you can use it in a component like this.

 <MaterialTable
                  ref={this.tableRef}
                  columns={this.state.columns}
                  data={this.state.data}
                  title="Demo Title"
                  options={{
                  }}
                  components={{
                    Toolbar: NewBar
                  }}
                />

class NewBar extends React.Component {
  state = {
    text: 'Abc'
  }

  render() {
    return (
      <div>
        <MTableToolbar {...this.props} />
        <TextField value={this.state.text}
          onChange={event => {
            this.setState({ text: event.target.value });
          }}
        />
      </div>
    )
  }
}

@mbrn any ideas on how I can pass some props from MaterialTable parent component to NewBar from your example?

@Hatko did you find any solution ?

@osky9913, just use function expression or wrapper component
Toolbar: (props) => <Newbar {...props} yourProp={yourProp} />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VipinJoshi picture VipinJoshi  路  3Comments

Likurg2010 picture Likurg2010  路  3Comments

balibou picture balibou  路  3Comments

bohrsty picture bohrsty  路  3Comments

victorwvieira picture victorwvieira  路  3Comments