| 1 |
import { useState } from 'react'; |
| 2 |
import { Select, SelectOption, SelectList, MenuToggle, MenuToggleElement } from '@patternfly/react-core'; |
| 3 |
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon'; |
| 4 |
|
| 5 |
export const SelectOptionVariations: React.FunctionComponent = () => { |
| 6 |
const [isOpen, setIsOpen] = useState(false); |
| 7 |
const [selected, setSelected] = useState<string>('Select a value'); |
| 8 |
|
| 9 |
const onToggleClick = () => { |
| 10 |
setIsOpen(!isOpen); |
| 11 |
}; |
| 12 |
|
| 13 |
const onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => { |
| 14 |
// eslint-disable-next-line no-console |
| 15 |
console.log('selected', value); |
| 16 |
|
| 17 |
setSelected(value as string); |
| 18 |
setIsOpen(false); |
| 19 |
}; |
| 20 |
|
| 21 |
const toggle = (toggleRef: React.Ref<MenuToggleElement>) => ( |
| 22 |
<MenuToggle |
| 23 |
ref={toggleRef} |
| 24 |
onClick={onToggleClick} |
| 25 |
isExpanded={isOpen} |
| 26 |
style={ |
| 27 |
{ |
| 28 |
width: '200px' |
| 29 |
} as React.CSSProperties |
| 30 |
} |
| 31 |
> |
| 32 |
{selected} |
| 33 |
</MenuToggle> |
| 34 |
); |
| 35 |
|
| 36 |
return ( |
| 37 |
<Select |
| 38 |
id="option-variations-select" |
| 39 |
isOpen={isOpen} |
| 40 |
selected={selected} |
| 41 |
onSelect={onSelect} |
| 42 |
onOpenChange={(isOpen) => setIsOpen(isOpen)} |
| 43 |
toggle={toggle} |
| 44 |
shouldFocusToggleOnSelect |
| 45 |
> |
| 46 |
<SelectList> |
| 47 |
<SelectOption value="Basic option">Basic option</SelectOption> |
| 48 |
<SelectOption value="Option with description" description="This is a description"> |
| 49 |
Option with description |
| 50 |
</SelectOption> |
| 51 |
<SelectOption |
| 52 |
to="#" |
| 53 |
// Prevent default clicking functionality for example purposes only |
| 54 |
onClick={(event) => event.preventDefault()} |
| 55 |
value="Option with link" |
| 56 |
isExternalLink |
| 57 |
> |
| 58 |
Option with link |
| 59 |
</SelectOption> |
| 60 |
<SelectOption value="Option with icon" icon={<RhUiNotificationFillIcon />}> |
| 61 |
Option with icon |
| 62 |
</SelectOption> |
| 63 |
<SelectOption value="Disabled option" isDisabled> |
| 64 |
Disabled option |
| 65 |
</SelectOption> |
| 66 |
<SelectOption value="See Menu for additional variations!">See Menu for additional variations!</SelectOption> |
| 67 |
</SelectList> |
| 68 |
</Select> |
| 69 |
); |
| 70 |
}; |