Passing react element to title not working properly.
Hi, I tried to pass TextField to title of the Material Table but after one keystroke, TextField loses focus. I'm trying to export file name as the user input. Is there a way to remain focus after changing the value of the TextField?
<MaterialTable
options={{
exportButton: true,
exportFileName: title
}}
title={<TextField value={title} onChange={updateTitle} fullWidth id="title" name="title" label="Title" autoComplete="title" /> }
columns={column}
data={data}
/>
You can use the component override to achieve that like this:
components={{
Toolbar: props => (
<div>
<input style={{margin: 20}}/>
</div>
)}}
This will render a simple input instead of the header. But you can style it as you want.
I want to save the onChange title so I can use it for the exportFileName as above. With input, it doesn't work.
Ok so I created a codepen for you and it works just fine.
It works in the link you have showed me but idk why it is not working for me in the console.(Gives same behaviour)
below is my code and I'm using typescript.
<MaterialTable
options={{
exportButton: true,
exportFileName: title
}}
title={
<TextField value={title} onChange={e => setTitle(e.target.value)} fullWidth id="title" name="title" label="Title" autoComplete="title" />
}
columns={column}
data={data}
components={{
Toolbar: props => (
<div>
<MTableToolbar {...props} />
<Grid container direction="row" justify="flex-end" alignItems="center">
<Button className={classes.import} variant="outlined" color="default" startIcon={<CloudUploadIcon />}>
Import
</Button>
</Grid>
</div>
),
}}
I think the components part is the problem. I tried to paste it in the codepan you have created and it doesn't work either. Try the below code. That is the behaviour I am getting.
import React, { useState } from "react";
import ReactDOM from "react-dom";
import MaterialTable, {MTableToolbar} from "material-table";
import { TextField, Button, Grid } from "@material-ui/core";
import "./styles.css";
function App() {
const [title, setTitle] = useState("");
const column = [
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]
const [data, setData] = useState([
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{
name: 'Zerya Betül',
surname: 'Baran',
birthYear: 2017,
birthCity: 34,
},
])
return (
<div className="App">
<MaterialTable
options={{
exportButton: true,
exportFileName: title
}}
columns={column}
data={data}
title={
<TextField
value={title}
onChange={e => setTitle(e.target.value)}
fullWidth
id="title"
name="title"
label="Title"
autoComplete="title"
/>
}
components={{
Toolbar: props => (
<div>
<MTableToolbar {...props} />
<Grid container direction="row" justify="flex-end" alignItems="center">
<Button variant="outlined" color="default">
Import
</Button>
</Grid>
</div>
),
}}
/>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Becauseof your custom component, the header gets rerenderd in every hit and thus loses focus. Let me think about a solution
In addition, if possible, is there a way to put the custom component next to the other buttons?(export, add)
I would recommend to ask these questions on stack overflow the next time. To fix the loss of focus, change onChange to onBlur and value to defaultValue. This will work for your case since you do not rerender the table in every key hit. To add custom icons, you could write your own header to do whatever you like by providing your own toolbar with the given props.