diff --git a/README.md b/README.md index 9e70771a..d0c9d941 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ import Swiper from 'react-native-swiper'; var styles = StyleSheet.create({ wrapper: { }, + content: { + }, slide1: { flex: 1, justifyContent: 'center', @@ -149,7 +151,7 @@ var styles = StyleSheet.create({ var swiper = React.createClass({ render: function() { return ( - + Hello Swiper @@ -185,7 +187,8 @@ AppRegistry.registerComponent('myproject', () => swiper); | :------------ |:---------------:| :---------------:| :-----| | width | - | `number` | If no specify default enable fullscreen mode by `flex: 1`. | | height | - | `number` | If no specify default fullscreen mode by `flex: 1`. | -| style | {...} | `style` | See default style in source. | +| style | {...} | `style` | Customize the style of the View container. | +| contentStyle | {...} | `style` | Customize the style of the ScrollView that has the content. | | loadMinimal | false | `bool` | Only load current index slide , `loadMinimalSize` slides before and after. | | loadMinimalSize | 1 | `number` | see `loadMinimal` | | loadMinimalLoader | `` | `element` | Custom loader to display when slides aren't loaded @@ -225,7 +228,6 @@ AppRegistry.registerComponent('myproject', () => swiper); | Prop | Default | Type | Description | | :------------ |:---------------:| :---------------:| :-----| -| style | {...} | `style` | Custom styles will merge with the default styles. | | title | {...} | `element` | If this parameter is not specified, will not render the title. | #### Basic props of `` diff --git a/src/index.js b/src/index.js index d1da66f7..1696d66f 100644 --- a/src/index.js +++ b/src/index.js @@ -8,15 +8,12 @@ import { Text, View, ScrollView, - Dimensions, TouchableOpacity, ViewPagerAndroid, Platform, ActivityIndicator } from 'react-native' -const { width, height } = Dimensions.get('window') - /** * Default styles * @type {StyleSheetPropType} @@ -24,7 +21,8 @@ const { width, height } = Dimensions.get('window') const styles = { container: { backgroundColor: 'transparent', - position: 'relative' + position: 'relative', + flex: 1 }, wrapper: { @@ -102,6 +100,7 @@ export default class extends Component { horizontal: PropTypes.bool, children: PropTypes.node.isRequired, style: ViewPropTypes.style, + contentStyle: ViewPropTypes.style, pagingEnabled: PropTypes.bool, showsHorizontalScrollIndicator: PropTypes.bool, showsVerticalScrollIndicator: PropTypes.bool, @@ -155,7 +154,7 @@ export default class extends Component { * Init states * @return {object} states */ - state = this.initState(this.props, true) + state = this.initState(this.props) /** * autoplay timer @@ -165,10 +164,8 @@ export default class extends Component { loopJumpTimer = null componentWillReceiveProps (nextProps) { - const sizeChanged = (nextProps.width || width) !== this.state.width || - (nextProps.height || height) !== this.state.height if (!nextProps.autoplay && this.autoplayTimer) clearTimeout(this.autoplayTimer) - this.setState(this.initState(nextProps, sizeChanged)) + this.setState(this.initState(nextProps)) } componentDidMount () { @@ -180,7 +177,7 @@ export default class extends Component { this.loopJumpTimer && clearTimeout(this.loopJumpTimer) } - initState (props, setOffsetInState) { + initState (props) { // set the current state const state = this.state || {} @@ -199,32 +196,11 @@ export default class extends Component { // retain the index initState.index = state.index } else { - // reset the index - setOffsetInState = true // if the index is reset, go ahead and update the offset in state initState.index = initState.total > 1 ? Math.min(props.index, initState.total - 1) : 0 } // Default: horizontal initState.dir = props.horizontal === false ? 'y' : 'x' - initState.width = props.width || width - initState.height = props.height || height - newInternals.offset = {} - - if (initState.total > 1) { - let setup = initState.index - if (props.loop) { - setup++ - } - newInternals.offset[initState.dir] = initState.dir === 'y' - ? initState.height * setup - : initState.width * setup - } - - // only update the offset in state if needed, updating offset while swiping - // causes some bad jumping / stuttering - if (setOffsetInState) { - initState.offset = newInternals.offset - } this.internals = newInternals return initState @@ -235,6 +211,29 @@ export default class extends Component { return Object.assign({}, this.state, this.internals) } + setDimensions = (event) => { + const { width, height } = event.nativeEvent.layout + const offset = this.internals.offset = {} + const state = { width, height } + + if (this.state.total > 1) { + let setup = this.state.index + if (this.props.loop) { + setup++ + } + offset[this.state.dir] = this.state.dir === 'y' + ? height * setup + : width * setup + } + + // only update the offset in state if needed, updating offset while swiping + // causes some bad jumping / stuttering + if (width !== this.state.width || height !== this.state.height) { + state.offset = offset + } + this.setState(state) + } + loopJump = () => { if (!this.state.loopJump) return const i = this.state.index + (this.props.loop ? 1 : 0) @@ -551,7 +550,7 @@ export default class extends Component { + {this.renderScrollView(pages)} {props.showsPagination && (props.renderPagination ? this.props.renderPagination(state.index, state.total, this)