This might be a good use-case for a portal.
This allows us to more flexibly decide where we render our button. Using portals we can make the button a sibbling of the search input:
const gridjsHeadRoot = document.getElementsByClassName("gridjs-head");
class GridHeaderButton extends React.Component {
constructor(props) {
super(props);
this.el = document.createElement("button");
this.el.innerText = "Click";
this.el.style.cssText = `
background-color: #0069d9;
color: #fff;
border-radius: .25rem;
padding: .375rem .75rem;
float: right
`;
this.el.onclick = function () {
// Do something
};
}
componentDidMount() {
gridjsHeadRoot[0].appendChild(this.el);
}
componentWillUnmount() {
gridjsHeadRoot[0].removeChild(this.el);
}
render() {
return ReactDOM.createPortal(this.props.children, this.el);
}
}
Then you can use it like this:
function MyCustomGrid() {
return (
<Grid
sort={true}
search={true} // This adds the search inp
columns={tableColumns}
data={tableData}
language={{
search: {
placeholder: "?? Search...",
},
}}
pagination={{
enabled: true,
limit: 2,
}}
/>
);
}
export default function App() {
return (
<div className="App">
<MyCustomGrid />
<GridHeaderButton />
</div>
);
}
The order of where you put GridHeaderButton
is important here. Because GridHeaderButton
targets an element rendered inside MyCustomGrid
which means you should put GridHeaderButton
below CustomGrid
or it will not work.
Sandbox Example
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…