Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | 14x 14x 14x 14x 14x 14x 249x 14x 319x 14x 600x 14x 88x 559x 559x 505x 54x 54x 2x 52x 14x 510x 14x 264x 14x 14x 88x 14x 176x 176x 14x 88x 88x 88x 88x 88x 88x 88x 845x 845x 502x 343x 88x 88x 88x 88x 88x 845x 52x 793x 793x 845x 88x 88x 88x 88x 733x 88x 845x 845x 88x 430x 88x 88x 88x 88x 88x 88x 80x 27x 53x 53x 1x 52x 22x 22x 52x 88x 1x 1x 1x 88x 88x 88x 14x 63x 272x 272x 14x 14x 63x 63x 63x 15x 5x 15x 15x 63x 7x 7x 63x 63x 63x 43x 43x 227x 63x 54x 63x 63x 14x 49x 49x 49x | import React, {
Children,
cloneElement,
CSSProperties,
isValidElement,
memo,
ReactElement,
ReactNode,
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import ReactSelect, {
components as reactSelectComponents,
GroupBase,
InputActionMeta,
MenuListProps,
OptionProps,
Props as ReactSelectProps,
SelectComponentsConfig,
SelectInstance,
useStateManager,
} from "react-select";
import { CreatableProps, useCreatable } from "react-select/creatable";
import { List, RowComponentProps, useListCallbackRef } from "react-window";
import styled from "styled-components";
interface MenuChildProps {
children?: ReactNode;
data?: {
label?: string;
options?: unknown[];
};
isDisabled?: boolean;
isFocused?: boolean;
isSelected?: boolean;
type?: "group" | "option";
}
interface InternalSelectProps {
windowedShouldScrollToFocusedOption?: React.MutableRefObject<boolean>;
windowedRenderToken?: object;
}
type MenuChild = ReactElement<MenuChildProps>;
interface MenuRowProps {
readonly heights: number[];
readonly items: MenuChild[];
}
const MenuRowComponent = ({
index,
style,
items,
}: RowComponentProps<MenuRowProps>) => <div style={style}>{items[index]}</div>;
const MenuRow = memo(
MenuRowComponent,
(previous, next) =>
previous.index === next.index &&
previous.items[previous.index] === next.items[next.index],
) as typeof MenuRowComponent;
const getRowHeight = (index: number, props: MenuRowProps) =>
props.heights[index] ?? 35;
const flattenMenuChildren = (children: ReactNode): MenuChild[] =>
Children.toArray(children)
.filter(isValidElement)
.flatMap((child) => {
const item = child as MenuChild;
if (!Array.isArray(item.props.data?.options)) {
return [item];
}
const options = Children.toArray(item.props.children).filter(
isValidElement,
) as MenuChild[];
if (item.props.data?.label === "") {
return options;
}
return [cloneElement(item, { type: "group" }, []), ...options];
});
const canReuseMenuChild = (previous: MenuChild, next: MenuChild) =>
previous.key === next.key &&
previous.type === next.type &&
previous.props.data === next.props.data &&
previous.props.type === next.props.type &&
previous.props.isDisabled === next.props.isDisabled &&
previous.props.isFocused === next.props.isFocused &&
previous.props.isSelected === next.props.isSelected;
const numericHeight = (height: unknown, fallback: number) =>
typeof height === "number" ? height : fallback;
const FORMATTED_OPTION_WIDTH_ALLOWANCE = 52;
const MenuWidthSizer = styled.div<{ $formatted: boolean }>`
box-sizing: border-box;
width: max-content;
height: 0;
padding: 0
${(props) =>
10 + (props.$formatted ? FORMATTED_OPTION_WIDTH_ALLOWANCE : 0)}px
0 10px;
visibility: hidden;
pointer-events: none;
white-space: nowrap;
`;
const setRef = (
ref: React.Ref<HTMLDivElement>,
value: HTMLDivElement | null,
) => {
if (typeof ref === "function") {
ref(value);
} else Eif (ref) {
ref.current = value;
}
};
const SelectWindowedMenuList = <
Option,
IsMulti extends boolean,
Group extends GroupBase<Option>,
>(
props: MenuListProps<Option, IsMulti, Group>,
) => {
const { children, getStyles, innerProps, innerRef, selectProps } = props;
const previousRender = useRef<{
items: MenuChild[];
renderToken?: object;
}>(undefined);
const renderToken = (selectProps as InternalSelectProps).windowedRenderToken;
const shouldScrollToFocusedOption = (selectProps as InternalSelectProps)
.windowedShouldScrollToFocusedOption;
const nextItems = flattenMenuChildren(children);
const isInternalSelectUpdate =
previousRender.current?.renderToken === renderToken;
const items = nextItems.map((item, index) => {
const previousItem = previousRender.current?.items[index];
if (
isInternalSelectUpdate &&
previousItem &&
canReuseMenuChild(previousItem, item)
) {
return previousItem;
}
return item;
});
useLayoutEffect(() => {
previousRender.current = { items, renderToken };
}, [items, renderToken]);
const optionHeight = numericHeight(
getStyles("option", props as never).height,
35,
);
const groupHeight = numericHeight(
getStyles("groupHeading", props as never).height,
25,
);
const heights = items.map((item) => {
if (item.props.type === "group") {
return groupHeight;
}
Eif (item.props.type === "option") {
return optionHeight;
}
return 35;
});
const totalHeight = heights.reduce((total, height) => total + height, 0);
const menuListStyles = getStyles("menuList", props);
const maxHeight = numericHeight(menuListStyles.maxHeight, props.maxHeight);
const height = Math.min(maxHeight, totalHeight);
const currentIndex = Math.max(
items.findIndex((item) => item.props.isFocused),
0,
);
const longestLabel = items.reduce((longest, item) => {
const label = item.props.data?.label ?? "";
return label.length > longest.length ? label : longest;
}, "");
const hasFormattedLabels = items.some(
(item) =>
item.props.type === "option" &&
typeof item.props.children !== "string" &&
typeof item.props.children !== "number",
);
const hasScrolledToInitialItem = useRef(false);
const [listApi, setListApi] = useListCallbackRef(null);
useEffect(() => {
setRef(innerRef, listApi?.element ?? null);
return () => setRef(innerRef, null);
}, [innerRef, listApi]);
useEffect(() => {
if (!listApi || items.length === 0) {
return;
}
const isInitialScroll = !hasScrolledToInitialItem.current;
if (!isInitialScroll && shouldScrollToFocusedOption?.current === false) {
return;
}
const animationFrame = requestAnimationFrame(() => {
listApi.scrollToRow({
align: isInitialScroll ? "center" : "auto",
index: currentIndex,
});
hasScrolledToInitialItem.current = true;
});
return () => cancelAnimationFrame(animationFrame);
}, [currentIndex, items.length, listApi, shouldScrollToFocusedOption]);
const handleMouseMoveCapture = useCallback<
React.MouseEventHandler<HTMLDivElement>
>(
(event) => {
Eif (shouldScrollToFocusedOption) {
shouldScrollToFocusedOption.current = false;
}
innerProps.onMouseMoveCapture?.(event);
},
[innerProps, shouldScrollToFocusedOption],
);
const { maxHeight: _maxHeight, ...style } = menuListStyles;
const classNamePrefix = selectProps.classNamePrefix;
return (
<>
{longestLabel && (
<MenuWidthSizer aria-hidden inert $formatted={hasFormattedLabels}>
{longestLabel}
</MenuWidthSizer>
)}
<List
{...innerProps}
onMouseMoveCapture={handleMouseMoveCapture}
className={
classNamePrefix
? `${classNamePrefix}__menu-list${
selectProps.isMulti
? ` ${classNamePrefix}__menu-list--is-multi`
: ""
}`
: undefined
}
defaultHeight={height}
listRef={setListApi}
rowComponent={MenuRow}
rowCount={items.length}
rowHeight={getRowHeight}
rowProps={{ heights, items }}
style={{
...(style as CSSProperties),
height,
width: "100%",
}}
/>
</>
);
};
const countOptions = <Option, Group extends GroupBase<Option>>(
options: readonly (Option | Group)[],
) =>
options.reduce((total, option) => {
const groupOptions = (option as Group).options;
return total + (Array.isArray(groupOptions) ? groupOptions.length : 1);
}, 0);
const ignoreOptionRef = () => {};
export interface SelectWindowedProps<
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
> extends ReactSelectProps<Option, IsMulti, Group> {
windowThreshold?: number;
}
export const SelectWindowed = React.forwardRef(
<
Option,
IsMulti extends boolean,
Group extends GroupBase<Option> = GroupBase<Option>,
>(
{
components,
onKeyDown,
onInputChange,
options = [],
windowThreshold = 100,
...props
}: SelectWindowedProps<Option, IsMulti, Group>,
ref: React.ForwardedRef<SelectInstance<Option, IsMulti, Group>>,
) => {
// Make uncontrolled search changes visible to the menu-list memoization.
const [, setInputRevision] = useState(0);
const shouldScrollToFocusedOption = useRef(true);
const handleInputChange = useCallback(
(value: string, action: InputActionMeta) => {
if (action.action === "input-change") {
shouldScrollToFocusedOption.current = true;
}
setInputRevision((revision) => revision + 1);
return onInputChange?.(value, action);
},
[onInputChange],
);
const handleKeyDown = useCallback<
React.KeyboardEventHandler<HTMLDivElement>
>(
(event) => {
shouldScrollToFocusedOption.current = true;
onKeyDown?.(event);
},
[onKeyDown],
);
const isWindowed = countOptions(options) >= windowThreshold;
const windowedRenderToken = {};
const WindowedOption = useMemo(() => {
const OptionComponent =
components?.Option ?? reactSelectComponents.Option;
return (optionProps: OptionProps<Option, IsMulti, Group>) => (
<OptionComponent {...optionProps} innerRef={ignoreOptionRef} />
);
}, [components?.Option]);
const selectComponents = useMemo<
SelectComponentsConfig<Option, IsMulti, Group>
>(
() => ({
...components,
...(isWindowed && {
MenuList: SelectWindowedMenuList,
Option: WindowedOption,
}),
}),
[components, isWindowed, WindowedOption],
);
const reactSelectProps = {
...props,
windowedShouldScrollToFocusedOption: shouldScrollToFocusedOption,
windowedRenderToken,
} as ReactSelectProps<Option, IsMulti, Group> & InternalSelectProps;
return (
<ReactSelect
{...reactSelectProps}
ref={ref}
components={selectComponents}
onKeyDown={handleKeyDown}
onInputChange={handleInputChange}
options={options}
/>
);
},
) as <
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
>(
props: SelectWindowedProps<Option, IsMulti, Group> & {
ref?: React.ForwardedRef<SelectInstance<Option, IsMulti, Group>>;
},
) => ReactElement;
export type CreatableSelectWindowedProps<
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
> = CreatableProps<Option, IsMulti, Group> & {
windowThreshold?: number;
};
export const CreatableSelectWindowed = React.forwardRef(
<
Option,
IsMulti extends boolean,
Group extends GroupBase<Option> = GroupBase<Option>,
>(
props: CreatableSelectWindowedProps<Option, IsMulti, Group>,
ref: React.ForwardedRef<SelectInstance<Option, IsMulti, Group>>,
) => {
const stateManagedProps = useStateManager(props);
const creatableProps = useCreatable(stateManagedProps);
return <SelectWindowed {...creatableProps} ref={ref} />;
},
) as <
Option = unknown,
IsMulti extends boolean = false,
Group extends GroupBase<Option> = GroupBase<Option>,
>(
props: CreatableSelectWindowedProps<Option, IsMulti, Group> & {
ref?: React.ForwardedRef<SelectInstance<Option, IsMulti, Group>>;
},
) => ReactElement;
|