|
| 1 | +/* |
| 2 | + Copied as is from: https://github.com/jacobworrel/react-windowed-select/blob/master/src/MenuList.tsx |
| 3 | + MIT License |
| 4 | + Copyright (c) 2019 Jacob Worrel |
| 5 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + of this software and associated documentation files (the "Software"), to deal |
| 7 | + in the Software without restriction, including without limitation the rights |
| 8 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + copies of the Software, and to permit persons to whom the Software is |
| 10 | + furnished to do so, subject to the following conditions: |
| 11 | + The above copyright notice and this permission notice shall be included in all |
| 12 | + copies or substantial portions of the Software. |
| 13 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | + SOFTWARE. |
| 20 | +*/ |
| 21 | + |
| 22 | +import * as React from "react"; |
| 23 | +import { ListChildComponentProps, VariableSizeList as List } from "react-window"; |
| 24 | +import { OptionProps, GroupBase } from "react-select"; |
| 25 | +import { createGetHeight, flattenGroupedChildren, getCurrentIndex } from "./lib"; |
| 26 | + |
| 27 | +interface Style extends React.CSSProperties { |
| 28 | + top: number; |
| 29 | +} |
| 30 | + |
| 31 | +interface ListChildProps extends ListChildComponentProps { |
| 32 | + style: Style; |
| 33 | +} |
| 34 | + |
| 35 | +interface OptionTypeBase { |
| 36 | + [key: string]: any; |
| 37 | +} |
| 38 | + |
| 39 | +function MenuList(props) { |
| 40 | + const children = React.useMemo(() => { |
| 41 | + const children = React.Children.toArray(props.children); |
| 42 | + |
| 43 | + const head = children[0] || {}; |
| 44 | + |
| 45 | + if (React.isValidElement<OptionProps<OptionTypeBase, boolean, GroupBase<OptionTypeBase>>>(head)) { |
| 46 | + const { props: { data: { options = [] } = {} } = {} } = head; |
| 47 | + const groupedChildrenLength = options.length; |
| 48 | + const isGrouped = groupedChildrenLength > 0; |
| 49 | + const flattenedChildren = isGrouped && flattenGroupedChildren(children); |
| 50 | + |
| 51 | + return isGrouped ? flattenedChildren : children; |
| 52 | + } else { |
| 53 | + return []; |
| 54 | + } |
| 55 | + }, [props.children]); |
| 56 | + |
| 57 | + const { getStyles } = props; |
| 58 | + const groupHeadingStyles = getStyles("groupHeading", props); |
| 59 | + const loadingMsgStyles = getStyles("loadingMessage", props); |
| 60 | + const noOptionsMsgStyles = getStyles("noOptionsMessage", props); |
| 61 | + const optionStyles = getStyles("option", props); |
| 62 | + const getHeight = createGetHeight({ |
| 63 | + groupHeadingStyles, |
| 64 | + noOptionsMsgStyles, |
| 65 | + optionStyles, |
| 66 | + loadingMsgStyles, |
| 67 | + }); |
| 68 | + |
| 69 | + const heights = React.useMemo(() => children.map(getHeight), [children]); |
| 70 | + const currentIndex = React.useMemo(() => getCurrentIndex(children), [children]); |
| 71 | + |
| 72 | + const itemCount = children.length; |
| 73 | + |
| 74 | + const [measuredHeights, setMeasuredHeights] = React.useState({}); |
| 75 | + |
| 76 | + // calc menu height |
| 77 | + const { maxHeight, paddingBottom = 0, paddingTop = 0, ...menuListStyle } = getStyles("menuList", props); |
| 78 | + const totalHeight = React.useMemo(() => { |
| 79 | + return heights.reduce((sum, height, idx) => { |
| 80 | + if (measuredHeights[idx]) { |
| 81 | + return sum + measuredHeights[idx]; |
| 82 | + } else { |
| 83 | + return sum + height; |
| 84 | + } |
| 85 | + }, 0); |
| 86 | + }, [heights, measuredHeights]); |
| 87 | + const totalMenuHeight = totalHeight + paddingBottom + paddingTop; |
| 88 | + const menuHeight = Math.min(maxHeight, totalMenuHeight); |
| 89 | + const estimatedItemSize = Math.floor(totalHeight / itemCount); |
| 90 | + |
| 91 | + const { innerRef, selectProps } = props; |
| 92 | + |
| 93 | + const { classNamePrefix, isMulti } = selectProps || {}; |
| 94 | + const list = React.useRef<List>(null); |
| 95 | + |
| 96 | + React.useEffect(() => { |
| 97 | + setMeasuredHeights({}); |
| 98 | + }, [props.children]); |
| 99 | + |
| 100 | + // method to pass to inner item to set this items outer height |
| 101 | + const setMeasuredHeight = ({ index, measuredHeight }) => { |
| 102 | + if (measuredHeights[index] !== undefined && measuredHeights[index] === measuredHeight) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + setMeasuredHeights((measuredHeights) => ({ |
| 107 | + ...measuredHeights, |
| 108 | + [index]: measuredHeight, |
| 109 | + })); |
| 110 | + |
| 111 | + // this forces the list to rerender items after the item positions resizing |
| 112 | + if (list.current) { |
| 113 | + list.current.resetAfterIndex(index); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + React.useEffect(() => { |
| 118 | + /** |
| 119 | + * enables scrolling on key down arrow |
| 120 | + */ |
| 121 | + if (currentIndex >= 0 && list.current !== null) { |
| 122 | + list.current.scrollToItem(currentIndex); |
| 123 | + } |
| 124 | + }, [currentIndex, children, list]); |
| 125 | + |
| 126 | + return ( |
| 127 | + <List |
| 128 | + className={ |
| 129 | + classNamePrefix |
| 130 | + ? `${classNamePrefix}__menu-list${isMulti ? ` ${classNamePrefix}__menu-list--is-multi` : ""}` |
| 131 | + : "" |
| 132 | + } |
| 133 | + style={menuListStyle} |
| 134 | + ref={list} |
| 135 | + outerRef={innerRef} |
| 136 | + estimatedItemSize={estimatedItemSize} |
| 137 | + innerElementType={React.forwardRef(({ style, ...rest }, ref) => ( |
| 138 | + <div |
| 139 | + ref={ref} |
| 140 | + style={{ |
| 141 | + ...style, |
| 142 | + height: `${parseFloat(style.height) + paddingBottom + paddingTop}px`, |
| 143 | + }} |
| 144 | + {...rest} |
| 145 | + /> |
| 146 | + ))} |
| 147 | + height={menuHeight} |
| 148 | + width="100%" |
| 149 | + itemCount={itemCount} |
| 150 | + itemData={children} |
| 151 | + itemSize={(index) => measuredHeights[index] || heights[index]} |
| 152 | + > |
| 153 | + {/*@ts-ignore*/} |
| 154 | + {({ data, index, style }: ListChildProps) => { |
| 155 | + return ( |
| 156 | + <div |
| 157 | + style={{ |
| 158 | + ...style, |
| 159 | + top: `${parseFloat(style.top.toString()) + paddingTop}px`, |
| 160 | + }} |
| 161 | + > |
| 162 | + <MenuItem data={data[index]} index={index} setMeasuredHeight={setMeasuredHeight} /> |
| 163 | + </div> |
| 164 | + ); |
| 165 | + }} |
| 166 | + </List> |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function MenuItem({ data, index, setMeasuredHeight }) { |
| 171 | + const ref = React.useRef<HTMLDivElement>(null); |
| 172 | + |
| 173 | + // using useLayoutEffect prevents bounciness of options of re-renders |
| 174 | + React.useLayoutEffect(() => { |
| 175 | + if (ref.current) { |
| 176 | + const measuredHeight = ref.current.getBoundingClientRect().height; |
| 177 | + |
| 178 | + setMeasuredHeight({ index, measuredHeight }); |
| 179 | + } |
| 180 | + }, [ref.current]); |
| 181 | + |
| 182 | + return ( |
| 183 | + <div key={`option-${index}`} ref={ref}> |
| 184 | + {data} |
| 185 | + </div> |
| 186 | + ); |
| 187 | +} |
| 188 | +export default MenuList; |
0 commit comments