| 1 |
import { useState } from 'react'; |
| 2 |
import { Slider, SliderOnChangeEvent, Button, Content } from '@patternfly/react-core'; |
| 3 |
import MinusIcon from '@patternfly/react-icons/dist/esm/icons/minus-icon'; |
| 4 |
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon'; |
| 5 |
import LockIcon from '@patternfly/react-icons/dist/esm/icons/lock-icon'; |
| 6 |
import LockOpenIcon from '@patternfly/react-icons/dist/esm/icons/lock-open-icon'; |
| 7 |
|
| 8 |
export const SliderActions: React.FunctionComponent = () => { |
| 9 |
const [value1, setValue1] = useState(50); |
| 10 |
const [value2, setValue2] = useState(50); |
| 11 |
const [inputValue, setInputValue] = useState(50); |
| 12 |
const [isDisabled, setIsDisabled] = useState(false); |
| 13 |
|
| 14 |
const onChange1 = (_event: SliderOnChangeEvent, value: number) => { |
| 15 |
setValue1(Math.floor(Number(value))); |
| 16 |
}; |
| 17 |
|
| 18 |
const onChange2 = ( |
| 19 |
_event: SliderOnChangeEvent, |
| 20 |
value: number, |
| 21 |
inputValue: number, |
| 22 |
setLocalInputValue: React.Dispatch<React.SetStateAction<number>> |
| 23 |
) => { |
| 24 |
let newValue; |
| 25 |
if (inputValue === undefined) { |
| 26 |
newValue = Math.floor(Number(value)); |
| 27 |
} else { |
| 28 |
if (inputValue > 100) { |
| 29 |
newValue = 100; |
| 30 |
setLocalInputValue(100); |
| 31 |
} else if (inputValue < 0) { |
| 32 |
newValue = 0; |
| 33 |
setLocalInputValue(0); |
| 34 |
} else { |
| 35 |
newValue = Math.floor(inputValue); |
| 36 |
} |
| 37 |
} |
| 38 |
setValue2(newValue); |
| 39 |
setInputValue(newValue); |
| 40 |
}; |
| 41 |
|
| 42 |
const onMinusClick = () => { |
| 43 |
const newValue = value1 - 1; |
| 44 |
if (newValue >= 0) { |
| 45 |
setValue1(newValue); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
const onPlusClick = () => { |
| 50 |
const newValue = value1 + 1; |
| 51 |
if (newValue <= 100) { |
| 52 |
setValue1(newValue); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
const buildAction = (isDisabled: boolean) => ( |
| 57 |
<Button |
| 58 |
variant="plain" |
| 59 |
aria-label={isDisabled ? 'Lock' : 'Unlock'} |
| 60 |
onClick={() => setIsDisabled(!isDisabled)} |
| 61 |
icon={isDisabled ? <LockIcon /> : <LockOpenIcon />} |
| 62 |
/> |
| 63 |
); |
| 64 |
|
| 65 |
return ( |
| 66 |
<> |
| 67 |
<Content component="h3">Slider value is: {value1}</Content> |
| 68 |
<Slider |
| 69 |
value={value1} |
| 70 |
onChange={onChange1} |
| 71 |
startActions={<Button variant="plain" aria-label="Minus" onClick={onMinusClick} icon={<MinusIcon />} />} |
| 72 |
endActions={<Button variant="plain" aria-label="Plus" onClick={onPlusClick} icon={<PlusIcon />} />} |
| 73 |
/> |
| 74 |
<br /> |
| 75 |
<br /> |
| 76 |
<Slider |
| 77 |
value={value2} |
| 78 |
inputValue={inputValue} |
| 79 |
onChange={onChange2} |
| 80 |
inputLabel="%" |
| 81 |
isInputVisible |
| 82 |
isDisabled={isDisabled} |
| 83 |
endActions={buildAction(isDisabled)} |
| 84 |
/> |
| 85 |
</> |
| 86 |
); |
| 87 |
}; |