| 1 |
import { forwardRef, useEffect, useRef } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import { Menu, MenuContent, MenuProps } from '../Menu'; |
| 4 |
import { Popper, PopperOptions } from '../../helpers/Popper/Popper'; |
| 5 |
import { getOUIAProps, OUIAProps, onToggleArrowKeydownDefault } from '../../helpers'; |
| 6 |
import { useOUIAId } from '../../helpers/OUIA/ouia'; |
| 7 |
import type { SelectOptionProps } from './SelectOption'; |
| 8 |
|
| 9 |
/** @deprecated Use PopperOptions instead */ |
| 10 |
export type SelectPopperProps = PopperOptions; |
| 11 |
|
| 12 |
export interface SelectToggleProps { |
| 13 |
/** Select toggle node. */ |
| 14 |
toggleNode: React.ReactNode; |
| 15 |
/** Reference to the toggle. */ |
| 16 |
toggleRef?: React.RefObject<HTMLButtonElement | null>; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* See the Menu documentation for additional props that may be passed. |
| 21 |
*/ |
| 22 |
|
| 23 |
export interface SelectProps extends MenuProps, OUIAProps { |
| 24 |
/** Anything which can be rendered in a select */ |
| 25 |
children?: React.ReactNode; |
| 26 |
/** Classes applied to root element of select */ |
| 27 |
className?: string; |
| 28 |
/** Flag to indicate if select is open */ |
| 29 |
isOpen?: boolean; |
| 30 |
/** Single select option value for single select menus, or array of select option values for multi select. You can also specify isSelected on the SelectOption. */ |
| 31 |
selected?: any | any[]; |
| 32 |
/** Select toggle. The toggle should either be a renderer function which forwards the given toggle ref, or a direct ReactNode that should be passed along with the toggleRef property. */ |
| 33 |
toggle: SelectToggleProps | ((toggleRef: React.RefObject<any>) => React.ReactNode); |
| 34 |
/** Flag indicating the toggle should be focused after a selection. If this use case is too restrictive, the optional toggleRef property with a node toggle may be used to control focus. */ |
| 35 |
shouldFocusToggleOnSelect?: boolean; |
| 36 |
/** @beta Flag indicating the first menu item should be focused after opening the menu. */ |
| 37 |
shouldFocusFirstItemOnOpen?: boolean; |
| 38 |
/** Function callback when user selects an option. */ |
| 39 |
onSelect?: (event?: React.MouseEvent<Element, MouseEvent>, value?: SelectOptionProps['value']) => void; |
| 40 |
/** Callback to allow the select component to change the open state of the menu. |
| 41 |
* Triggered by clicking outside of the menu, or by pressing any keys specified in onOpenChangeKeys. */ |
| 42 |
onOpenChange?: (isOpen: boolean) => void; |
| 43 |
/** Keys that trigger onOpenChange, defaults to tab and escape. It is highly recommended to include Escape in the array, while Tab may be omitted if the menu contains non-menu items that are focusable. */ |
| 44 |
onOpenChangeKeys?: string[]; |
| 45 |
/** Callback to override the toggle keydown behavior. By default, when the toggle has focus and the menu is open, pressing the up/down arrow keys will focus a valid non-disabled menu item - the first item for the down arrow key and last item for the up arrow key. */ |
| 46 |
onToggleKeydown?: (event: KeyboardEvent) => void; |
| 47 |
/** Select variant. For typeahead variant focus won't shift to menu items when pressing up/down arrows. */ |
| 48 |
variant?: 'default' | 'typeahead'; |
| 49 |
/** Indicates if the select should be without the outer box-shadow */ |
| 50 |
isPlain?: boolean; |
| 51 |
/** @hide Forwarded ref */ |
| 52 |
innerRef?: React.Ref<HTMLDivElement>; |
| 53 |
/** z-index of the select menu */ |
| 54 |
zIndex?: number; |
| 55 |
/** Determines the accessible role of the select. For a checkbox select pass in "menu". */ |
| 56 |
role?: string; |
| 57 |
/** Additional properties to pass to the popper */ |
| 58 |
popperProps?: PopperOptions; |
| 59 |
/** Height of the select menu */ |
| 60 |
menuHeight?: string; |
| 61 |
/** Maximum height of select menu */ |
| 62 |
maxMenuHeight?: string; |
| 63 |
/** Indicates if the select menu should be scrollable */ |
| 64 |
isScrollable?: boolean; |
| 65 |
/** Flag indicating if scroll on focus of the first menu item should occur. */ |
| 66 |
shouldPreventScrollOnItemFocus?: boolean; |
| 67 |
/** Time in ms to wait before firing the toggles' focus event. Defaults to 0 */ |
| 68 |
focusTimeoutDelay?: number; |
| 69 |
} |
| 70 |
|
| 71 |
const SelectBase: React.FunctionComponent<SelectProps & OUIAProps> = ({ |
| 72 |
children, |
| 73 |
className, |
| 74 |
onSelect, |
| 75 |
isOpen, |
| 76 |
selected, |
| 77 |
toggle, |
| 78 |
shouldFocusToggleOnSelect = false, |
| 79 |
shouldFocusFirstItemOnOpen = false, |
| 80 |
onOpenChange, |
| 81 |
onOpenChangeKeys = ['Escape', 'Tab'], |
| 82 |
onToggleKeydown, |
| 83 |
variant, |
| 84 |
isPlain, |
| 85 |
innerRef, |
| 86 |
zIndex = 9999, |
| 87 |
role = 'listbox', |
| 88 |
popperProps, |
| 89 |
menuHeight, |
| 90 |
maxMenuHeight, |
| 91 |
isScrollable, |
| 92 |
shouldPreventScrollOnItemFocus = true, |
| 93 |
focusTimeoutDelay = 0, |
| 94 |
...props |
| 95 |
}: SelectProps & OUIAProps) => { |
| 96 |
const generatedOuiaId = useOUIAId(Select.displayName, props.ouiaId); |
| 97 |
const localMenuRef = useRef<HTMLDivElement>(undefined); |
| 98 |
const localToggleRef = useRef<HTMLButtonElement>(undefined); |
| 99 |
|
| 100 |
const menuRef = (innerRef as React.RefObject<HTMLDivElement | null>) || localMenuRef; |
| 101 |
const toggleRef = |
| 102 |
typeof toggle === 'function' || (typeof toggle !== 'function' && !toggle.toggleRef) |
| 103 |
? localToggleRef |
| 104 |
: (toggle?.toggleRef as React.RefObject<HTMLButtonElement | null>); |
| 105 |
|
| 106 |
const prevIsOpen = useRef<boolean>(isOpen); |
| 107 |
useEffect(() => { |
| 108 |
// menu was opened, focus on first menu item |
| 109 |
if (prevIsOpen.current === false && isOpen === true && shouldFocusFirstItemOnOpen) { |
| 110 |
setTimeout(() => { |
| 111 |
const firstElement = menuRef?.current?.querySelector('li button:not(:disabled),li input:not(:disabled)'); |
| 112 |
firstElement && (firstElement as HTMLElement).focus({ preventScroll: shouldPreventScrollOnItemFocus }); |
| 113 |
}, focusTimeoutDelay); |
| 114 |
} |
| 115 |
|
| 116 |
prevIsOpen.current = isOpen; |
| 117 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 118 |
}, [isOpen]); |
| 119 |
|
| 120 |
useEffect(() => { |
| 121 |
const handleMenuKeys = (event: KeyboardEvent) => { |
| 122 |
// Close the menu on tab or escape if onOpenChange is provided |
| 123 |
if ( |
| 124 |
isOpen && |
| 125 |
onOpenChange && |
| 126 |
(menuRef.current?.contains(event.target as Node) || toggleRef.current?.contains(event.target as Node)) |
| 127 |
) { |
| 128 |
if (onOpenChangeKeys.includes(event.key)) { |
| 129 |
event.preventDefault(); |
| 130 |
onOpenChange(false); |
| 131 |
toggleRef.current?.focus(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (toggleRef.current?.contains(event.target as Node)) { |
| 136 |
if (onToggleKeydown) { |
| 137 |
onToggleKeydown(event); |
| 138 |
} else if (isOpen && variant !== 'typeahead') { |
| 139 |
onToggleArrowKeydownDefault(event, menuRef); |
| 140 |
} |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
const handleClick = (event: MouseEvent) => { |
| 145 |
// If the event is not on the toggle and onOpenChange callback is provided, close the menu |
| 146 |
if (isOpen && onOpenChange && !toggleRef?.current?.contains(event.target as Node)) { |
| 147 |
if (isOpen && !menuRef.current?.contains(event.target as Node)) { |
| 148 |
onOpenChange(false); |
| 149 |
} |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
window.addEventListener('keydown', handleMenuKeys); |
| 154 |
window.addEventListener('click', handleClick); |
| 155 |
|
| 156 |
return () => { |
| 157 |
window.removeEventListener('keydown', handleMenuKeys); |
| 158 |
window.removeEventListener('click', handleClick); |
| 159 |
}; |
| 160 |
}, [ |
| 161 |
isOpen, |
| 162 |
menuRef, |
| 163 |
toggleRef, |
| 164 |
onOpenChange, |
| 165 |
onOpenChangeKeys, |
| 166 |
onToggleKeydown, |
| 167 |
shouldPreventScrollOnItemFocus, |
| 168 |
shouldFocusFirstItemOnOpen, |
| 169 |
focusTimeoutDelay |
| 170 |
]); |
| 171 |
|
| 172 |
const menu = ( |
| 173 |
<Menu |
| 174 |
role={role} |
| 175 |
className={css(className)} |
| 176 |
ref={menuRef} |
| 177 |
onSelect={(event, value) => { |
| 178 |
onSelect && onSelect(event, value); |
| 179 |
shouldFocusToggleOnSelect && toggleRef.current.focus(); |
| 180 |
}} |
| 181 |
isPlain={isPlain} |
| 182 |
selected={selected} |
| 183 |
isScrollable={isScrollable ?? (menuHeight !== undefined || maxMenuHeight !== undefined)} |
| 184 |
{...getOUIAProps(Select.displayName, generatedOuiaId, props.ouiaSafe !== undefined ? props.ouiaSafe : true)} |
| 185 |
{...props} |
| 186 |
> |
| 187 |
<MenuContent menuHeight={menuHeight} maxMenuHeight={maxMenuHeight}> |
| 188 |
{children} |
| 189 |
</MenuContent> |
| 190 |
</Menu> |
| 191 |
); |
| 192 |
return ( |
| 193 |
<Popper |
| 194 |
trigger={typeof toggle === 'function' ? toggle(toggleRef) : toggle.toggleNode} |
| 195 |
triggerRef={toggleRef} |
| 196 |
popper={menu} |
| 197 |
popperRef={menuRef} |
| 198 |
isVisible={isOpen} |
| 199 |
zIndex={zIndex} |
| 200 |
{...popperProps} |
| 201 |
/> |
| 202 |
); |
| 203 |
}; |
| 204 |
|
| 205 |
export const Select = forwardRef((props: SelectProps, ref: React.Ref<any>) => <SelectBase innerRef={ref} {...props} />); |
| 206 |
|
| 207 |
Select.displayName = 'Select'; |