Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/react-compiler-autocomplete-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Autocomplete: Improve rendering performance with React Compiler support.
1 change: 0 additions & 1 deletion packages/react/script/react-compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const files = glob
const unsupportedPatterns = [
'src/ActionMenu/ActionMenu.test.tsx',
'src/AnchoredOverlay/AnchoredOverlay.tsx',
'src/Autocomplete/AutocompleteInput.tsx',
'src/Dialog/Dialog.tsx',
'src/FilteredActionList/FilteredActionList.tsx',
'src/LabelGroup/LabelGroup.tsx',
Expand Down
13 changes: 8 additions & 5 deletions packages/react/src/Autocomplete/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TextInput from '../TextInput'
import {useMergedRefs} from '../hooks/useMergedRefs'
import type {ComponentProps} from '../utils/types'
import useSafeTimeout from '../hooks/useSafeTimeout'
import {useEffectCallback} from '../internal/hooks/useEffectCallback'

type InternalAutocompleteInputProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -48,6 +49,11 @@ const AutocompleteInput = React.forwardRef(
const [highlightRemainingText, setHighlightRemainingText] = useState<boolean>(true)
const {safeSetTimeout} = useSafeTimeout()

// Read the latest highlighting state without reapplying a suggestion on Backspace keyup.
const shouldHighlightRemainingText = useEffectCallback(() => {
return highlightRemainingText
})

const handleInputFocus: FocusEventHandler<HTMLInputElement> = event => {
onFocus?.(event)
if (openOnFocus) {
Expand Down Expand Up @@ -158,8 +164,7 @@ const AutocompleteInput = React.forwardRef(

if (
isInputFocused &&
// eslint-disable-next-line react-you-might-not-need-an-effect/no-event-handler
highlightRemainingText &&
shouldHighlightRemainingText() &&
autocompleteSuggestion &&
(inputValue || isMenuDirectlyActivated)
) {
Expand All @@ -169,9 +174,7 @@ const AutocompleteInput = React.forwardRef(
inputRef.current.setSelectionRange(inputValue.length, autocompleteSuggestion.length)
}
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [autocompleteSuggestion, inputValue, inputRef, isMenuDirectlyActivated])
}, [autocompleteSuggestion, inputValue, inputRef, isMenuDirectlyActivated, shouldHighlightRemainingText])

useEffect(() => {
setInputValue(typeof value !== 'undefined' ? value.toString() : '')
Expand Down
Loading