Skip to content

Commit 7c11144

Browse files
authored
Fix/ migrate callback refs to object (#1912)
* migrate callback refs to object * fix ref checks
1 parent 87267e9 commit 7c11144

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

generatedTypes/src/components/hint/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ declare class Hint extends Component<HintProps, HintState> {
127127
};
128128
static positions: typeof HintPositions;
129129
targetRef: ElementRef<typeof RNView> | null;
130-
hintRef: ElementRef<typeof RNView> | null;
130+
hintRef: React.RefObject<RNView>;
131131
animationDuration: number;
132132
state: {
133133
targetLayoutInWindow: {
@@ -140,12 +140,12 @@ declare class Hint extends Component<HintProps, HintState> {
140140
hintUnmounted: boolean;
141141
};
142142
visibleAnimated: Animated.Value;
143+
componentDidMount(): void;
143144
componentDidUpdate(prevProps: HintProps): void;
144145
animateHint: () => void;
145146
toggleAnimationEndedToRemoveHint: () => void;
146147
focusAccessibilityOnHint: () => void;
147148
setTargetRef: (ref: ElementRef<typeof RNView>) => void;
148-
setHintRef: (ref: ElementRef<typeof RNView>) => void;
149149
onTargetLayout: ({ nativeEvent: { layout } }: LayoutChangeEvent) => void;
150150
getAccessibilityInfo(): {
151151
accessible: boolean;

generatedTypes/src/components/slider/context/asSliderGroupChild.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { View } from 'react-native';
33
declare function asSliderGroupChild(WrappedComponent: any): {
44
new (props: {} | Readonly<{}>): {
5-
contentRef: View | undefined;
5+
contentRef: React.RefObject<View>;
66
render(): JSX.Element;
77
context: any;
88
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<{}>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
@@ -28,7 +28,7 @@ declare function asSliderGroupChild(WrappedComponent: any): {
2828
UNSAFE_componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
2929
};
3030
new (props: {}, context: any): {
31-
contentRef: View | undefined;
31+
contentRef: React.RefObject<View>;
3232
render(): JSX.Element;
3333
context: any;
3434
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<{}>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;

generatedTypes/src/components/slider/index.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PureComponent, ReactElement, ElementRef } from 'react';
2-
import { Animated, StyleProp, ViewStyle, PanResponderGestureState, GestureResponderEvent, LayoutChangeEvent, AccessibilityActionEvent, AccessibilityRole, View as RNView, ViewProps } from 'react-native';
1+
import { PureComponent, ReactElement } from 'react';
2+
import { Animated, StyleProp, ViewStyle, PanResponderGestureState, GestureResponderEvent, LayoutChangeEvent, AccessibilityActionEvent, AccessibilityRole, ViewProps } from 'react-native';
33
export declare type SliderOnValueChange = (value: number) => void;
44
export declare type SliderProps = {
55
/**
@@ -176,8 +176,6 @@ export default class Slider extends PureComponent<SliderProps, State> {
176176
getXForValue(v: number): number;
177177
getValueForX(x: number): number;
178178
getRange(): number;
179-
setMinTrackRef: (ref: ElementRef<typeof RNView>) => void;
180-
setThumbRef: (ref: ElementRef<typeof RNView>) => void;
181179
calculatedThumbActiveScale: () => number;
182180
updateTrackStepAndStyle: ({ nativeEvent }: GestureResponderEvent) => void;
183181
onOrientationChanged: () => void;

src/components/hint/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Hint extends Component<HintProps, HintState> {
169169
static positions = HintPositions;
170170

171171
targetRef: ElementRef<typeof RNView> | null = null;
172-
hintRef: ElementRef<typeof RNView> | null = null;
172+
hintRef = React.createRef<RNView>();
173173
animationDuration = 170;
174174

175175
state = {
@@ -180,6 +180,10 @@ class Hint extends Component<HintProps, HintState> {
180180

181181
visibleAnimated = new Animated.Value(Number(!!this.props.visible));
182182

183+
componentDidMount() {
184+
this.focusAccessibilityOnHint();
185+
}
186+
183187
componentDidUpdate(prevProps: HintProps) {
184188
if (prevProps.visible !== this.props.visible) {
185189
this.animateHint();
@@ -201,7 +205,7 @@ class Hint extends Component<HintProps, HintState> {
201205
focusAccessibilityOnHint = () => {
202206
const {message} = this.props;
203207
const targetRefTag = findNodeHandle(this.targetRef);
204-
const hintRefTag = findNodeHandle(this.hintRef);
208+
const hintRefTag = findNodeHandle(this.hintRef.current);
205209

206210
if (targetRefTag && _.isString(message)) {
207211
AccessibilityInfo.setAccessibilityFocus(targetRefTag);
@@ -215,11 +219,6 @@ class Hint extends Component<HintProps, HintState> {
215219
this.focusAccessibilityOnHint();
216220
};
217221

218-
setHintRef = (ref: ElementRef<typeof RNView>) => {
219-
this.hintRef = ref;
220-
this.focusAccessibilityOnHint();
221-
};
222-
223222
onTargetLayout = ({nativeEvent: {layout}}: LayoutChangeEvent) => {
224223
if (!_.isEqual(this.state.targetLayout, layout)) {
225224
this.setState({targetLayout: layout});
@@ -472,7 +471,7 @@ class Hint extends Component<HintProps, HintState> {
472471
{backgroundColor: color},
473472
!_.isUndefined(borderRadius) && {borderRadius}
474473
]}
475-
ref={this.setHintRef}
474+
ref={this.hintRef}
476475
>
477476
{customContent}
478477
{!customContent && icon && <Image source={icon} style={[styles.icon, iconStyle]}/>}

src/components/slider/context/asSliderGroupChild.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import React, {Component, ElementRef} from 'react';
1+
import React, {Component} from 'react';
22
import {View} from 'react-native';
33
import hoistNonReactStatic from 'hoist-non-react-statics';
44
import SliderContext from './SliderContext';
55

66

77
const getConsumer = (WrappedComponent: React.ComponentClass<any>) => (
88
class SliderContextConsumer extends Component {
9-
contentRef: ElementRef<typeof View> | undefined;
9+
contentRef = React.createRef<View>();
1010

1111
render() {
1212
return (
1313
<SliderContext.Consumer>
1414
{(context) => (
1515
<WrappedComponent
16-
ref={(r: ElementRef<typeof View>) => (this.contentRef = r)}
16+
ref={this.contentRef}
1717
sliderContext={context}
1818
{...this.props}
1919
/>

src/components/slider/index.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash';
2-
import React, {PureComponent, ReactElement, ElementRef} from 'react';
2+
import React, {PureComponent, ReactElement} from 'react';
33
import {
44
StyleSheet,
55
PanResponder,
@@ -155,9 +155,9 @@ export default class Slider extends PureComponent<SliderProps, State> {
155155

156156
static defaultProps = defaultProps;
157157

158-
private thumb: ElementRef<typeof RNView> | undefined = undefined;
158+
private thumb = React.createRef<RNView>();
159159
private _thumbStyles: ThumbStyle = {};
160-
private minTrack: ElementRef<typeof RNView> | undefined = undefined;
160+
private minTrack = React.createRef<RNView>();
161161
private _minTrackStyles: MinTrackStyle = {};
162162
private _x = 0;
163163
private _dx = 0;
@@ -299,18 +299,18 @@ export default class Slider extends PureComponent<SliderProps, State> {
299299
}
300300

301301
updateStyles(x: number) {
302-
if (this.thumb) {
302+
if (this.thumb.current) {
303303
const {disableRTL} = this.props;
304304
const {trackSize} = this.state;
305305
const nonOverlappingTrackWidth = trackSize.width - this.initialThumbSize.width;
306306
const _x = Constants.isRTL && disableRTL ? nonOverlappingTrackWidth - x : x; // adjust for RTL
307307
this._thumbStyles.left = trackSize.width === 0 ? _x : (_x * nonOverlappingTrackWidth) / trackSize.width; // do not render above prefix\suffix icon\text
308-
this.thumb.setNativeProps(this._thumbStyles);
308+
this.thumb.current?.setNativeProps(this._thumbStyles);
309309
}
310310

311-
if (this.minTrack) {
311+
if (this.minTrack.current) {
312312
this._minTrackStyles.width = Math.min(this.state.trackSize.width, x);
313-
this.minTrack.setNativeProps(this._minTrackStyles);
313+
this.minTrack.current?.setNativeProps(this._minTrackStyles);
314314
}
315315
}
316316

@@ -320,14 +320,14 @@ export default class Slider extends PureComponent<SliderProps, State> {
320320
}
321321

322322
updateThumbStyle(start: boolean) {
323-
if (this.thumb && !this.props.disableActiveStyling) {
323+
if (this.thumb.current && !this.props.disableActiveStyling) {
324324
const {thumbStyle, activeThumbStyle} = this.props;
325325
const style = thumbStyle || styles.thumb;
326326
const activeStyle = activeThumbStyle || styles.activeThumb;
327327

328328
const activeOrInactiveStyle = !this.props.disabled ? (start ? activeStyle : style) : {};
329329
this._thumbStyles.style = _.omit(activeOrInactiveStyle, 'height', 'width');
330-
this.thumb.setNativeProps(this._thumbStyles);
330+
this.thumb.current?.setNativeProps(this._thumbStyles);
331331
this.scaleThumb(start);
332332
}
333333
}
@@ -387,13 +387,6 @@ export default class Slider extends PureComponent<SliderProps, State> {
387387
return range;
388388
}
389389

390-
setMinTrackRef = (ref: ElementRef<typeof RNView>) => {
391-
this.minTrack = ref;
392-
};
393-
394-
setThumbRef = (ref: ElementRef<typeof RNView>) => {
395-
this.thumb = ref;
396-
};
397390

398391
calculatedThumbActiveScale = () => {
399392
const {activeThumbStyle, thumbStyle, disabled, disableActiveStyling} = this.props;
@@ -518,7 +511,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
518511
return (
519512
<Animated.View
520513
hitSlop={thumbHitSlop}
521-
ref={this.setThumbRef}
514+
ref={this.thumb}
522515
onLayout={this.onThumbLayout}
523516
{...this._panResponder.panHandlers}
524517
style={[
@@ -579,7 +572,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
579572
onLayout={this.onTrackLayout}
580573
/>
581574
<View
582-
ref={this.setMinTrackRef}
575+
ref={this.minTrack}
583576
style={[
584577
styles.track,
585578
trackStyle,

0 commit comments

Comments
 (0)