Skip to content

Commit 8175736

Browse files
committed
basic updates
1 parent 81b6ded commit 8175736

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/helpers/__tests__/accessiblity.test.tsx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import React from 'react';
22
import { Pressable, Switch, Text, TextInput, TouchableOpacity, View } from 'react-native';
33

44
import { isHiddenFromAccessibility, isInaccessible, render, screen } from '../..';
5-
import { computeAriaDisabled, computeAriaLabel, isAccessibilityElement } from '../accessibility';
5+
import {
6+
computeAccessibleName,
7+
computeAriaDisabled,
8+
computeAriaLabel,
9+
isAccessibilityElement,
10+
} from '../accessibility';
611

712
describe('isHiddenFromAccessibility', () => {
813
test('returns false for accessible elements', async () => {
@@ -476,3 +481,45 @@ describe('computeAriaDisabled', () => {
476481
expect(computeAriaDisabled(screen.getByText('ARIA Disabled Text'))).toBe(true);
477482
});
478483
});
484+
485+
describe('computeAccessibleName', () => {
486+
test('basic cases', async () => {
487+
await render(
488+
<>
489+
<View testID="aria-label" aria-label="ARIA Label" />
490+
<View testID="accessibility-label" accessibilityLabel="Accessibility Label" />
491+
<View testID="text-content">
492+
<Text>Text Content</Text>
493+
</View>
494+
<TextInput testID="text-input" placeholder="Text Input" />
495+
</>,
496+
);
497+
expect(computeAccessibleName(screen.getByTestId('aria-label'))).toBe('ARIA Label');
498+
expect(computeAccessibleName(screen.getByTestId('accessibility-label'))).toBe(
499+
'Accessibility Label',
500+
);
501+
expect(computeAccessibleName(screen.getByTestId('text-content'))).toBe('Text Content');
502+
expect(computeAccessibleName(screen.getByTestId('text-input'))).toBe('Text Input');
503+
});
504+
505+
test('basic precedence', async () => {
506+
await render(
507+
<>
508+
<View testID="aria-label" aria-label="ARIA Label" accessibilityLabel="Accessibility Label">
509+
<Text>Text Content</Text>
510+
</View>
511+
<View testID="accessibility-label" accessibilityLabel="Accessibility Label">
512+
<Text>Text Content</Text>
513+
</View>
514+
<View testID="text-content">
515+
<Text>Text Content</Text>
516+
</View>
517+
</>,
518+
);
519+
expect(computeAccessibleName(screen.getByTestId('aria-label'))).toBe('ARIA Label');
520+
expect(computeAccessibleName(screen.getByTestId('accessibility-label'))).toBe(
521+
'Accessibility Label',
522+
);
523+
expect(computeAccessibleName(screen.getByTestId('text-content'))).toBe('Text Content');
524+
});
525+
});

src/helpers/accessibility.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getContainerElement, getHostSiblings, isHostElement } from './component
66
import { findAll } from './find-all';
77
import { isHostImage, isHostSwitch, isHostText, isHostTextInput } from './host-component-names';
88
import { getTextContent } from './text-content';
9-
import { isEditableTextInput } from './text-input';
9+
import { getTextInputValue, isEditableTextInput } from './text-input';
1010

1111
type IsInaccessibleOptions = {
1212
cache?: WeakMap<HostElement, boolean>;
@@ -245,7 +245,30 @@ export function computeAriaValue(element: HostElement): AccessibilityValue {
245245
}
246246

247247
export function computeAccessibleName(element: HostElement): string | undefined {
248-
return computeAriaLabel(element) ?? getTextContent(element);
248+
const label = computeAriaLabel(element);
249+
if (label) {
250+
return label;
251+
}
252+
253+
if (isHostTextInput(element) && element.props.placeholder) {
254+
return element.props.placeholder;
255+
}
256+
257+
const parts = [];
258+
for (const child of element.children) {
259+
if (typeof child === 'string') {
260+
if (child) {
261+
parts.push(child);
262+
}
263+
} else {
264+
const childLabel = computeAccessibleName(child);
265+
if (childLabel) {
266+
parts.push(childLabel);
267+
}
268+
}
269+
}
270+
271+
return parts.join(' ');
249272
}
250273

251274
type RoleSupportMap = Partial<Record<Role | AccessibilityRole, true>>;

0 commit comments

Comments
 (0)