Skip to content

Commit 7b7ef1f

Browse files
committed
feat(Slider): add support for custom tooltip content
1 parent 8edf04a commit 7b7ef1f

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

packages/react-core/src/components/Slider/Slider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ export interface SliderProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onCh
4242
className?: string;
4343
/** Array of custom slider step objects (value and label of each step) for the slider. */
4444
customSteps?: SliderStepObject[];
45-
/* Adds a tooltip over the slider thumb containing the current value. */
45+
/** Enables a tooltip over the silder thumb. Defaults to the current value, or tooltipContent if provided. */
4646
hasTooltipOverThumb?: boolean;
47+
/** Content of the tooltip over the slider thumb. Defaults to the current value. */
48+
tooltipContent?: React.ReactNode;
4749
/** Accessible label for the input field. */
4850
inputAriaLabel?: string;
4951
/** Text label that is place after the input field. */
@@ -101,6 +103,7 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
101103
inputAriaLabel = 'Slider value input',
102104
thumbAriaLabel = 'Value',
103105
hasTooltipOverThumb = false,
106+
tooltipContent,
104107
inputPosition = 'end',
105108
onChange,
106109
leftActions,
@@ -477,7 +480,7 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
477480
className={css('pf-v6-m-tabular-nums')}
478481
triggerRef={thumbRef}
479482
entryDelay={0}
480-
content={findAriaTextValue()}
483+
content={tooltipContent ? tooltipContent : findAriaTextValue()}
481484
>
482485
{thumbComponent}
483486
</Tooltip>

packages/react-core/src/components/Slider/__tests__/Slider.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { render, screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
23
import { Slider } from '../Slider';
34
import { Button } from '../../Button';
45

@@ -77,6 +78,17 @@ describe('slider', () => {
7778
const { asFragment } = render(<Slider value={50} hasTooltipOverThumb />);
7879
expect(asFragment()).toMatchSnapshot();
7980
});
81+
82+
test('renders slider with custom tooltip content on thumb', async () => {
83+
const user = userEvent.setup();
84+
85+
render(<Slider value={50} hasTooltipOverThumb tooltipContent="Custom tooltip content" />);
86+
87+
await user.hover(screen.getByRole('slider'));
88+
89+
await screen.findByRole('tooltip');
90+
expect(screen.getByRole('tooltip')).toHaveTextContent('Custom tooltip content');
91+
});
8092
});
8193

8294
test('renders slider with aria-labelledby', () => {

packages/react-core/src/components/Slider/examples/Slider.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ import LockOpenIcon from '@patternfly/react-icons/dist/esm/icons/lock-open-icon'
5050

5151
```
5252

53+
### Custom step tooltip
54+
55+
```ts file="./SliderCustomTooltip.tsx"
56+
57+
```
58+
5359
## Types
5460

5561
### SliderOnChangeEvent
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useState } from 'react';
2+
import { Slider, SliderOnChangeEvent } from '@patternfly/react-core';
3+
4+
export const SliderCustomTooltip: React.FunctionComponent = () => {
5+
const [value, setValue] = useState(50);
6+
const steps = [
7+
{ value: 0, label: '0' },
8+
{ value: 12.5, label: '1', isLabelHidden: true },
9+
{ value: 25, label: '2' },
10+
{ value: 37.5, label: '3', isLabelHidden: true },
11+
{ value: 50, label: '4' },
12+
{ value: 62.5, label: '5', isLabelHidden: true },
13+
{ value: 75, label: '6' },
14+
{ value: 87.5, label: '7', isLabelHidden: true },
15+
{ value: 100, label: '8' }
16+
];
17+
18+
const displayValue = () => {
19+
const step = steps.find((step) => step.value === value);
20+
return step ? step.label : 0;
21+
};
22+
23+
const customTooltipContent = () => <div>Custom tooltip content for step: {displayValue()}</div>;
24+
25+
return (
26+
<Slider
27+
hasTooltipOverThumb
28+
tooltipContent={customTooltipContent()}
29+
value={value}
30+
onChange={(_event: SliderOnChangeEvent, value: number) => setValue(value)}
31+
customSteps={steps}
32+
/>
33+
);
34+
};

0 commit comments

Comments
 (0)