Repository navigation
fix(Typeahead): a Typeahead result that renderItem returns as a TypeaheadItem with isDisabled, in its usual layout, is announced as disabled and never picked #11833
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: main
Are you sure you want to change the base?
Changes from 10 commits
6c2436b
950e76c
a7ae9fe
14e0f24
7270ae2
40c0f47
cd64420
d0cbf92
3cb4e1f
3c16d0b
9b4ef19
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [fix] Typeahead: a Typeahead result that renders as a `TypeaheadItem` with `isDisabled` (`renderItem` returns it directly, or the default rendering), in its usual layout (not `item.element`), is now announced as disabled, and Enter, click or tap on it picks nothing and leaves the list open, also inside a shadow root. The pointer does not highlight it; arrow keys still reach it. | ||
|
|
||
| @cixzhang |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| */ | ||
|
|
||
| import React, { | ||
| isValidElement, | ||
| use, | ||
| useCallback, | ||
| useEffect, | ||
|
|
@@ -54,7 +55,7 @@ import { | |
| useOutsidePress, | ||
| } from '../Layer/outsidePress'; | ||
| import {isImeKeyEvent} from '../utils/ime'; | ||
| import {TypeaheadItem} from './TypeaheadItem'; | ||
| import {TypeaheadItem, type TypeaheadItemProps} from './TypeaheadItem'; | ||
| import {Icon} from '../Icon'; | ||
| import {Spinner} from '../Spinner'; | ||
| import {SpinnerOwnerContext} from '../Spinner/SpinnerOwnerContext'; | ||
|
|
@@ -90,6 +91,7 @@ import { | |
| } from '../i18n/useComponentTranslator'; | ||
| import english from '../i18n/generated-english/Typeahead/BaseTypeahead.generated'; | ||
| import {textEntryTraitAttributes} from '../Field/textEntryTraits'; | ||
| import {isDisabledControl} from '../utils/pressDisabled'; | ||
|
|
||
| registerEnglishFallback(english); | ||
| // ============================================================================= | ||
|
|
@@ -591,6 +593,20 @@ function isBelowMinQueryLength(query: string, minQueryLength: number): boolean { | |
| // Component | ||
| // ============================================================================= | ||
|
|
||
| /** | ||
| * Whether a result's rendered content is a disabled TypeaheadItem in its | ||
| * usual layout. TypeaheadItem renders a truthy `item.element` as is, with no | ||
| * disabled state, so only a falsy one counts. | ||
| */ | ||
| function isDisabledTypeaheadItem(content: ReactNode): boolean { | ||
| return ( | ||
| isValidElement<TypeaheadItemProps<SearchableItem>>(content) && | ||
| content.type === TypeaheadItem && | ||
| content.props.isDisabled === true && | ||
| !content.props.item?.element | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Combobox engine: input, search, keyboard navigation, and dropdown. | ||
| * | ||
|
|
@@ -670,6 +686,9 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
| // screen-reader user something the sighted user is not reading | ||
| // (`spec:AST-056` AR1). | ||
| const emptyStateRef = useRef<HTMLDivElement>(null); | ||
| // A pick reads the disabled mark from the row it acts on, not from the | ||
| // item, so two results with the same id stay apart. | ||
| const optionElementsRef = useRef<(HTMLElement | null)[]>([]); | ||
| const generatedId = useId(); | ||
| // Keep the released input-specific aliases authoritative when a caller uses | ||
| // them, but do not let an omitted alias erase the equivalent native BaseProp. | ||
|
|
@@ -1684,7 +1703,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
|
|
||
| // Handle item selection | ||
| const handleSelect = useCallback( | ||
| (item: T) => { | ||
| (item: T, index: number) => { | ||
| // A disabled field changes nothing, also from a list that was open | ||
| // before it became disabled (spec:AST-085 FR2). | ||
| if (isDisabled) { | ||
|
|
@@ -1698,6 +1717,10 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
| const pickedIndex = displayedResults.findIndex( | ||
| other => other.id === item.id, | ||
| ); | ||
| const option = optionElementsRef.current[index]; | ||
|
Contributor
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. Miss (non-blocking): this check now sits between the focus-list comment and |
||
| if (option != null && isDisabledControl(option)) { | ||
| return; | ||
| } | ||
| const nextIndex = | ||
| pickedIndex + 1 < displayedResults.length | ||
| ? pickedIndex + 1 | ||
|
|
@@ -2074,7 +2097,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
| break; | ||
| } | ||
| if (highlightedIndex >= 0 && highlightedIndex < results.length) { | ||
| handleSelect(displayedResults[highlightedIndex]); | ||
| handleSelect(displayedResults[highlightedIndex], highlightedIndex); | ||
| } | ||
| break; | ||
| case 'Escape': | ||
|
|
@@ -2327,21 +2350,45 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
| const index = flatIndex++; | ||
| const itemKey = getKey(item.id, index); | ||
| const isSelected = itemKey === selectedKey; | ||
| const content = renderItem ? ( | ||
| renderItem(item) | ||
| ) : ( | ||
| <TypeaheadItem item={item} xstyle={styles.defaultItem} /> | ||
| ); | ||
| return ( | ||
| <div | ||
| key={itemKey} | ||
| ref={node => { | ||
| optionElementsRef.current[index] = node; | ||
| }} | ||
| id={getItemId(index)} | ||
| role="option" | ||
| aria-selected={isSelected} | ||
| aria-disabled={isDisabledTypeaheadItem(content) || undefined} | ||
| tabIndex={-1} | ||
| onMouseDown={canKeepFocusList ? preventOptionFocus : undefined} | ||
| onClick={() => handleSelect(item)} | ||
| onMouseDown={event => { | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| // A press on a disabled option keeps focus in the input, | ||
| // so the list stays open. | ||
| if ( | ||
| canKeepFocusList || | ||
| isDisabledControl(event.currentTarget) | ||
| ) { | ||
| preventOptionFocus(event); | ||
| } | ||
| }} | ||
| onClick={() => handleSelect(item, index)} | ||
| // Highlight follows pointer movement, not pointer | ||
| // presence: a menu that opens or reflows under a resting | ||
| // pointer keeps its keyboard highlight, so Enter acts on | ||
| // the option the user was shown, not the one that | ||
| // happened to appear under the cursor. | ||
| onMouseMove={() => highlightOnHover(index)} | ||
| onMouseMove={event => { | ||
| // A disabled option takes no hover highlight: it cannot | ||
| // be picked. | ||
| if (!isDisabledControl(event.currentTarget)) { | ||
| highlightOnHover(index); | ||
| } | ||
| }} | ||
| {...mergeProps( | ||
| themeProps('typeahead-option', { | ||
| size, | ||
|
|
@@ -2356,13 +2403,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({ | |
| isSelected && styles.itemSelected, | ||
| ), | ||
| )}> | ||
| <span {...stylex.props(styles.itemContent)}> | ||
| {renderItem ? ( | ||
| renderItem(item) | ||
| ) : ( | ||
| <TypeaheadItem item={item} xstyle={styles.defaultItem} /> | ||
| )} | ||
| </span> | ||
| <span {...stylex.props(styles.itemContent)}>{content}</span> | ||
| {isSelected && <Icon icon="check" size="sm" color="primary" />} | ||
| </div> | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.