-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: (WIP) Autocomplete context refactor #8695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: baseCollection_filter
Are you sure you want to change the base?
Changes from 9 commits
c111744
24dd7cb
d8969a7
ae3cc84
3a81f31
db91b3a
7177106
dfb80f3
ec8066f
76107ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,45 @@ | |
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {AriaAutocompleteProps, CollectionOptions, useAutocomplete} from '@react-aria/autocomplete'; | ||
import {AriaAutocompleteProps, useAutocomplete} from '@react-aria/autocomplete'; | ||
import {AriaLabelingProps, DOMProps, FocusEvents, KeyboardEvents, Node, ValueBase} from '@react-types/shared'; | ||
import {AriaTextFieldProps} from '@react-aria/textfield'; | ||
import {AutocompleteState, useAutocompleteState} from '@react-stately/autocomplete'; | ||
import {InputContext} from './Input'; | ||
import {ContextValue, Provider, removeDataAttributes, SlotProps, SlottedContextValue, useSlottedContext} from './utils'; | ||
import {mergeProps} from '@react-aria/utils'; | ||
import {Node} from '@react-types/shared'; | ||
import {Provider, removeDataAttributes, SlotProps, SlottedContextValue, useSlottedContext} from './utils'; | ||
import React, {createContext, JSX, RefObject, useRef} from 'react'; | ||
import {SearchFieldContext} from './SearchField'; | ||
import {TextFieldContext} from './TextField'; | ||
|
||
export interface AutocompleteProps<T> extends AriaAutocompleteProps<T>, SlotProps {} | ||
|
||
interface InternalAutocompleteContextValue<T> { | ||
// TODO: naming | ||
// IMO I think this could also contain the props that useSelectableCollection takes (minus the selection options?) | ||
interface CollectionContextValue<T> extends DOMProps, AriaLabelingProps { | ||
filter?: (nodeTextValue: string, node: Node<T>) => boolean, | ||
collectionProps: CollectionOptions, | ||
collectionRef: RefObject<HTMLElement | null> | ||
/** Whether the collection items should use virtual focus instead of being focused directly. */ | ||
shouldUseVirtualFocus?: boolean, | ||
/** Whether typeahead is disabled. */ | ||
disallowTypeAhead?: boolean, | ||
collectionRef?: RefObject<HTMLElement | null> | ||
} | ||
|
||
// TODO: naming | ||
interface FieldInputContextValue<T = HTMLInputElement> extends | ||
DOMProps, | ||
FocusEvents<T>, | ||
KeyboardEvents, | ||
Pick<ValueBase<string>, 'onChange' | 'value'>, | ||
Pick<AriaTextFieldProps, 'enterKeyHint' | 'aria-controls' | 'aria-autocomplete' | 'aria-activedescendant' | 'spellCheck' | 'autoCorrect' | 'autoComplete'> {} | ||
|
||
export const AutocompleteContext = createContext<SlottedContextValue<Partial<AutocompleteProps<any>>>>(null); | ||
export const AutocompleteStateContext = createContext<AutocompleteState | null>(null); | ||
// This context is to pass the register and filter down to whatever collection component is wrapped by the Autocomplete | ||
// TODO: export from RAC, but rename to something more appropriate | ||
export const UNSTABLE_InternalAutocompleteContext = createContext<InternalAutocompleteContextValue<any> | null>(null); | ||
|
||
// TODO export from RAC, maybe move up and out of Autocomplete | ||
// also can't make this use ContextValue (so that we can call useContextProps) like FieldInput for a similar reason. The HTMLElement type for the ref | ||
// makes useContextProps complain since it doesn't mesh up with HTMLDivElement | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, move up |
||
export const CollectionContext = createContext<CollectionContextValue<any> | null>(null); | ||
// TODO: too restrictive to type this as a HTMLInputElement? Needed for the ref merging that happens in TextField/SearchField | ||
// Attempted to use FocusableElement but as mentioned above, SearchField and TextField complain since they expect HTMLInputElement for their hooks and stuff | ||
export const FieldInputContext = createContext<ContextValue<FieldInputContextValue, HTMLInputElement>>(null); | ||
Comment on lines
+45
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Types are the final problem here. The problem is two fold, ideally we'd be able to have For |
||
|
||
/** | ||
* An autocomplete combines a TextField or SearchField with a Menu or ListBox, allowing users to search or filter a list of suggestions. | ||
|
@@ -61,12 +77,13 @@ export function Autocomplete<T extends object>(props: AutocompleteProps<T>): JSX | |
<Provider | ||
values={[ | ||
[AutocompleteStateContext, state], | ||
[SearchFieldContext, textFieldProps], | ||
[TextFieldContext, textFieldProps], | ||
[InputContext, {ref: inputRef}], | ||
[UNSTABLE_InternalAutocompleteContext, { | ||
filter: filterFn as (nodeTextValue: string, node: Node<T>) => boolean, | ||
collectionProps, | ||
[FieldInputContext, { | ||
...textFieldProps, | ||
ref: inputRef | ||
}], | ||
[CollectionContext, { | ||
...collectionProps, | ||
filter: filterFn, | ||
collectionRef: mergedCollectionRef | ||
}] | ||
]}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
import {AriaListBoxOptions, AriaListBoxProps, DraggableItemResult, DragPreviewRenderer, DroppableCollectionResult, DroppableItemResult, FocusScope, ListKeyboardDelegate, mergeProps, useCollator, useFocusRing, useHover, useListBox, useListBoxSection, useLocale, useOption} from 'react-aria'; | ||
import {Collection, CollectionBuilder, createBranchComponent, createLeafComponent, FilterLessNode, ItemNode, SectionNode} from '@react-aria/collections'; | ||
import {CollectionContext} from './Autocomplete'; | ||
import {CollectionProps, CollectionRendererContext, ItemRenderProps, SectionContext, SectionProps} from './Collection'; | ||
import {ContextValue, DEFAULT_SLOT, Provider, RenderProps, SlotProps, StyleProps, StyleRenderProps, useContextProps, useRenderProps, useSlot} from './utils'; | ||
import {DragAndDropContext, DropIndicatorContext, DropIndicatorProps, useDndPersistedKeys, useRenderDropIndicator} from './DragAndDrop'; | ||
|
@@ -23,7 +24,6 @@ import {HeaderContext} from './Header'; | |
import React, {createContext, ForwardedRef, forwardRef, JSX, ReactNode, useContext, useEffect, useMemo, useRef} from 'react'; | ||
import {SeparatorContext} from './Separator'; | ||
import {TextContext} from './Text'; | ||
import {UNSTABLE_InternalAutocompleteContext} from './Autocomplete'; | ||
|
||
export interface ListBoxRenderProps { | ||
/** | ||
|
@@ -120,7 +120,7 @@ interface ListBoxInnerProps<T> { | |
} | ||
|
||
function ListBoxInner<T extends object>({state: inputState, props, listBoxRef}: ListBoxInnerProps<T>) { | ||
let {filter, collectionProps, collectionRef} = useContext(UNSTABLE_InternalAutocompleteContext) || {}; | ||
let {filter, collectionRef, ...collectionProps} = useContext(CollectionContext) || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we still using not actually attached to this line, but it's nearby |
||
props = useMemo(() => collectionProps ? ({...props, ...collectionProps}) : props, [props, collectionProps]); | ||
let {dragAndDropHooks, layout = 'stack', orientation = 'vertical'} = props; | ||
// Memoed so that useAutocomplete callback ref is properly only called once on mount and not everytime a rerender happens | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import {AriaMenuProps, FocusScope, mergeProps, useHover, useMenu, useMenuItem, useMenuSection, useMenuTrigger, useSubmenuTrigger} from 'react-aria'; | ||
import {BaseCollection, Collection, CollectionBuilder, CollectionNode, createBranchComponent, createLeafComponent, ItemNode, SectionNode} from '@react-aria/collections'; | ||
import {MenuTriggerProps as BaseMenuTriggerProps, Collection as ICollection, Node, RootMenuTriggerState, TreeState, useMenuTriggerState, useSubmenuTriggerState, useTreeState} from 'react-stately'; | ||
import {CollectionContext} from './Autocomplete'; | ||
import {CollectionProps, CollectionRendererContext, ItemRenderProps, SectionContext, SectionProps, usePersistedKeys} from './Collection'; | ||
import {ContextValue, DEFAULT_SLOT, Provider, RenderProps, SlotProps, StyleRenderProps, useContextProps, useRenderProps, useSlot, useSlottedContext} from './utils'; | ||
import {filterDOMProps, mergeRefs, useObjectRef, useResizeObserver} from '@react-aria/utils'; | ||
|
@@ -39,7 +40,6 @@ import React, { | |
} from 'react'; | ||
import {SeparatorContext} from './Separator'; | ||
import {TextContext} from './Text'; | ||
import {UNSTABLE_InternalAutocompleteContext} from './Autocomplete'; | ||
|
||
export const MenuContext = createContext<ContextValue<MenuProps<any>, HTMLDivElement>>(null); | ||
export const MenuStateContext = createContext<TreeState<any> | null>(null); | ||
|
@@ -202,7 +202,7 @@ interface MenuInnerProps<T> { | |
} | ||
|
||
function MenuInner<T extends object>({props, collection, menuRef: ref}: MenuInnerProps<T>) { | ||
let {filter, collectionProps: autocompleteMenuProps, collectionRef} = useContext(UNSTABLE_InternalAutocompleteContext) || {}; | ||
let {filter, collectionRef, ...autocompleteMenuProps} = useContext(CollectionContext) || {}; | ||
// Memoed so that useAutocomplete callback ref is properly only called once on mount and not everytime a rerender happens | ||
ref = useObjectRef(useMemo(() => mergeRefs(ref, collectionRef !== undefined ? collectionRef as RefObject<HTMLDivElement> : null), [collectionRef, ref])); | ||
let filteredCollection = useMemo(() => filter ? collection.filter(filter) : collection, [collection, filter]); | ||
|
@@ -251,7 +251,7 @@ function MenuInner<T extends object>({props, collection, menuRef: ref}: MenuInne | |
[SectionContext, {name: 'MenuSection', render: MenuSectionInner}], | ||
[SubmenuTriggerContext, {parentMenuRef: ref, shouldUseVirtualFocus: autocompleteMenuProps?.shouldUseVirtualFocus}], | ||
[MenuItemContext, null], | ||
[UNSTABLE_InternalAutocompleteContext, null], | ||
[CollectionContext, null], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should ListBox and GridList also clear this now that we support nested grids? ex TagGroup inside Cell? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also clear the new input context |
||
[SelectionManagerContext, state.selectionManager], | ||
/* Ensure root MenuTriggerState is defined, in case Menu is rendered outside a MenuTrigger. */ | ||
/* We assume the context can never change between defined and undefined. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems more reasonable, this is specific to the Autocomplete, whereas the CollectionContext is specific to the Collections. Unless we had something in mind for this context that wasn't Autocomplete?