Skip to content

Commit 95a9de7

Browse files
committed
[add] View prop nativeID
Maps the View prop 'nativeID' to DOM 'id' as these are equivalent. Enables declarative use of various 'aria-*' properties that require ID references. Ref #1116
1 parent a54bdee commit 95a9de7

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

packages/react-native-web/src/exports/View/ViewPropTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type ViewProps = {
3838
children?: any,
3939
hitSlop?: EdgeInsetsProp,
4040
importantForAccessibility?: 'auto' | 'yes' | 'no' | 'no-hide-descendants',
41+
nativeID?: string,
4142
onBlur?: Function,
4243
onClick?: Function,
4344
onClickCapture?: Function,
@@ -87,6 +88,7 @@ const ViewPropTypes = {
8788
children: any,
8889
hitSlop: EdgeInsetsPropType,
8990
importantForAccessibility: oneOf(['auto', 'no', 'no-hide-descendants', 'yes']),
91+
nativeID: string,
9092
onBlur: func,
9193
onClick: func,
9294
onClickCapture: func,

packages/react-native-web/src/modules/createDOMProps/__tests__/index-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ describe('modules/createDOMProps', () => {
183183
expect(props['aria-hidden']).toEqual(true);
184184
});
185185

186+
test('prop "nativeID" becomes "id"', () => {
187+
const nativeID = 'Example.nativeID';
188+
const props = createProps({ nativeID });
189+
expect(props.id).toEqual(nativeID);
190+
});
191+
186192
test('prop "testID" becomes "data-testid"', () => {
187193
const testID = 'Example.testID';
188194
const props = createProps({ testID });

packages/react-native-web/src/modules/createDOMProps/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const createDOMProps = (component, props, styleResolver) => {
7777
accessibilityLabel,
7878
accessibilityLiveRegion,
7979
importantForAccessibility,
80+
nativeID,
8081
placeholderTextColor,
8182
pointerEvents,
8283
style: providedStyle,
@@ -164,10 +165,15 @@ const createDOMProps = (component, props, styleResolver) => {
164165
}
165166

166167
// OTHER
168+
// Native element ID
169+
if (nativeID && nativeID.constructor === String) {
170+
domProps.id = nativeID;
171+
}
167172
// Link security and automation test ids
168173
if (component === 'a' && domProps.target === '_blank') {
169174
domProps.rel = `${domProps.rel || ''} noopener noreferrer`;
170175
}
176+
// Automated test IDs
171177
if (testID && testID.constructor === String) {
172178
domProps['data-testid'] = testID;
173179
}

packages/website/storybook/1-components/View/ViewScreen.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ const ViewScreen = () => (
119119
]}
120120
/>
121121

122+
<DocItem
123+
name="nativeID"
124+
typeInfo="?string"
125+
description="Used to locate this view from any native DOM code, or to define accessibility relationships. This is rendered to the native 'id' DOM attribute"
126+
/>
127+
122128
<DocItem name="onBlur" typeInfo="?function" />
123129
<DocItem name="onContextMenu" typeInfo="?function" />
124130
<DocItem name="onFocus" typeInfo="?function" />

0 commit comments

Comments
 (0)