react-native-scroll-into-view
Version:
React-Native port of DOMElement.scrollIntoView() web function, for ScrollView
62 lines • 3.12 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React from 'react';
import { Animated, } from 'react-native';
import { normalizeOptions, normalizeHOCConfig, } from './config';
import { ProvideAPI } from './context';
export const wrapScrollViewHOC = (ScrollViewComp, config = {}) => {
const { refPropName, getScrollViewNode, scrollEventThrottle, options, } = normalizeHOCConfig(config);
class ScrollViewWrapper extends React.Component {
constructor(props) {
super(props);
this.handleRef = (ref) => {
// @ts-ignore
this.ref.current = ref;
if (this.props.innerRef) {
if (typeof this.props.innerRef.current !== 'undefined') {
this.props.innerRef.current = ref;
}
else {
this.props.innerRef(ref);
}
}
};
this.handleScroll = (event) => {
this.scrollY = event.nativeEvent.contentOffset.y;
};
this.getScrollY = () => this.scrollY;
this.getScrollView = () => getScrollViewNode(this.ref.current);
this.getDefaultOptions = () => normalizeOptions(this.props.scrollIntoViewOptions, options);
this.ref = React.createRef();
this.scrollY = this.props.contentOffset ? this.props.contentOffset.y : 0;
this.dependencies = {
getScrollView: this.getScrollView,
getScrollY: this.getScrollY,
getDefaultOptions: this.getDefaultOptions,
};
}
render() {
const _a = this.props, { children } = _a, props = __rest(_a, ["children"]);
const scrollViewProps = Object.assign(Object.assign({}, props), { [refPropName]: this.handleRef, scrollEventThrottle: this.props.scrollEventThrottle || scrollEventThrottle,
// See https://github.com/facebook/react-native/issues/19623
// @ts-ignore // TODO not yet in typedefs
onScroll: Animated.forkEvent(this.props.onScroll, this.handleScroll) });
return (React.createElement(ScrollViewComp, Object.assign({}, scrollViewProps),
React.createElement(ProvideAPI, { dependencies: this.dependencies }, children)));
}
}
ScrollViewWrapper.displayName = `ScrollIntoViewWrapper(${ScrollViewComp.displayName ||
ScrollViewComp.name ||
'Component'})`;
return React.forwardRef((props, ref) => (React.createElement(ScrollViewWrapper, Object.assign({ innerRef: ref }, props))));
};
//# sourceMappingURL=hoc.js.map