Adding tooltips
Adding a tooltip
You can add a default tooltip to a cell or your custom one. To add a tooltip, you should do the following:
- Import the Tooltip component
- To enable/disable tooltips, use the
tooltipparameter of thecolumnsproperty:- set it to false to disable
- set it to true or do not specify to show a tooltip by default
- define a function that takes an object to be processed as an argument and returns the string to show
- Wrap the Grid tag inside the Tooltip tag and bind it to the Grid
api(see how to access the Grid API in theapidocs). In React this is done by obtaining the Grid instance (ref) and storing it in a variable namedapi, then passing thatapito the Tooltip via theapiprop.
Example:
import { useState, useCallback } from "react";
import { getData } from "./common/data";
import { Grid, Tooltip } from "@svar-ui/react-grid";
export default function ExampleTooltip() {
const { data } = getData();
const columns = [
{ id: "id", width: 50, tooltip: false },
{
id: "city",
width: 100,
header: "City",
},
{
id: "firstName",
header: "First Name",
width: 150,
tooltip: false,
},
{
id: "lastName",
header: "Last Name",
width: 150,
tooltip: false,
},
{ id: "email", header: "Email" },
{ id: "companyName", header: "Company" },
{ id: "date", tooltip: (obj) => obj.date?.toDateString() },
];
// Keep the API object in the same variable name `api`
const [api, setApi] = useState(null);
// Use a callback ref to capture the Grid instance and store it as `api`
const setGridRef = useCallback((node) => {
setApi(node);
}, []);
return (
<Tooltip api={api}>
<Grid data={data} columns={columns} ref={setGridRef} />
</Tooltip>
);
}
Setting custom tooltip content
In the next example we add a custom React component as a tooltip:
- prepare and import your custom template as a React component
- import the Tooltip component and wrap the Grid tag into the Tooltip tag
- add the component (constructor) as the value of the Tooltip content attribute
- bind Tooltip to the Grid
apiobject (obtain the Grid instance and pass it as theapiprop)
import { useState, useCallback } from "react";
import { getData } from "../data";
import { Grid, Tooltip } from "@svar-ui/react-grid";
import YourCustomTooltip from "../custom/CustomTooltip.jsx";
export default function CustomTooltipExample() {
const { data, columns } = getData();
const [api, setApi] = useState(null);
const setGridRef = useCallback((node) => setApi(node), []);
return (
<Tooltip content={YourCustomTooltip} api={api}>
<Grid data={data} columns={columns} ref={setGridRef} />
</Tooltip>
);
}
Example of a custom component:
// ../custom/CustomTooltip.jsx
export default function CustomTooltip({ data }) {
return (
<div className="data">
<div className="text">{data.email}</div>
</div>
);
}
.data {
background-color: white;
}
.text {
font-size: 13px;
color: black;
}
Changing a tooltip style
The example below shows how to change the default style of tooltips via global CSS classes. Add this to your global stylesheet (or a CSS module/global styles file so the selector applies globally):
.wx-area .tooltip {
background-color: white;
color: black;
border: 2px solid black;
}