I am using antd date picker in my react application. I want to show a custom message while hovering or clicking on the dates. but it is not working as expected, the tooltip is not showing while hovering over the dates
import React from 'react';
import { DatePicker, Space, Tooltip } from 'antd';
const App = () => {
const renderDatePickerCell = (date) => {
return <Tooltip title="prompt text">
<span>{date.date()}</span>
</Tooltip>
};
const onChange = (date, dateString) => {
console.log(date, dateString);
};
return (
<Space direction="vertical">
<DatePicker onChange={onChange} cellRender={renderDatePickerCell} />
</Space>
);
};
export default App;
2
Answers
To show a tooltip when hovering over the dates, you can’t directly use the
Tooltip
component as a child of thespan
. Instead, you should use CSS to apply the tooltip effect.You can achieve this by defining a CSS class for the tooltip and applying it to your date cells.
In your CSS file (e.g.,
App.css
), you can define the styles for the tooltip effect:This CSS code creates a custom tooltip that is initially hidden (with
visibility: hidden
) and becomes visible when you hover over the date cell.Following antd custom cell rendering docs you can show tooltips in every day/date option in picker using
useCallback
hook:You can find a working example here.
OBS: If you don’t add
className="ant-picker-cell-inner"
your tooltips will not show.