dbb-react-native-invertible-scroll-view
Version:
An invertible ScrollView for React Native
95 lines (80 loc) • 2.19 kB
JavaScript
;
import React, {
PropTypes,
} from 'react';
import {
ScrollView,
StyleSheet,
View,Platform
} from 'react-native';
import ScrollableMixin from 'react-native-scrollable-mixin';
import cloneReferencedElement from 'react-clone-referenced-element';
type DefaultProps = {
renderScrollComponent: (props: Object) => ReactElement;
};
let InvertibleScrollView = React.createClass({
mixins: [ScrollableMixin],
propTypes: {
...ScrollView.propTypes,
inverted: PropTypes.bool,
renderScrollComponent: PropTypes.func.isRequired,
},
getDefaultProps(): DefaultProps {
return {
renderScrollComponent: props => <ScrollView {...props} />,
};
},
getScrollResponder(): ReactComponent {
return this._scrollComponent.getScrollResponder();
},
setNativeProps(props: Object) {
this._scrollComponent.setNativeProps(props);
},
render() {
var {
inverted,
renderScrollComponent,
...props,
} = this.props;
if (inverted) {
if (this.props.horizontal) {
props.style = [styles.horizontallyInverted, props.style];
props.children = this._renderInvertedChildren(props.children, styles.horizontallyInverted);
} else {
props.style = [styles.verticallyInverted, props.style];
props.children = this._renderInvertedChildren(props.children, styles.verticallyInverted);
}
}
return cloneReferencedElement(renderScrollComponent(props), {
ref: component => { this._scrollComponent = component; },
});
},
_renderInvertedChildren(children, inversionStyle) {
return React.Children.map(children, child => {
return child ? <View style={inversionStyle}>{child}</View> : child;
});
},
});
let styles = StyleSheet.create({
verticallyInverted: {
flex:1,transform: Platform.select({
ios: [
{ scaleY: -1 },
],
android:[{perspective: 1},
{ scaleY: -1 },
]
})
},
horizontallyInverted: {
flex:1,transform: Platform.select({
ios: [
{ scaleX: -1 },
],
android:[{perspective: 1},
{ scaleX: -1 },
]
})
},
});
module.exports = InvertibleScrollView;