-
Notifications
You must be signed in to change notification settings - Fork 390
feat(Slider): add support for custom tooltip content #12531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,8 +42,10 @@ export interface SliderProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onCh | |||||
| className?: string; | ||||||
| /** Array of custom slider step objects (value and label of each step) for the slider. */ | ||||||
| customSteps?: SliderStepObject[]; | ||||||
| /* Adds a tooltip over the slider thumb containing the current value. */ | ||||||
| /** Enables a tooltip over the silder thumb. Defaults to the current value, or tooltipContent if provided. */ | ||||||
| hasTooltipOverThumb?: boolean; | ||||||
| /** Content of the tooltip over the slider thumb. Defaults to the current value. */ | ||||||
| tooltipContent?: React.ReactNode; | ||||||
| /** Accessible label for the input field. */ | ||||||
| inputAriaLabel?: string; | ||||||
| /** Text label that is place after the input field. */ | ||||||
|
|
@@ -81,8 +83,10 @@ export interface SliderProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onCh | |||||
| showTicks?: boolean; | ||||||
| /** The step interval. */ | ||||||
| step?: number; | ||||||
| /* Accessible label for the slider thumb. */ | ||||||
| /** Accessible label for the slider thumb. */ | ||||||
| thumbAriaLabel?: string; | ||||||
| /** Accessible text for the current value of the slider. Defaults to the current value. */ | ||||||
| thumbAriaValueText?: string; | ||||||
| /** Current value of the slider. */ | ||||||
| value?: number; | ||||||
| } | ||||||
|
|
@@ -100,7 +104,9 @@ export const Slider: React.FunctionComponent<SliderProps> = ({ | |||||
| inputLabel, | ||||||
| inputAriaLabel = 'Slider value input', | ||||||
| thumbAriaLabel = 'Value', | ||||||
| thumbAriaValueText, | ||||||
| hasTooltipOverThumb = false, | ||||||
| tooltipContent, | ||||||
| inputPosition = 'end', | ||||||
| onChange, | ||||||
| leftActions, | ||||||
|
|
@@ -425,8 +431,8 @@ export const Slider: React.FunctionComponent<SliderProps> = ({ | |||||
| aria-valuemin={customSteps ? customSteps[0].value : min} | ||||||
| aria-valuemax={customSteps ? customSteps[customSteps.length - 1].value : max} | ||||||
| aria-valuenow={localValue} | ||||||
| aria-valuetext={findAriaTextValue()} | ||||||
| aria-label={thumbAriaLabel} | ||||||
| aria-valuetext={thumbAriaValueText ? thumbAriaValueText : findAriaTextValue()} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
|
||||||
| aria-label={thumbAriaLabel ? thumbAriaLabel : thumbAriaLabel} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to use a different value for one of these? |
||||||
| aria-disabled={isDisabled} | ||||||
| aria-describedby={ariaDescribedby} | ||||||
| aria-labelledby={ariaLabelledby} | ||||||
|
|
@@ -477,7 +483,7 @@ export const Slider: React.FunctionComponent<SliderProps> = ({ | |||||
| className={css('pf-v6-m-tabular-nums')} | ||||||
| triggerRef={thumbRef} | ||||||
| entryDelay={0} | ||||||
| content={findAriaTextValue()} | ||||||
| content={tooltipContent ? tooltipContent : findAriaTextValue()} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same nit as above |
||||||
| > | ||||||
| {thumbComponent} | ||||||
| </Tooltip> | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,12 @@ import LockOpenIcon from '@patternfly/react-icons/dist/esm/icons/lock-open-icon' | |
|
|
||
| ``` | ||
|
|
||
| ### Custom step tooltip | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To add some example verbiage:
|
||
|
|
||
| ```ts file="./SliderCustomTooltip.tsx" | ||
|
|
||
| ``` | ||
|
|
||
| ## Types | ||
|
|
||
| ### SliderOnChangeEvent | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { useState } from 'react'; | ||
| import { Slider, SliderOnChangeEvent } from '@patternfly/react-core'; | ||
|
|
||
| export const SliderCustomTooltip: React.FunctionComponent = () => { | ||
| const [value, setValue] = useState(50); | ||
| const steps = [ | ||
| { value: 0, label: '0' }, | ||
| { value: 12.5, label: '1', isLabelHidden: true }, | ||
| { value: 25, label: '2' }, | ||
| { value: 37.5, label: '3', isLabelHidden: true }, | ||
| { value: 50, label: '4' }, | ||
| { value: 62.5, label: '5', isLabelHidden: true }, | ||
| { value: 75, label: '6' }, | ||
| { value: 87.5, label: '7', isLabelHidden: true }, | ||
| { value: 100, label: '8' } | ||
| ]; | ||
|
|
||
| const displayValue = () => { | ||
| const step = steps.find((step) => step.value === value); | ||
| return step ? step.label : 0; | ||
| }; | ||
|
|
||
| const customTooltipContent = () => <div>Custom tooltip content for step: {displayValue()}</div>; | ||
|
|
||
| return ( | ||
| <Slider | ||
| hasTooltipOverThumb | ||
| tooltipContent={customTooltipContent()} | ||
| value={value} | ||
| onChange={(_event: SliderOnChangeEvent, value: number) => setValue(value)} | ||
| customSteps={steps} | ||
| /> | ||
|
Comment on lines
+26
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May not be absolutely necessary with the above example verbiage added in, but we could add a second slider tothis example showing an implementation more similar to the original issue, where each step is a date timestamp. If we do, maybe we have the slider steps min and max values be 1-10 with whole number intervals |
||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a
tooltipPropsto spread to the Tooltip