-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
201 lines (171 loc) · 4.79 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { Component } from 'react';
import { View, ScrollView, Platform } from 'react-native';
import PropTypes from 'prop-types';
// Styles
import { Styles } from './styles';
// set to fit
const scalesPageToFit = Platform.OS === 'android';
const DEFAULT_PASSIVE_DOT_WIDTH = 10;
const DEFAULT_ACTIVE_DOT_WIDTH = 10;
export default class Dots extends Component {
static get propTypes() {
return {
length: PropTypes.number.isRequired,
active: PropTypes.number.isRequired,
width: PropTypes.number,
paddingVertical: PropTypes.number,
paddingHorizontal: PropTypes.number,
// Dots
passiveDotWidth: PropTypes.number,
activeDotWidth: PropTypes.number,
activeDotHeight: PropTypes.number,
passiveDotHeight: PropTypes.number,
passiveColor: PropTypes.string,
activeColor: PropTypes.string,
// active Border
activeBorder: PropTypes.bool,
activeBorderColor: PropTypes.string,
activeBorderWidth: PropTypes.number,
// events
onScrollTo: PropTypes.func,
// accessibility
accessible: PropTypes.bool,
accessibilityLabel: PropTypes.string,
};
}
static defaultProps = {
width: 300,
marginHorizontal: 2,
paddingVertical: 10,
paddingHorizontal: 10,
passiveDotWidth: DEFAULT_PASSIVE_DOT_WIDTH,
activeDotWidth: DEFAULT_ACTIVE_DOT_WIDTH,
activeDotHeight: DEFAULT_ACTIVE_DOT_WIDTH,
passiveDotHeight: DEFAULT_PASSIVE_DOT_WIDTH,
passiveColor: '#CCCCCC',
activeColor: '#016bd8',
activeBorder: false,
activeBorderWidth: 3,
activeBorderColor: '#FFF',
// events
onScrollTo() {
// this function on change index.
},
accessible: false,
accessibilityLabel: '',
};
componentDidUpdate(prevProps) {
const newActive = this.props.active;
if (prevProps.active !== newActive) {
this.scrollTo(newActive);
}
}
scrollTo(index) {
const { width, activeDotWidth, onScrollTo } = this.props;
const key = `dots_${index}`;
const get = this[key];
if (get) {
const x = get.x - (width / 2 - activeDotWidth);
this.scrollRef.scrollTo({ x });
// on change event
onScrollTo(index, key);
}
}
isActive(index) {
return this.props.active === index;
}
dotStyle(isActive) {
const {
activeDotWidth,
passiveDotWidth,
activeDotHeight,
passiveDotHeight,
activeColor,
passiveColor,
activeBorder,
activeBorderWidth,
activeBorderColor,
marginHorizontal,
alignDotsOnXAxis,
} = this.props;
const width = isActive ? activeDotWidth : passiveDotWidth;
const marginTop = alignDotsOnXAxis || !isActive ? 0 : -width / 6;
let height = width;
if (isActive && activeDotHeight != null) {
height = activeDotHeight;
} else if (!isActive && passiveDotHeight != null) {
height = passiveDotHeight;
}
let style = {
width,
height,
marginHorizontal,
backgroundColor: isActive ? activeColor : passiveColor,
borderRadius: width,
marginTop,
};
// active Border Styles.
if (activeBorder && isActive) {
style.borderWidth = activeBorderWidth;
style.borderColor = activeBorderColor;
}
return style;
}
render() {
const {
length,
width,
paddingVertical,
paddingHorizontal,
activeDotWidth,
activeBorderWidth,
passiveDotWidth,
marginHorizontal,
accessible,
accessibilityLabel,
} = this.props;
const list = [...Array(length).keys()];
const activeWidth =
activeBorderWidth * 4 + activeDotWidth + paddingHorizontal;
const scrollWidth =
activeWidth +
(list.length - 1) * passiveDotWidth +
marginHorizontal * (list.length * 2);
return (
<View style={Styles.container} accessible={accessible} accessibilityLabel={accessibilityLabel}>
<ScrollView
ref={(el) => {
this.scrollRef = el;
}}
style={{ width: width < scrollWidth ? width : scrollWidth }}
contentContainerStyle={{
alignItems: 'center',
paddingVertical,
paddingHorizontal,
}}
scalesPageToFit={scalesPageToFit}
bounces={false}
horizontal={true}
scrollEnabled={false}
showsHorizontalScrollIndicator={false}
>
{list.map((i) => {
return (
<View
key={i}
style={this.dotStyle(this.isActive(i))}
onLayout={({
nativeEvent: {
layout: { x, y },
},
}) => {
this[`dots_${i}`] = { x, y };
}}
/>
);
})}
</ScrollView>
</View>
);
}
}