| 1 |
import { useState } from 'react'; |
| 2 |
import { Slider, SliderOnChangeEvent } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const SliderThumbValueInput: React.FunctionComponent = () => { |
| 5 |
const [value, setValue] = useState(50); |
| 6 |
const [inputValue, setInputValue] = useState(50); |
| 7 |
|
| 8 |
const onChange = ( |
| 9 |
_event: SliderOnChangeEvent, |
| 10 |
value: number, |
| 11 |
inputValue: number, |
| 12 |
setLocalInputValue: React.Dispatch<React.SetStateAction<number>> |
| 13 |
) => { |
| 14 |
let newValue; |
| 15 |
if (inputValue === undefined) { |
| 16 |
newValue = Number(value); |
| 17 |
} else { |
| 18 |
if (inputValue > 100) { |
| 19 |
newValue = 100; |
| 20 |
setLocalInputValue(100); |
| 21 |
} else if (inputValue < 0) { |
| 22 |
newValue = 0; |
| 23 |
setLocalInputValue(0); |
| 24 |
} else { |
| 25 |
newValue = Math.floor(inputValue); |
| 26 |
} |
| 27 |
} |
| 28 |
setValue(newValue); |
| 29 |
setInputValue(newValue); |
| 30 |
}; |
| 31 |
|
| 32 |
return ( |
| 33 |
<Slider |
| 34 |
value={value} |
| 35 |
isInputVisible |
| 36 |
inputValue={inputValue} |
| 37 |
inputLabel="%" |
| 38 |
inputPosition="aboveThumb" |
| 39 |
onChange={onChange} |
| 40 |
/> |
| 41 |
); |
| 42 |
}; |