| 1 |
import { useState } from 'react'; |
| 2 |
import { Popover, PopoverPosition, Checkbox, Button } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const PopoverAdvanced: React.FunctionComponent = () => { |
| 5 |
const [position, setPosition] = useState(PopoverPosition.auto); |
| 6 |
const [show, setShow] = useState(false); |
| 7 |
const [keepInViewChecked, setKeepInViewChecked] = useState(true); |
| 8 |
|
| 9 |
const handleKeepInViewChange = (checked: boolean) => { |
| 10 |
setKeepInViewChecked(checked); |
| 11 |
}; |
| 12 |
|
| 13 |
const handleProgrammaticChange = (checked: boolean) => { |
| 14 |
setShow(checked); |
| 15 |
}; |
| 16 |
|
| 17 |
const shouldClose = (_event, _fn) => { |
| 18 |
setShow(false); |
| 19 |
}; |
| 20 |
|
| 21 |
const shouldOpen = (_event, _fn) => { |
| 22 |
setShow(true); |
| 23 |
}; |
| 24 |
|
| 25 |
return ( |
| 26 |
<> |
| 27 |
<div> |
| 28 |
<span style={{ paddingRight: '10px' }}>Popover position</span> |
| 29 |
<select |
| 30 |
aria-label="Popover position" |
| 31 |
onChange={(event) => { |
| 32 |
setPosition(event.target.value as PopoverPosition); |
| 33 |
}} |
| 34 |
> |
| 35 |
{Object.values(PopoverPosition).map((position) => ( |
| 36 |
<option key={position} value={position}> |
| 37 |
{position} |
| 38 |
</option> |
| 39 |
))} |
| 40 |
</select> |
| 41 |
<Checkbox |
| 42 |
label="Flip popover if the position falls outside the view" |
| 43 |
isChecked={keepInViewChecked} |
| 44 |
onChange={(_event, checked) => handleKeepInViewChange(checked)} |
| 45 |
aria-label="Keep in view" |
| 46 |
id="popover-advanced-check-1" |
| 47 |
/> |
| 48 |
<Checkbox |
| 49 |
label="Toggle popover from outside" |
| 50 |
isChecked={show} |
| 51 |
onChange={(_event, checked) => handleProgrammaticChange(checked)} |
| 52 |
aria-label="Toggle popover from outside" |
| 53 |
id="popover-advanced-check-2" |
| 54 |
/> |
| 55 |
</div> |
| 56 |
|
| 57 |
<div style={{ margin: '50px' }}> |
| 58 |
<Popover |
| 59 |
aria-label="Advanced popover usages example" |
| 60 |
position={position} |
| 61 |
hideOnOutsideClick={false} |
| 62 |
isVisible={show} |
| 63 |
shouldClose={shouldClose} |
| 64 |
shouldOpen={shouldOpen} |
| 65 |
enableFlip={keepInViewChecked} |
| 66 |
headerContent={<div>Popover header</div>} |
| 67 |
bodyContent={ |
| 68 |
<div> |
| 69 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id feugiat augue, nec fringilla turpis. |
| 70 |
</div> |
| 71 |
} |
| 72 |
footerContent="Popover footer" |
| 73 |
> |
| 74 |
<Button>Toggle popover</Button> |
| 75 |
</Popover> |
| 76 |
</div> |
| 77 |
</> |
| 78 |
); |
| 79 |
}; |