@@ -4,7 +4,7 @@ import { useListItem } from '@floating-ui/react';
44import React , { useEffect , useId } from 'react' ;
55
66import { type ComponentProps , type DefaultProps , isHTMLElement , mergeProps , useRender } from '../utils' ;
7- import { useAutocompleteContext } from './autocomplete-context' ;
7+ import { useAutocompleteHighlightContext , useAutocompleteOptionsContext } from './autocomplete-context' ;
88
99export interface AutocompleteOptionProps extends ComponentProps < 'div' > {
1010 value : string ;
@@ -14,72 +14,91 @@ export interface AutocompleteOptionProps extends ComponentProps<'div'> {
1414
1515export const AutocompleteOption = React . forwardRef < HTMLDivElement , AutocompleteOptionProps > (
1616 function AutocompleteOption ( props , ref ) {
17- const { render, value, label, disabled, ...otherProps } = props ;
18- const { activeIndex, selectedValue, getItemProps, handleSelect, valuesByIndexRef, registerSelectedIndex, refs } =
19- useAutocompleteContext ( ) ;
17+ const { activeIndex, selectedValue } = useAutocompleteHighlightContext ( ) ;
18+ const { ref : itemRef , index } = useListItem ( { label : props . label ?? props . value } ) ;
19+ return (
20+ < AutocompleteOptionBody
21+ { ...props }
22+ forwardedRef = { ref }
23+ listItemRef = { itemRef }
24+ index = { index }
25+ isActive = { activeIndex === index }
26+ isSelected = { selectedValue === props . value }
27+ />
28+ ) ;
29+ } ,
30+ ) ;
2031
21- const id = useId ( ) ;
22- const displayLabel = label ?? value ;
23- const { ref : itemRef , index } = useListItem ( { label : displayLabel } ) ;
32+ interface AutocompleteOptionBodyProps extends AutocompleteOptionProps {
33+ forwardedRef : React . ForwardedRef < HTMLDivElement > ;
34+ listItemRef : ( node : HTMLElement | null ) => void ;
35+ index : number ;
36+ isActive : boolean ;
37+ isSelected : boolean ;
38+ }
2439
25- const isSelected = selectedValue === value ;
26- const isActive = activeIndex === index ;
40+ const AutocompleteOptionBody = React . memo ( function AutocompleteOptionBody ( bodyProps : AutocompleteOptionBodyProps ) {
41+ const { forwardedRef : ref , listItemRef : itemRef , index, isActive, isSelected, ...props } = bodyProps ;
42+ const { render, value, label, disabled, ...otherProps } = props ;
43+ const { getItemProps, handleSelect, valuesByIndexRef, registerSelectedIndex, refs } = useAutocompleteOptionsContext ( ) ;
2744
28- useEffect ( ( ) => {
29- const map = valuesByIndexRef . current ;
30- if ( ! disabled ) {
31- map . set ( index , value ) ;
32- }
33- registerSelectedIndex ( index , value ) ;
34- return ( ) => {
35- map . delete ( index ) ;
36- } ;
37- } , [ index , value , disabled , valuesByIndexRef , registerSelectedIndex ] ) ;
45+ const id = useId ( ) ;
46+ const displayLabel = label ?? value ;
3847
39- const state = {
40- selected : isSelected ,
41- active : isActive ,
42- disabled : ! ! disabled ,
48+ useEffect ( ( ) => {
49+ const map = valuesByIndexRef . current ;
50+ if ( ! disabled ) {
51+ map . set ( index , value ) ;
52+ }
53+ registerSelectedIndex ( index , isSelected ) ;
54+ return ( ) => {
55+ map . delete ( index ) ;
4356 } ;
57+ } , [ index , value , disabled , isSelected , valuesByIndexRef , registerSelectedIndex ] ) ;
4458
45- const ownProps = {
46- id,
47- role : 'option' ,
48- 'aria-selected' : isSelected ,
49- 'aria-disabled' : disabled || undefined ,
50- } satisfies DefaultProps < 'div' > ;
59+ const state = {
60+ selected : isSelected ,
61+ active : isActive ,
62+ disabled : ! ! disabled ,
63+ } ;
5164
52- const defaultProps = {
53- ...ownProps ,
54- ...getItemProps ( {
55- onClick ( ) {
56- if ( ! disabled ) {
57- handleSelect ( value , index , displayLabel ) ;
58- const reference = refs . domReference . current ;
59- if ( isHTMLElement ( reference ) ) {
60- reference . focus ( ) ;
61- }
65+ const ownProps = {
66+ id,
67+ role : 'option' ,
68+ 'aria-selected' : isSelected ,
69+ 'aria-disabled' : disabled || undefined ,
70+ } satisfies DefaultProps < 'div' > ;
71+
72+ const defaultProps = {
73+ ...ownProps ,
74+ ...getItemProps ( {
75+ onClick ( ) {
76+ if ( ! disabled ) {
77+ handleSelect ( value , index , displayLabel ) ;
78+ const reference = refs . domReference . current ;
79+ if ( isHTMLElement ( reference ) ) {
80+ reference . focus ( ) ;
6281 }
63- } ,
64- } ) ,
65- } ;
82+ }
83+ } ,
84+ } ) ,
85+ } ;
6686
67- const merged = mergeProps < 'div' > ( defaultProps , otherProps ) ;
68- // The option id is owned by the primitive and drives the input's
69- // aria-activedescendant linkage: a consumer-supplied id must not override it.
70- merged . id = id ;
87+ const merged = mergeProps < 'div' > ( defaultProps , otherProps ) ;
88+ // The option id is owned by the primitive and drives the input's
89+ // aria-activedescendant linkage: a consumer-supplied id must not override it.
90+ merged . id = id ;
7191
72- return useRender ( {
73- defaultTagName : 'div' ,
74- render,
75- ref : [ itemRef , ref ] ,
76- state,
77- stateAttributesMapping : {
78- selected : ( v : boolean ) => ( v ? { 'data-selected' : '' } : null ) ,
79- active : ( v : boolean ) => ( v ? { 'data-active' : '' } : null ) ,
80- disabled : ( v : boolean ) => ( v ? { 'data-disabled' : '' } : null ) ,
81- } ,
82- props : merged ,
83- } ) ;
84- } ,
85- ) ;
92+ return useRender ( {
93+ defaultTagName : 'div' ,
94+ render,
95+ ref : [ itemRef , ref ] ,
96+ state,
97+ stateAttributesMapping : {
98+ selected : ( v : boolean ) => ( v ? { 'data-selected' : '' } : null ) ,
99+ active : ( v : boolean ) => ( v ? { 'data-active' : '' } : null ) ,
100+ disabled : ( v : boolean ) => ( v ? { 'data-disabled' : '' } : null ) ,
101+ } ,
102+ props : merged ,
103+ } ) ;
104+ } ) ;
0 commit comments