From aec017589106118ac9bb2e7cb1f68ee401640303 Mon Sep 17 00:00:00 2001 From: Vanson Date: Sat, 9 Jul 2016 17:26:49 +0800 Subject: [PATCH] fix state update --- JellySideMenu.js | 131 ++++++++++++++++++++++++++++++++++++++--------- package.json | 2 +- 2 files changed, 109 insertions(+), 24 deletions(-) diff --git a/JellySideMenu.js b/JellySideMenu.js index 2fcb4df..c2dee18 100644 --- a/JellySideMenu.js +++ b/JellySideMenu.js @@ -50,6 +50,9 @@ class JellySideMenu extends Component { offsetDragY : height / 2, } this.makePanResponder(); + + this.onJellyUndocked = this.onJellyUndocked.bind(this); + this.onJellyNotUndocked = this.onJellyNotUndocked.bind(this); } makePanResponder() { @@ -89,7 +92,8 @@ class JellySideMenu extends Component { } onDragSideMenuSvg(x, y) { - this.sideMenuSvg.setOffsetDrag(x, y, false) + this.refs.sideMenuSvgWrapper ? this.refs.sideMenuSvgWrapper.onJellyNotUndocked() : {}; + this.refs.sideMenuSvg ? this.refs.sideMenuSvg.setOffsetDrag(x, y, false) : {}; } onDropSideMenuSvg(bool) { @@ -97,12 +101,13 @@ class JellySideMenu extends Component { this.setState({ is_dock: true }) - this.sideMenuSvg.dockOffsetDrag(true) + this.refs.sideMenuSvgWrapper ? this.refs.sideMenuSvgWrapper.onJellyNotUndocked() : {}; + this.refs.sideMenuSvg ? this.refs.sideMenuSvg.dockOffsetDrag(true) : {}; } else { this.setState({ is_dock: false }) - this.sideMenuSvg.resetOffsetDrag(true) + this.refs.sideMenuSvg ? this.refs.sideMenuSvg.resetOffsetDrag(true) : {}; } } @@ -115,6 +120,17 @@ class JellySideMenu extends Component { } + + onJellyNotUndocked() { + this.refs.sideMenuSvgWrapper ? this.refs.sideMenuSvgWrapper.onJellyNotUndocked() : {}; + } + + + onJellyUndocked() { + this.refs.sideMenuSvgWrapper ? this.refs.sideMenuSvgWrapper.onJellyUndocked() : {}; + } + + render() { var dockPullWidth = 20; var dockWidth = 240; @@ -132,13 +148,7 @@ class JellySideMenu extends Component { { - Platform.OS === "ios" ? ( - {this.sideMenuSvg = view}}/> - ) : ( - - {this.sideMenuSvg = view}}/> - - ) + this.renderSvg(dockWidth) } {this.props.renderMenu()} @@ -146,6 +156,72 @@ class JellySideMenu extends Component { ) } + + + renderSvg(dockWidth) { + if (Platform.OS === "ios") { + return ( + + + + ) + } + + return ( + + + + + + ) + + } +} + + +class JellySideMenuSvgWrapper extends Component { + constructor(props) { + super(props); + this.state = { + is_undocked: true, + } + } + + onJellyNotUndocked() { + this.setState({ + is_undocked: false + }) + } + + + onJellyUndocked() { + this.setState({ + is_undocked: true + }) + } + + + render() { + if (this.state.is_undocked) { + return null + } + + return ( + + {this.props.children} + + ) + } } @@ -190,6 +266,7 @@ class JellySideMenuSvg extends Component { offsetDragY : height / 2, offsetDragXSm : 0, } + this.isBusy = false this.springSystem = new rebound.SpringSystem() this.springSystem2 = new rebound.SpringSystem() @@ -198,6 +275,10 @@ class JellySideMenuSvg extends Component { this.ssOffsetDragX.setCurrentValue(0) this.ssOffsetDragY.setCurrentValue(height / 2) this.ssOffsetDragX.addListener({onSpringUpdate: () => { + if (this.isBusy) { + return + } + this.isBusy = true; if (this.ssOffsetDragX.getEndValue() <= 0) { if (this.state.offsetDragX <= 0 && !this.state.is_undocked) { this.setState({offsetDragX: this.ssOffsetDragX.getCurrentValue(), is_undocked: true}); @@ -236,6 +317,17 @@ class JellySideMenuSvg extends Component { } + componentWillUpdate(nextProps, nextState) { + if (nextState.is_undocked != this.state.is_undocked) { + if (nextState.is_undocked == true) { + this.props.onJellyUndocked(); + } else { + this.props.onJellyNotUndocked(); + } + } + } + + setOffsetDrag(x, y, animated) { if (animated) { this.ssOffsetDragX.setEndValue(x / 2); @@ -273,9 +365,7 @@ class JellySideMenuSvg extends Component { } render() { - if (this.state.is_undocked) { - return null - } + this.isBusy = false; var offsetDragX = this.state.offsetDragX; var offsetDragY = this.state.offsetDragY; @@ -289,19 +379,14 @@ class JellySideMenuSvg extends Component { path = pathSide + " L" + " 0 " + this.props.height + " L0 0 Z"; return ( - - - + ) } } + module.exports = JellySideMenu; \ No newline at end of file diff --git a/package.json b/package.json index cd4652b..af57b0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-jelly-side-menu", - "version": "0.0.4", + "version": "0.0.5", "description": "A side menu that animates like a jelly! iOS & Android tested.", "main": "JellySideMenu.js", "scripts": {