-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Role and item announcement in Flatlist #31666
Closed
intergalacticspacehighway
wants to merge
4
commits into
react:main
from
intergalacticspacehighway:flatlist-a11y
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
26d37b7
announce showing x of y items in flatlist
intergalacticspacehighway a40df5d
Merge branch 'master' into flatlist-a11y
intergalacticspacehighway ec09520
feat: grid accessibility announcement in flatlist
intergalacticspacehighway 55877da
feat: nested flatlist example
intergalacticspacehighway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -606,24 +606,71 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> { | |||||
| return ( | ||||||
| <View style={StyleSheet.compose(styles.row, columnWrapperStyle)}> | ||||||
| {item.map((it, kk) => { | ||||||
| const element = renderer({ | ||||||
| item: it, | ||||||
| index: index * numColumns + kk, | ||||||
| separators: info.separators, | ||||||
| }); | ||||||
| const accessibilityCollectionItemInfo = { | ||||||
| rowIndex: index, | ||||||
| rowSpan: 1, | ||||||
| columnIndex: (index * numColumns + kk) % numColumns, | ||||||
| columnSpan: 1, | ||||||
| heading: false, | ||||||
| itemIndex: index * numColumns + kk, | ||||||
| }; | ||||||
|
|
||||||
| const element = ( | ||||||
| <View | ||||||
| importantForAccessibility="yes" | ||||||
| style={styles.cellStyle} | ||||||
| accessibilityCollectionItemInfo={ | ||||||
| accessibilityCollectionItemInfo | ||||||
| }> | ||||||
| {renderer({ | ||||||
| item: it, | ||||||
| index: index * numColumns + kk, | ||||||
| separators: info.separators, | ||||||
| })} | ||||||
| </View> | ||||||
| ); | ||||||
| return element != null ? ( | ||||||
| <React.Fragment key={kk}>{element}</React.Fragment> | ||||||
| ) : null; | ||||||
| })} | ||||||
| </View> | ||||||
| ); | ||||||
| } else { | ||||||
| return renderer(info); | ||||||
| const {index} = info; | ||||||
|
|
||||||
| const accessibilityCollectionItemInfo = { | ||||||
| rowIndex: index, | ||||||
| rowSpan: 1, | ||||||
| columnIndex: 0, | ||||||
| columnSpan: 1, | ||||||
| heading: false, | ||||||
| itemIndex: index, | ||||||
| }; | ||||||
|
|
||||||
| return ( | ||||||
| <View | ||||||
| importantForAccessibility="yes" | ||||||
| style={styles.cellStyle} | ||||||
| accessibilityCollectionItemInfo={accessibilityCollectionItemInfo}> | ||||||
| {renderer(info)} | ||||||
| </View> | ||||||
| ); | ||||||
| } | ||||||
| }, | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| _getAccessibilityCollectionInfo = () => { | ||||||
| const accessibilityCollectionProps = { | ||||||
| itemCount: this.props.data ? this.props.data.length : 0, | ||||||
| rowCount: this._getItemCount(this.props.data), | ||||||
| columnCount: this.props.numColumns, | ||||||
|
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.
Suggested change
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. Thanks @blavalla
|
||||||
| hierarchical: false, | ||||||
| }; | ||||||
|
|
||||||
| return accessibilityCollectionProps; | ||||||
| }; | ||||||
|
|
||||||
| render(): React.Node { | ||||||
| const {numColumns, columnWrapperStyle, ...restProps} = this.props; | ||||||
|
|
||||||
|
|
@@ -633,6 +680,10 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> { | |||||
| getItem={this._getItem} | ||||||
| getItemCount={this._getItemCount} | ||||||
| keyExtractor={this._keyExtractor} | ||||||
| accessibilityCollectionInfo={this._getAccessibilityCollectionInfo()} | ||||||
| accessibilityRole={Platform.select({ | ||||||
| android: this.props.numColumns > 1 ? 'grid' : 'list', | ||||||
| })} | ||||||
| ref={this._captureRef} | ||||||
| viewabilityConfigCallbackPairs={this._virtualizedListPairs} | ||||||
| {...this._renderer()} | ||||||
|
|
@@ -643,6 +694,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> { | |||||
|
|
||||||
| const styles = StyleSheet.create({ | ||||||
| row: {flexDirection: 'row'}, | ||||||
| cellStyle: {flex: 1}, | ||||||
| }); | ||||||
|
|
||||||
| module.exports = FlatList; | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,14 @@ | |
| package com.facebook.react.uimanager; | ||
|
|
||
| import android.content.Context; | ||
| import android.graphics.Rect; | ||
| import android.os.Bundle; | ||
| import android.os.Handler; | ||
| import android.os.Message; | ||
| import android.text.SpannableString; | ||
| import android.text.style.URLSpan; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.view.accessibility.AccessibilityEvent; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.core.view.AccessibilityDelegateCompat; | ||
|
|
@@ -107,6 +109,8 @@ public enum AccessibilityRole { | |
| TAB, | ||
| TABLIST, | ||
| TIMER, | ||
| LIST, | ||
| GRID, | ||
| TOOLBAR; | ||
|
|
||
| public static String getValue(AccessibilityRole role) { | ||
|
|
@@ -135,6 +139,10 @@ public static String getValue(AccessibilityRole role) { | |
| return "android.widget.SpinButton"; | ||
| case SWITCH: | ||
| return "android.widget.Switch"; | ||
| case LIST: | ||
|
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. Oh wow, I didn't realize we didn't have a role for list! Good catch! |
||
| return "android.widget.AbsListView"; | ||
| case GRID: | ||
| return "android.widget.GridView"; | ||
| case NONE: | ||
| case LINK: | ||
| case SUMMARY: | ||
|
|
@@ -204,6 +212,20 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo | |
| } | ||
| final ReadableArray accessibilityActions = | ||
| (ReadableArray) host.getTag(R.id.accessibility_actions); | ||
|
|
||
| final ReadableMap accessibilityCollectionItemInfo = | ||
| (ReadableMap) host.getTag(R.id.accessibility_collection_item_info); | ||
| if (accessibilityCollectionItemInfo != null) { | ||
| int rowIndex = accessibilityCollectionItemInfo.getInt("rowIndex"); | ||
| int columnIndex = accessibilityCollectionItemInfo.getInt("columnIndex"); | ||
| int rowSpan = accessibilityCollectionItemInfo.getInt("rowSpan"); | ||
| int columnSpan = accessibilityCollectionItemInfo.getInt("columnSpan"); | ||
| boolean heading = accessibilityCollectionItemInfo.getBoolean("heading"); | ||
|
|
||
| AccessibilityNodeInfoCompat.CollectionItemInfoCompat collectionItemInfoCompat = AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain(rowIndex, rowSpan, columnIndex, columnSpan, heading); | ||
| info.setCollectionItemInfo(collectionItemInfoCompat); | ||
| } | ||
|
|
||
| if (accessibilityActions != null) { | ||
| for (int i = 0; i < accessibilityActions.size(); i++) { | ||
| final ReadableMap action = accessibilityActions.getMap(i); | ||
|
|
@@ -259,12 +281,14 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo | |
| } | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { | ||
| super.onInitializeAccessibilityEvent(host, event); | ||
| // Set item count and current item index on accessibility events for adjustable | ||
| // in order to make Talkback announce the value of the adjustable | ||
| final ReadableMap accessibilityValue = (ReadableMap) host.getTag(R.id.accessibility_value); | ||
|
|
||
| if (accessibilityValue != null | ||
| && accessibilityValue.hasKey("min") | ||
| && accessibilityValue.hasKey("now") | ||
|
|
@@ -438,7 +462,8 @@ public static void setDelegate(final View view) { | |
| && (view.getTag(R.id.accessibility_role) != null | ||
| || view.getTag(R.id.accessibility_state) != null | ||
| || view.getTag(R.id.accessibility_actions) != null | ||
| || view.getTag(R.id.react_test_id) != null)) { | ||
| || view.getTag(R.id.react_test_id) != null | ||
| || view.getTag(R.id.accessibility_collection_item_info) != null)) { | ||
| ViewCompat.setAccessibilityDelegate(view, new ReactAccessibilityDelegate()); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only issue I can think of with this approach is that it needs an additional wrapper View so that we can get the accessibility collection info on the native side.
The solution would be if we can create accessibility-only Views. I think @amarlette had created an issue for the same, but I am able to find it.