-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswipeable.js
165 lines (145 loc) · 5.43 KB
/
swipeable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* React */
import React, { Component, Children } from 'react';
import { StyleSheet, View, LayoutAnimation } from 'react-native';
//+------------------------------------------------------------------+
//| Module Name: PannableMenu |
//| Module Purpose: make your animations cool |
//| Function: set components in the left and right view |
//+------------------------------------------------------------------+
/********************************************************************/
/* global values */
/********************************************************************/
const Dimensions = require('Dimensions');
const { width } = Dimensions.get('window');
const STR_LEFT = 'LeftView'
const STR_RIGHT = 'RightView'
const MAX_LEFT = width
const MIN_LEFT = -width
/********************************************************************/
/* SwipeableView */
/* props: isVisibleLeft -> true/false if the left is opened */
/********************************************************************/
export class SwipeableView extends Component{
constructor(props) {
super(props)
// 実際に表示するコンポーネント
this.LeftToRight = this.changeTheOrderLeftToRight(this.props)
this.state = {
rightPosition: this.props.isVisibleLeft ? MIN_LEFT : MAX_LEFT,
}
}
/*******左→右の順にしてchildren配列を返す*********/
changeTheOrderLeftToRight(props) {
const arrayComponent = React.Children.toArray(props.children)
let LeftToRight = new Array()
let errCheck = ''
// 全ての子コンポーネントを探索する
React.Children.forEach(this.props.children, (child) => {
let strType = child.type + ''
strType = strType.substring(9, strType.indexOf('(props)'))
// Leftを最初にする
if(STR_LEFT === strType) {
LeftToRight.push(arrayComponent[0])
errCheck += '1'
}
else if(STR_RIGHT === strType){
LeftToRight.push(arrayComponent[1])
errCheck += '10'
}
else {
errCheck += '01'
}
})
// エラーがあるかチェックする
if(('110' !== errCheck) &&
('101' !== errCheck)) {
if('1' === errCheck) {
throw new Error('you have to add RightView in SwipeableView, Error code:' + errCheck)
}
else if ('0' === errCheck) {
throw new Error('you have to add LeftView in SwipeableView. Error code:' + errCheck)
}
throw new Error('Something wrong with your configuration of SwipeableView. Error code:' + errCheck)
}
return LeftToRight
}
/**********propsが変更された場合************/
UNSAFE_componentWillReceiveProps(nextProps, nextState) {
// 子コンポーネントは必ず更新する
this.LeftToRight = this.changeTheOrderLeftToRight(nextProps)
// 比較して異なれば更新
if(nextProps.isVisibleLeft != this.props.isVisibleLeft) {
this.swipe(nextProps.isVisibleLeft)
}
}
/**************左へ移動***************/
swipe(isVisibleLeft) {
this.startLayoutAnimation()
this.setState({
rightPosition: isVisibleLeft ? MIN_LEFT : MAX_LEFT,
})
}
/***********アニメーション************/
startLayoutAnimation() {
LayoutAnimation.configureNext({
duration: 700,
create: {
type: LayoutAnimation.Types.spring,
property: LayoutAnimation.Properties.opacity,
springDamping: 1.0
},
update: {
type: LayoutAnimation.Types.spring,
springDamping: 0.9
}
});
}
/************レンダー*************/
render() {
return (
<View style={this.props.style}>
<View style={[styles.Container, {right: this.state.rightPosition / 2}]}>
{this.LeftToRight[0]}
{this.LeftToRight[1]}
</View>
</View>
)
}
}
const styles = StyleSheet.create({
Container: {
flexDirection: 'row',
width: width * 2,
flex: 1,
},
})
/********************************************************************/
/* LeftView */
/********************************************************************/
export class LeftView extends Component {
constructor(props) {
super(props)
}
render() {
return (
<View style={[this.props.style, {flex: 1}]}>
{this.props.children}
</View>
)
}
}
/********************************************************************/
/* RightView */
/********************************************************************/
export class RightView extends Component {
constructor(props) {
super(props)
}
render() {
return (
<View style={[this.props.style, {flex: 1}]}>
{this.props.children}
</View>
)
}
}