Skip to content

Commit b23e312

Browse files
committed
fix: Remove FocusGroup in ScrollView
1 parent f9a9cab commit b23e312

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

.changeset/lemon-dots-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plextv/react-native-lightning": patch
3+
---
4+
5+
fix: Remove FocusGroup in ScrollView

packages/react-native-lightning/src/exports/ScrollView.tsx

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@ import {
22
type LightningElement,
33
LightningViewElement,
44
type LightningViewElementProps,
5-
simpleDiff,
65
} from '@plextv/react-lightning';
7-
import { Component, createRef } from 'react';
6+
import { createRef, PureComponent } from 'react';
87
import type { JSX } from 'react/jsx-runtime';
98
import type {
109
NativeScrollEvent,
1110
ScrollView as RNScrollView,
1211
ScrollViewProps as RNScrollViewProps,
1312
} from 'react-native';
14-
import { View } from 'react-native';
1513
import type { LightningViewElementStyle } from '../../../react-lightning/src/types';
14+
import { createHandler } from '../hooks/useFocusHandler';
1615
import { createNativeSyntheticEvent } from '../utils/createNativeSyntheticEvent';
17-
import { FocusGroup } from './FocusGroup';
18-
import { defaultViewStyle } from './View';
16+
import { defaultViewStyle, View } from './View';
1917

2018
type ScrollViewProps = RNScrollViewProps & {
2119
animated?: boolean;
22-
onChildFocused?: (el: LightningElement) => void;
2320
};
2421

2522
type ScrollViewState = {
@@ -108,7 +105,10 @@ function getScrollInfo(
108105
};
109106
}
110107

111-
export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
108+
export class ScrollView extends PureComponent<
109+
ScrollViewProps,
110+
ScrollViewState
111+
> {
112112
private _containerRef = createRef<LightningViewElement>();
113113
private _viewportRef = createRef<LightningViewElement>();
114114

@@ -189,24 +189,9 @@ export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
189189
return this._containerRef.current;
190190
}
191191

192-
public shouldComponentUpdate(
193-
nextProps: ScrollViewProps,
194-
nextState: ScrollViewState,
195-
): boolean {
196-
return (
197-
!!simpleDiff(this.props, nextProps) || !!simpleDiff(this.state, nextState)
198-
);
199-
}
200-
201192
public render(): JSX.Element {
202-
const {
203-
children,
204-
style,
205-
contentContainerStyle,
206-
horizontal,
207-
onChildFocused,
208-
...props
209-
} = this.props;
193+
const { children, style, contentContainerStyle, horizontal, ...props } =
194+
this.props;
210195
const flexDirection = horizontal ? 'row' : 'column';
211196

212197
return (
@@ -218,8 +203,10 @@ export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
218203
{ overflow: 'hidden', flexDirection, flexGrow: 1, flexShrink: 1 },
219204
]}
220205
{...props}
206+
onFocus={createHandler(this.props.onFocus)}
207+
onBlur={createHandler(this.props.onBlur)}
221208
>
222-
<FocusGroup
209+
<View
223210
ref={this._containerRef}
224211
transition={
225212
this.state.animated
@@ -229,7 +216,6 @@ export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
229216
}
230217
: undefined
231218
}
232-
onChildFocused={onChildFocused}
233219
style={[
234220
defaultViewStyle,
235221
{ display: 'flex', flexDirection },
@@ -238,7 +224,7 @@ export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
238224
]}
239225
>
240226
{children}
241-
</FocusGroup>
227+
</View>
242228
</View>
243229
);
244230
}
@@ -264,9 +250,7 @@ export class ScrollView extends Component<ScrollViewProps, ScrollViewState> {
264250
};
265251

266252
private _setViewRef = (ref: View | null) => {
267-
this._viewportRef.current = ref
268-
? (ref as unknown as LightningViewElement)
269-
: null;
253+
this._viewportRef.current = ref ?? null;
270254
};
271255

272256
private _doScroll(newOffset: NativeScrollEvent) {

packages/react-native-lightning/src/hooks/useFocusHandler.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,45 @@ export type FocusHandler<T extends 'focus' | 'blur', TEvent> = (
1919
| LightningElement,
2020
) => void | Promise<void>;
2121

22+
function handleEvent<T extends 'focus' | 'blur'>(
23+
element: Parameters<FocusHandler<T, TargetedEvent>>[0],
24+
onEvent: ViewProps[`on${Capitalize<T>}`] | undefined,
25+
): void {
26+
if (element instanceof LightningViewElement) {
27+
onEvent?.(
28+
createNativeSyntheticEvent<TargetedEvent>(
29+
{ target: element.id },
30+
element,
31+
),
32+
);
33+
}
34+
}
35+
2236
function useHandler<T extends 'focus' | 'blur'>(
23-
_eventType: T,
2437
onEvent?: ViewProps[`on${Capitalize<T>}`],
2538
): FocusHandler<T, TargetedEvent> | undefined {
2639
const handler = useCallback<FocusHandler<T, TargetedEvent>>(
27-
(element) => {
28-
if (element instanceof LightningViewElement) {
29-
onEvent?.(
30-
createNativeSyntheticEvent<TargetedEvent>(
31-
{ target: element.id },
32-
element,
33-
),
34-
);
35-
}
36-
},
40+
(element) => handleEvent(element, onEvent),
3741
[onEvent],
3842
);
3943

4044
return onEvent ? handler : undefined;
4145
}
4246

47+
export function createHandler<T extends 'focus' | 'blur'>(
48+
onEvent?: ViewProps[`on${Capitalize<T>}`],
49+
): FocusHandler<T, TargetedEvent> | undefined {
50+
return (element) => handleEvent(element, onEvent);
51+
}
52+
4353
export function useFocusHandler(
4454
onFocus?: ViewProps['onFocus'],
4555
): FocusHandler<'focus', TargetedEvent> | undefined {
46-
return useHandler('focus', onFocus);
56+
return useHandler(onFocus);
4757
}
4858

4959
export function useBlurHandler(
5060
onBlur?: ViewProps['onBlur'],
5161
): FocusHandler<'blur', TargetedEvent> | undefined {
52-
return useHandler('blur', onBlur);
62+
return useHandler(onBlur);
5363
}

0 commit comments

Comments
 (0)