Skip to content

Commit 0cd3092

Browse files
Saadnajmichristophpurrerappden
authored
[0.66] Deprecate onScrollKeyDown, refactor Flatlist selection logic (#1375)
* Revert "Revert Flatlist changes (#1306)" This reverts commit 8ce9545. * Refactor handling of keyDown/keyUp (#1338) This refactors / simplifies certain keyUp|Down event handling. It will make a later change (adding textInput handling for textInput fields) easier (to review) Co-authored-by: Scott Kyle <[email protected]> * Deprecate onScrollKeyDown, refactor Flatlist selection logic (#1365) * Deprecate onScrollKeyDown remove pressable diff Remove JS handling for PageUp/Down, fix flow errors Add back "autoscroll to focused view" behavior remove commented code remove change to pressable Update documentation fix flow error fix lint issue Fix 'selectRowAtIndex' More simplification lock * Make method public again * Add initialSelectedIndex * macOS tags * fix lint * remove one more reference * yarn lint --fix * RNTester: only show the Flatlist keyboard navigation switch on macOS Co-authored-by: Christoph Purrer <[email protected]> Co-authored-by: Scott Kyle <[email protected]>
1 parent d9737f1 commit 0cd3092

22 files changed

+387
-570
lines changed

Libraries/Components/ScrollView/ScrollView.js

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,82 +1179,6 @@ class ScrollView extends React.Component<Props, State> {
11791179
}
11801180

11811181
// [TODO(macOS GH#774)
1182-
_handleKeyDown = (event: ScrollEvent) => {
1183-
if (this.props.onScrollKeyDown) {
1184-
this.props.onScrollKeyDown(event);
1185-
} else {
1186-
if (Platform.OS === 'macos') {
1187-
const nativeEvent = event.nativeEvent;
1188-
const key = nativeEvent.key;
1189-
const kMinScrollOffset = 10;
1190-
if (key === 'PAGE_UP') {
1191-
this._handleScrollByKeyDown(event, {
1192-
x: nativeEvent.contentOffset.x,
1193-
y:
1194-
nativeEvent.contentOffset.y +
1195-
-nativeEvent.layoutMeasurement.height,
1196-
});
1197-
} else if (key === 'PAGE_DOWN') {
1198-
this._handleScrollByKeyDown(event, {
1199-
x: nativeEvent.contentOffset.x,
1200-
y:
1201-
nativeEvent.contentOffset.y +
1202-
nativeEvent.layoutMeasurement.height,
1203-
});
1204-
} else if (key === 'LEFT_ARROW') {
1205-
this._handleScrollByKeyDown(event, {
1206-
x:
1207-
nativeEvent.contentOffset.x +
1208-
-(this.props.horizontalLineScroll !== undefined
1209-
? this.props.horizontalLineScroll
1210-
: kMinScrollOffset),
1211-
y: nativeEvent.contentOffset.y,
1212-
});
1213-
} else if (key === 'RIGHT_ARROW') {
1214-
this._handleScrollByKeyDown(event, {
1215-
x:
1216-
nativeEvent.contentOffset.x +
1217-
(this.props.horizontalLineScroll !== undefined
1218-
? this.props.horizontalLineScroll
1219-
: kMinScrollOffset),
1220-
y: nativeEvent.contentOffset.y,
1221-
});
1222-
} else if (key === 'DOWN_ARROW') {
1223-
this._handleScrollByKeyDown(event, {
1224-
x: nativeEvent.contentOffset.x,
1225-
y:
1226-
nativeEvent.contentOffset.y +
1227-
(this.props.verticalLineScroll !== undefined
1228-
? this.props.verticalLineScroll
1229-
: kMinScrollOffset),
1230-
});
1231-
} else if (key === 'UP_ARROW') {
1232-
this._handleScrollByKeyDown(event, {
1233-
x: nativeEvent.contentOffset.x,
1234-
y:
1235-
nativeEvent.contentOffset.y +
1236-
-(this.props.verticalLineScroll !== undefined
1237-
? this.props.verticalLineScroll
1238-
: kMinScrollOffset),
1239-
});
1240-
}
1241-
}
1242-
}
1243-
};
1244-
1245-
_handleScrollByKeyDown = (event: ScrollEvent, newOffset) => {
1246-
const maxX =
1247-
event.nativeEvent.contentSize.width -
1248-
event.nativeEvent.layoutMeasurement.width;
1249-
const maxY =
1250-
event.nativeEvent.contentSize.height -
1251-
event.nativeEvent.layoutMeasurement.height;
1252-
this.scrollTo({
1253-
x: Math.max(0, Math.min(maxX, newOffset.x)),
1254-
y: Math.max(0, Math.min(maxY, newOffset.y)),
1255-
});
1256-
};
1257-
12581182
_handlePreferredScrollerStyleDidChange = (event: ScrollEvent) => {
12591183
this.setState({contentKey: this.state.contentKey + 1});
12601184
}; // ]TODO(macOS GH#774)
@@ -1807,7 +1731,6 @@ class ScrollView extends React.Component<Props, State> {
18071731
// Override the onContentSizeChange from props, since this event can
18081732
// bubble up from TextInputs
18091733
onContentSizeChange: null,
1810-
onScrollKeyDown: this._handleKeyDown, // TODO(macOS GH#774)
18111734
onPreferredScrollerStyleDidChange: this
18121735
._handlePreferredScrollerStyleDidChange, // TODO(macOS GH#774)
18131736
onLayout: this._handleLayout,

Libraries/Components/View/ViewPropTypes.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ type DirectEventProps = $ReadOnly<{|
7676
*/
7777
onPreferredScrollerStyleDidChange?: ?(event: ScrollEvent) => mixed, // TODO(macOS GH#774)
7878

79-
/**
80-
* When `acceptsKeyboardFocus` is true, the system will try to invoke this function
81-
* when the user performs accessibility key down gesture.
82-
*/
83-
onScrollKeyDown?: ?(event: ScrollEvent) => mixed, // TODO(macOS GH#774)
84-
8579
/**
8680
* Invoked on mount and layout changes with:
8781
*

Libraries/Lists/FlatList.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ type OptionalProps<ItemT> = {|
6666
* Optional custom style for multi-item rows generated when numColumns > 1.
6767
*/
6868
columnWrapperStyle?: ViewStyleProp,
69+
// [TODO(macOS GH#774)
70+
/**
71+
* Allows you to 'select' a row using arrow keys. The selected row will have the prop `isSelected`
72+
* passed in as true to it's renderItem / ListItemComponent. You can also imperatively select a row
73+
* using the `selectRowAtIndex` method. You can set the initially selected row using the
74+
* `initialSelectedIndex` prop.
75+
* Keyboard Behavior:
76+
* - ArrowUp: Select row above current selected row
77+
* - ArrowDown: Select row below current selected row
78+
* - Option+ArrowUp: Select the first row
79+
* - Opton+ArrowDown: Select the last 'realized' row
80+
* - Home: Scroll to top of list
81+
* - End: Scroll to end of list
82+
*
83+
* @platform macos
84+
*/
85+
enableSelectionOnKeyPress?: ?boolean,
86+
// ]TODO(macOS GH#774)
6987
/**
7088
* A marker property for telling the list to re-render (since it implements `PureComponent`). If
7189
* any of your `renderItem`, Header, Footer, etc. functions depend on anything outside of the
@@ -111,6 +129,12 @@ type OptionalProps<ItemT> = {|
111129
* `getItemLayout` to be implemented.
112130
*/
113131
initialScrollIndex?: ?number,
132+
// [TODO(macOS GH#774)
133+
/**
134+
* The initially selected row, if `enableSelectionOnKeyPress` is set.
135+
*/
136+
initialSelectedIndex?: ?number,
137+
// ]TODO(macOS GH#774)
114138
/**
115139
* Reverses the direction of scroll. Uses scale transforms of -1.
116140
*/
@@ -369,6 +393,19 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
369393
}
370394
}
371395

396+
// [TODO(macOS GH#750)
397+
/**
398+
* Move selection to the specified index
399+
*
400+
* @platform macos
401+
*/
402+
selectRowAtIndex(index: number) {
403+
if (this._listRef) {
404+
this._listRef.selectRowAtIndex(index);
405+
}
406+
}
407+
// ]TODO(macOS GH#750)
408+
372409
/**
373410
* Provides a handle to the underlying scroll responder.
374411
*/

0 commit comments

Comments
 (0)