Skip to content

Commit 2dde549

Browse files
committed
feat(Slider): add support for dynamic updates with input
1 parent 6f2385b commit 2dde549

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export interface SliderProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onCh
5656
isDisabled?: boolean;
5757
/** Flag to show value input field. */
5858
isInputVisible?: boolean;
59+
/** Flag indicating if the input value should also update the slider value as the user types. When false, the slider will update its value on blur or enter key press. */
60+
isInputLive?: boolean;
5961
/** @deprecated Use startActions instead. Actions placed at the start of the slider. */
6062
leftActions?: React.ReactNode;
6163
/** Actions placed at the start of the slider. */
@@ -97,6 +99,7 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
9799
isDisabled = false,
98100
isInputVisible = false,
99101
inputValue = 0,
102+
isInputLive = false,
100103
inputLabel,
101104
inputAriaLabel = 'Slider value input',
102105
thumbAriaLabel = 'Value',
@@ -145,8 +148,11 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
145148
const widthChars = useMemo(() => localInputValue.toString().length, [localInputValue]);
146149
const inputStyle = { [cssFormControlWidthChars.name]: widthChars } as React.CSSProperties;
147150

148-
const onChangeHandler = (_event: React.FormEvent<HTMLInputElement>, value: string) => {
149-
setLocalInputValue(Number(value));
151+
const onChangeHandler = (event: React.FormEvent<HTMLInputElement>, value: string) => {
152+
const newValue = Number(value);
153+
setLocalInputValue(newValue);
154+
155+
isInputLive && onChange(event, localValue, newValue, setLocalInputValue);
150156
};
151157

152158
const handleKeyPressOnInput = (event: React.KeyboardEvent) => {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from 'react';
2-
import { Slider, SliderOnChangeEvent } from '@patternfly/react-core';
2+
import { Checkbox, Slider, SliderOnChangeEvent } from '@patternfly/react-core';
33

44
export const SliderValueInput: React.FunctionComponent = () => {
55
const [valueDiscrete, setValueDiscrete] = useState(62.5);
@@ -8,6 +8,7 @@ export const SliderValueInput: React.FunctionComponent = () => {
88
const [inputValuePercent, setInputValuePercent] = useState(50);
99
const [valueContinuous, setValueContinuous] = useState(50);
1010
const [inputValueContinuous, setInputValueContinuous] = useState(50);
11+
const [isInputLive, setIsInputLive] = useState(false);
1112

1213
const stepsDiscrete = [
1314
{ value: 0, label: '0' },
@@ -154,9 +155,17 @@ export const SliderValueInput: React.FunctionComponent = () => {
154155

155156
return (
156157
<>
158+
<Checkbox
159+
id="isInputLiveCheckbox"
160+
label="isInputLive"
161+
isChecked={isInputLive}
162+
onChange={(_event: React.FormEvent<HTMLInputElement>, checked: boolean) => setIsInputLive(checked)}
163+
style={{ marginBottom: 20 }}
164+
/>
157165
<Slider
158166
value={valueDiscrete}
159167
isInputVisible
168+
isInputLive={isInputLive}
160169
inputValue={inputValueDiscrete}
161170
customSteps={stepsDiscrete}
162171
onChange={onChangeDiscrete}
@@ -165,6 +174,7 @@ export const SliderValueInput: React.FunctionComponent = () => {
165174
<Slider
166175
value={valuePercent}
167176
isInputVisible
177+
isInputLive={isInputLive}
168178
inputValue={inputValuePercent}
169179
inputLabel="%"
170180
onChange={onChangePercent}
@@ -174,6 +184,7 @@ export const SliderValueInput: React.FunctionComponent = () => {
174184
<Slider
175185
value={valueContinuous}
176186
isInputVisible
187+
isInputLive={isInputLive}
177188
inputValue={inputValueContinuous}
178189
inputLabel="%"
179190
onChange={onChangeContinuous}

0 commit comments

Comments
 (0)