Skip to content

Commit

Permalink
feat(slider): expose formatLabel to value tooltip
Browse files Browse the repository at this point in the history
- expose formatLabel to thumbWrapper for twoHandles as well as single handle.
- add examples on storybooks on how it could be used
  • Loading branch information
dkaushik95 committed Jun 27, 2024
1 parent 0d2376a commit 84ce93b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
35 changes: 35 additions & 0 deletions packages/react/src/components/Slider/Slider.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ export const SliderWithHiddenInputs = () => (
/>
);

export const SliderWithHiddenInputsAndFormatLabel = () => (
<Slider
labelText="Slider label with percentage"
value={50}
min={0}
max={100}
stepMultiplier={10}
step={1}
noValidate
hideTextInput
formatLabel={(val) => `${val}%`}
/>
);

export const SliderWithHiddenInputsAndCustomFormat = () => (
<Slider
labelText="Slider label with low/medium/high"
value={50}
min={0}
max={100}
stepMultiplier={10}
step={1}
noValidate
hideTextInput
formatLabel={(val) => {
if (val < 25) {
return 'Low';
} else if (val > 75) {
return 'High';
}
return 'Medium';
}}
/>
);

export const ControlledSlider = () => {
const [val, setVal] = useState(87);
return (
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export interface SliderProps
disabled?: boolean;

/**
* The callback to format the label associated with the minimum/maximum value.
* The callback to format the label associated with the minimum/maximum value and the value tooltip when hideTextInput is true.
*/
formatLabel?: (value: number, label: string | undefined) => string;

Expand Down Expand Up @@ -1521,7 +1521,7 @@ class Slider extends PureComponent<SliderProps> {
<ThumbWrapper
hasTooltip={hideTextInput}
className={lowerThumbWrapperClasses}
label={`${value}`}
label={`${formatLabel(value, '')}`}
align="top"
{...lowerThumbWrapperProps}>
<div
Expand Down Expand Up @@ -1555,7 +1555,7 @@ class Slider extends PureComponent<SliderProps> {
<ThumbWrapper
hasTooltip={hideTextInput}
className={upperThumbWrapperClasses}
label={`${valueUpper}`}
label={`${formatLabel(valueUpper || 0, '')}`}
align="top"
{...upperThumbWrapperProps}>
<div
Expand Down

0 comments on commit 84ce93b

Please sign in to comment.