MCPcopy
hub / github.com/primer/react / Autocomplete

Function Autocomplete

packages/react/src/Autocomplete/Autocomplete.tsx:48–93  ·  view source on GitHub ↗
({children, id: idProp})

Source from the content-addressed store, hash-verified

46}
47
48const Autocomplete: React.FC<React.PropsWithChildren<{id?: string}>> = ({children, id: idProp}) => {
49 const activeDescendantRef = useRef<HTMLElement>(null)
50 const scrollContainerRef = useRef<HTMLDivElement>(null)
51 const inputRef = useRef<HTMLInputElement>(null)
52 const [state, dispatch] = useReducer(reducer, initialState)
53 const {inputValue, showMenu, autocompleteSuggestion, isMenuDirectlyActivated, selectedItemLength} = state
54 const setInputValue = useCallback((value: State['inputValue']) => {
55 dispatch({type: 'inputValue', payload: value})
56 }, [])
57 const setShowMenu = useCallback((value: State['showMenu']) => {
58 dispatch({type: 'showMenu', payload: value})
59 }, [])
60 const setAutocompleteSuggestion = useCallback((value: State['autocompleteSuggestion']) => {
61 dispatch({type: 'autocompleteSuggestion', payload: value})
62 }, [])
63 const setIsMenuDirectlyActivated = useCallback((value: State['isMenuDirectlyActivated']) => {
64 dispatch({type: 'isMenuDirectlyActivated', payload: value})
65 }, [])
66 const setSelectedItemLength = useCallback((value: State['selectedItemLength']) => {
67 dispatch({type: 'selectedItemLength', payload: value})
68 }, [])
69 const id = useId(idProp)
70
71 return (
72 <AutocompleteContext.Provider
73 value={{
74 activeDescendantRef,
75 autocompleteSuggestion,
76 id,
77 inputRef,
78 inputValue,
79 isMenuDirectlyActivated,
80 scrollContainerRef,
81 selectedItemLength,
82 setAutocompleteSuggestion,
83 setInputValue,
84 setIsMenuDirectlyActivated,
85 setShowMenu,
86 setSelectedItemLength,
87 showMenu,
88 }}
89 >
90 {children}
91 </AutocompleteContext.Provider>
92 )
93}
94
95export type AutocompleteProps = ComponentProps<typeof Autocomplete>
96export type {AutocompleteInputProps} from './AutocompleteInput'

Callers

nothing calls this directly

Calls 1

useIdFunction · 0.90

Tested by

no test coverage detected