| 1 |
import { useEffect, useRef, useState } from 'react'; |
| 2 |
import { Select, SelectOption, SelectList, MenuToggle, Spinner } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const SelectViewMore: React.FunctionComponent = () => { |
| 5 |
const [isOpen, setIsOpen] = useState(false); |
| 6 |
const [selected, setSelected] = useState<string>('Select a value'); |
| 7 |
const [activeItem, setActiveItem] = useState<number | string>(0); |
| 8 |
const [isLoading, setIsLoading] = useState(false); |
| 9 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 10 |
const [selectOptions, setSelectOptions] = useState([ |
| 11 |
<SelectOption key={0} value="Option 1"> |
| 12 |
Option 1 |
| 13 |
</SelectOption>, |
| 14 |
<SelectOption key={1} value="Option 2"> |
| 15 |
Option 2 |
| 16 |
</SelectOption>, |
| 17 |
<SelectOption key={2} value="Option 3"> |
| 18 |
Option 3 |
| 19 |
</SelectOption>, |
| 20 |
<SelectOption key={3} value="Option 4"> |
| 21 |
Option 4 |
| 22 |
</SelectOption>, |
| 23 |
<SelectOption key={4} value="Option 5"> |
| 24 |
Option 5 |
| 25 |
</SelectOption>, |
| 26 |
<SelectOption key={5} value="Option 6"> |
| 27 |
Option 6 |
| 28 |
</SelectOption>, |
| 29 |
<SelectOption key={6} value="Option 7"> |
| 30 |
Option 7 |
| 31 |
</SelectOption>, |
| 32 |
<SelectOption key={7} value="Option 8"> |
| 33 |
Option 8 |
| 34 |
</SelectOption>, |
| 35 |
<SelectOption key={8} value="Option 9"> |
| 36 |
Option 9 |
| 37 |
</SelectOption>, |
| 38 |
<SelectOption key={9} value="Final Option 10"> |
| 39 |
Final Option 10 |
| 40 |
</SelectOption> |
| 41 |
]); |
| 42 |
const [numOptions, setNumOptions] = useState(3); |
| 43 |
const [visibleOptions, setVisibleOptions] = useState(selectOptions.slice(0, numOptions)); |
| 44 |
const activeItemRef = useRef<HTMLElement>(null); |
| 45 |
const viewMoreRef = useRef<HTMLLIElement>(null); |
| 46 |
const toggleRef = useRef<HTMLButtonElement>(null); |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
activeItemRef.current?.focus(); |
| 50 |
}, [visibleOptions]); |
| 51 |
|
| 52 |
const simulateNetworkCall = (networkCallback: () => void) => { |
| 53 |
setTimeout(networkCallback, 2000); |
| 54 |
}; |
| 55 |
|
| 56 |
const getNextValidItem = (startingIndex: number, maxLength: number) => { |
| 57 |
let validItem; |
| 58 |
for (let i = startingIndex; i < maxLength; i++) { |
| 59 |
if (selectOptions[i].props.isDisabled) { |
| 60 |
continue; |
| 61 |
} else { |
| 62 |
validItem = selectOptions[i]; |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
return validItem; |
| 67 |
}; |
| 68 |
|
| 69 |
const loadMoreOptions = () => { |
| 70 |
const newLength = numOptions + 3 <= selectOptions.length ? numOptions + 3 : selectOptions.length; |
| 71 |
const prevPosition = numOptions; |
| 72 |
const nextValidItem = getNextValidItem(prevPosition, newLength); |
| 73 |
|
| 74 |
setNumOptions(newLength); |
| 75 |
setIsLoading(false); |
| 76 |
setActiveItem(nextValidItem.props.value); |
| 77 |
setVisibleOptions(selectOptions.slice(0, newLength)); |
| 78 |
}; |
| 79 |
|
| 80 |
const onViewMoreClick = () => { |
| 81 |
setIsLoading(true); |
| 82 |
simulateNetworkCall(() => { |
| 83 |
loadMoreOptions(); |
| 84 |
}); |
| 85 |
}; |
| 86 |
|
| 87 |
const onToggleClick = () => { |
| 88 |
setIsOpen(!isOpen); |
| 89 |
}; |
| 90 |
|
| 91 |
const onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => { |
| 92 |
// eslint-disable-next-line no-console |
| 93 |
console.log('selected', value); |
| 94 |
|
| 95 |
if (value !== 'loader') { |
| 96 |
setSelected(value as string); |
| 97 |
setIsOpen(false); |
| 98 |
toggleRef?.current?.focus(); // Only focus the toggle when a non-loader option is selected |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
const toggle = ( |
| 103 |
<MenuToggle |
| 104 |
ref={toggleRef} |
| 105 |
onClick={onToggleClick} |
| 106 |
isExpanded={isOpen} |
| 107 |
style={ |
| 108 |
{ |
| 109 |
width: '200px' |
| 110 |
} as React.CSSProperties |
| 111 |
} |
| 112 |
> |
| 113 |
{selected} |
| 114 |
</MenuToggle> |
| 115 |
); |
| 116 |
|
| 117 |
return ( |
| 118 |
<Select |
| 119 |
id="single-view-more-select" |
| 120 |
isOpen={isOpen} |
| 121 |
selected={selected} |
| 122 |
onSelect={onSelect} |
| 123 |
onOpenChange={(isOpen) => setIsOpen(isOpen)} |
| 124 |
toggle={{ toggleNode: toggle, toggleRef }} |
| 125 |
> |
| 126 |
<SelectList> |
| 127 |
{visibleOptions.map((option) => { |
| 128 |
const props = option.props; |
| 129 |
|
| 130 |
return <SelectOption key={option.key} ref={props.value === activeItem ? activeItemRef : null} {...props} />; |
| 131 |
})} |
| 132 |
{numOptions !== selectOptions.length && ( |
| 133 |
<SelectOption |
| 134 |
{...(!isLoading && { isLoadButton: true })} |
| 135 |
{...(isLoading && { isLoading: true })} |
| 136 |
onClick={onViewMoreClick} |
| 137 |
value="loader" |
| 138 |
ref={viewMoreRef} |
| 139 |
> |
| 140 |
{isLoading ? <Spinner size="lg" /> : 'View more'} |
| 141 |
</SelectOption> |
| 142 |
)} |
| 143 |
</SelectList> |
| 144 |
</Select> |
| 145 |
); |
| 146 |
}; |