Skip to content

Commit b4e8342

Browse files
committed
suggestions
1 parent 28e4b18 commit b4e8342

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/suggestions.js renamed to src/suggestions.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@ import {getImplicitAriaRoles} from './role-helpers'
66

77
const normalize = getDefaultNormalizer()
88

9-
function getLabelTextFor(element) {
10-
let label =
11-
element.labels &&
12-
Array.from(element.labels).find(el => Boolean(normalize(el.textContent)))
9+
function isElementWithLabels(
10+
element: HTMLElement,
11+
): element is HTMLInputElement {
12+
// We cast with HTMLInputElement, but it could be a HTMLSelectElement
13+
return (element as HTMLInputElement).labels !== undefined
14+
}
15+
16+
function getLabelTextFor(element: HTMLElement): string | undefined {
17+
let label: HTMLLabelElement | undefined =
18+
isElementWithLabels(element) &&
19+
Array.from(element.labels).find((el: HTMLLabelElement) =>
20+
Boolean(normalize(el.textContent)),
21+
)
1322

1423
// non form elements that are using aria-labelledby won't be included in `element.labels`
1524
if (!label) {
@@ -30,7 +39,16 @@ function escapeRegExp(string) {
3039
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
3140
}
3241

33-
function makeSuggestion(queryName, content, {variant = 'get', name}) {
42+
interface MakeSuggestionOptions {
43+
variant?: string
44+
name?: string
45+
}
46+
47+
function makeSuggestion(
48+
queryName,
49+
content,
50+
{variant = 'get', name}: MakeSuggestionOptions,
51+
) {
3452
const queryArgs = [content]
3553

3654
if (name) {
@@ -55,7 +73,20 @@ function makeSuggestion(queryName, content, {variant = 'get', name}) {
5573
}
5674
}
5775

58-
export function getSuggestedQuery(element, variant) {
76+
function isElementWithValue(element: HTMLElement): element is HTMLInputElement {
77+
// We cast with HTMLInputElement, but it could be a HTMLSelectElement
78+
return Boolean((element as HTMLInputElement).value)
79+
}
80+
81+
export interface Suggestion {
82+
queryName: string
83+
toString(): string
84+
}
85+
86+
export function getSuggestedQuery(
87+
element: HTMLElement,
88+
variant?: string,
89+
): Suggestion | undefined {
5990
const role =
6091
element.getAttribute('role') ?? getImplicitAriaRoles(element)?.[0]
6192
if (role) {
@@ -80,7 +111,7 @@ export function getSuggestedQuery(element, variant) {
80111
return makeSuggestion('Text', textContent, {variant})
81112
}
82113

83-
if (element.value) {
114+
if (isElementWithValue(element)) {
84115
return makeSuggestion('DisplayValue', normalize(element.value), {variant})
85116
}
86117

types/suggestions.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ export interface Suggestion {
22
queryName: string
33
toString(): string
44
}
5-
6-
export function getSuggestedQuery(element: HTMLElement): Suggestion | undefined
5+
export declare function getSuggestedQuery(
6+
element: HTMLElement,
7+
variant?: string,
8+
): Suggestion | undefined

0 commit comments

Comments
 (0)