Skip to content

Commit 9240746

Browse files
committed
Fix/ migrate callback refs to object (#1912)
* migrate callback refs to object * fix ref checks
1 parent 0ce5f69 commit 9240746

File tree

6 files changed

+30
-49
lines changed

6 files changed

+30
-49
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});
@@ -476,7 +475,7 @@ class Hint extends Component<HintProps, HintState> {
476475
{backgroundColor: color},
477476
!_.isUndefined(borderRadius) && {borderRadius}
478477
]}
479-
ref={this.setHintRef}
478+
ref={this.hintRef}
480479
>
481480
{customContent}
482481
{!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: 14 additions & 30 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,27 +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;
305-
const _x = Constants.isRTL && disableRTL ? trackSize.width - x : x;
306-
const position = _x - this.initialThumbSize.width / 2;
307-
const deviation = 3;
308-
309-
if (position + deviation < 0) {
310-
this._thumbStyles.left = 0;
311-
} else if (position - deviation > trackSize.width - this.initialThumbSize.width) {
312-
this._thumbStyles.left = trackSize.width - this.initialThumbSize.width;
313-
} else {
314-
this._thumbStyles.left = position;
315-
}
316-
317-
this.thumb.setNativeProps(this._thumbStyles);
305+
const nonOverlappingTrackWidth = trackSize.width - this.initialThumbSize.width;
306+
const _x = Constants.isRTL && disableRTL ? nonOverlappingTrackWidth - x : x; // adjust for RTL
307+
this._thumbStyles.left = trackSize.width === 0 ? _x : (_x * nonOverlappingTrackWidth) / trackSize.width; // do not render above prefix\suffix icon\text
308+
this.thumb.current?.setNativeProps(this._thumbStyles);
318309
}
319310

320-
if (this.minTrack) {
311+
if (this.minTrack.current) {
321312
this._minTrackStyles.width = Math.min(this.state.trackSize.width, x);
322-
this.minTrack.setNativeProps(this._minTrackStyles);
313+
this.minTrack.current?.setNativeProps(this._minTrackStyles);
323314
}
324315
}
325316

@@ -329,14 +320,14 @@ export default class Slider extends PureComponent<SliderProps, State> {
329320
}
330321

331322
updateThumbStyle(start: boolean) {
332-
if (this.thumb && !this.props.disableActiveStyling) {
323+
if (this.thumb.current && !this.props.disableActiveStyling) {
333324
const {thumbStyle, activeThumbStyle} = this.props;
334325
const style = thumbStyle || styles.thumb;
335326
const activeStyle = activeThumbStyle || styles.activeThumb;
336327

337328
const activeOrInactiveStyle = !this.props.disabled ? (start ? activeStyle : style) : {};
338329
this._thumbStyles.style = _.omit(activeOrInactiveStyle, 'height', 'width');
339-
this.thumb.setNativeProps(this._thumbStyles);
330+
this.thumb.current?.setNativeProps(this._thumbStyles);
340331
this.scaleThumb(start);
341332
}
342333
}
@@ -396,13 +387,6 @@ export default class Slider extends PureComponent<SliderProps, State> {
396387
return range;
397388
}
398389

399-
setMinTrackRef = (ref: ElementRef<typeof RNView>) => {
400-
this.minTrack = ref;
401-
};
402-
403-
setThumbRef = (ref: ElementRef<typeof RNView>) => {
404-
this.thumb = ref;
405-
};
406390

407391
calculatedThumbActiveScale = () => {
408392
const {activeThumbStyle, thumbStyle, disabled, disableActiveStyling} = this.props;
@@ -527,7 +511,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
527511
return (
528512
<Animated.View
529513
hitSlop={thumbHitSlop}
530-
ref={this.setThumbRef}
514+
ref={this.thumb}
531515
onLayout={this.onThumbLayout}
532516
{...this._panResponder.panHandlers}
533517
style={[
@@ -588,7 +572,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
588572
onLayout={this.onTrackLayout}
589573
/>
590574
<View
591-
ref={this.setMinTrackRef}
575+
ref={this.minTrack}
592576
style={[
593577
styles.track,
594578
trackStyle,

0 commit comments

Comments
 (0)