@primer/components
Version:
Primer react components
109 lines (106 loc) • 4.46 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useCallback, useEffect, useRef } from 'react';
import { useSSRSafeId } from '@react-aria/ssr';
import TextInput from '../TextInput';
import Box from '../Box';
import { ActionList } from '../ActionList';
import Spinner from '../Spinner';
import { useFocusZone } from '../hooks/useFocusZone';
import { useProvidedStateOrCreate } from '../hooks/useProvidedStateOrCreate';
import styled from 'styled-components';
import { get } from '../constants';
import { useProvidedRefOrCreate } from '../hooks/useProvidedRefOrCreate';
import useScrollFlash from '../hooks/useScrollFlash';
import { scrollIntoViewingArea } from '../behaviors/scrollIntoViewingArea';
const StyledHeader = styled.div.withConfig({
displayName: "FilteredActionList__StyledHeader",
componentId: "sc-yg3jkv-0"
})(["box-shadow:0 1px 0 ", ";z-index:1;"], get('colors.border.default'));
export function FilteredActionList({
loading = false,
placeholderText,
filterValue: externalFilterValue,
onFilterChange,
items,
textInputProps,
inputRef: providedInputRef,
sx,
...listProps
}) {
const [filterValue, setInternalFilterValue] = useProvidedStateOrCreate(externalFilterValue, undefined, '');
const onInputChange = useCallback(e => {
const value = e.target.value;
onFilterChange(value, e);
setInternalFilterValue(value);
}, [onFilterChange, setInternalFilterValue]);
const scrollContainerRef = useRef(null);
const listContainerRef = useRef(null);
const inputRef = useProvidedRefOrCreate(providedInputRef);
const activeDescendantRef = useRef();
const listId = useSSRSafeId();
const onInputKeyPress = useCallback(event => {
if (event.key === 'Enter' && activeDescendantRef.current) {
event.preventDefault();
event.nativeEvent.stopImmediatePropagation(); // Forward Enter key press to active descendant so that item gets activated
const activeDescendantEvent = new KeyboardEvent(event.type, event.nativeEvent);
activeDescendantRef.current.dispatchEvent(activeDescendantEvent);
}
}, [activeDescendantRef]);
useFocusZone({
containerRef: listContainerRef,
focusOutBehavior: 'wrap',
focusableElementFilter: element => {
return !(element instanceof HTMLInputElement);
},
activeDescendantFocus: inputRef,
onActiveDescendantChanged: (current, previous, directlyActivated) => {
activeDescendantRef.current = current;
if (current && scrollContainerRef.current && directlyActivated) {
scrollIntoViewingArea(current, scrollContainerRef.current);
}
}
}, [// List ref isn't set while loading. Need to re-bind focus zone when it changes
loading]);
useEffect(() => {
// if items changed, we want to instantly move active descendant into view
if (activeDescendantRef.current && scrollContainerRef.current) {
scrollIntoViewingArea(activeDescendantRef.current, scrollContainerRef.current, 'vertical', undefined, undefined, 'auto');
}
}, [items]);
useScrollFlash(scrollContainerRef);
return /*#__PURE__*/React.createElement(Box, {
display: "flex",
flexDirection: "column",
overflow: "hidden",
sx: sx
}, /*#__PURE__*/React.createElement(StyledHeader, null, /*#__PURE__*/React.createElement(TextInput, _extends({
ref: inputRef,
block: true,
width: "auto",
color: "fg.default",
value: filterValue,
onChange: onInputChange,
onKeyPress: onInputKeyPress,
placeholder: placeholderText,
"aria-label": placeholderText,
"aria-controls": listId
}, textInputProps))), /*#__PURE__*/React.createElement(Box, {
ref: scrollContainerRef,
overflow: "auto"
}, loading ? /*#__PURE__*/React.createElement(Box, {
width: "100%",
display: "flex",
flexDirection: "row",
justifyContent: "center",
pt: 6,
pb: 7
}, /*#__PURE__*/React.createElement(Spinner, null)) : /*#__PURE__*/React.createElement(ActionList, _extends({
ref: listContainerRef,
items: items
}, listProps, {
role: "listbox",
id: listId
}))));
}
FilteredActionList.displayName = "FilteredActionList";
FilteredActionList.displayName = 'FilteredActionList';