@shoutem/ui 8.2.0 → 8.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/TabMenu/TabMenu.js +56 -31
- package/package.json +1 -1
- package/theme.js +1 -0
|
@@ -11,70 +11,76 @@ class TabMenu extends PureComponent {
|
|
|
11
11
|
constructor(props) {
|
|
12
12
|
super(props);
|
|
13
13
|
|
|
14
|
+
this.scroll = {};
|
|
15
|
+
|
|
14
16
|
autoBindReact(this);
|
|
15
17
|
|
|
16
18
|
this.state = {
|
|
17
19
|
itemWidths: {},
|
|
18
|
-
|
|
20
|
+
viewportHorizontal: [],
|
|
19
21
|
};
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
componentDidMount() {
|
|
25
|
+
this.scrollToSelectedItem();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
componentDidUpdate(prevProps) {
|
|
29
|
+
const { selectedOption } = this.props;
|
|
30
|
+
|
|
31
|
+
if (prevProps.selectedOption !== selectedOption) {
|
|
32
|
+
this.scrollToSelectedItem();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
scrollToSelectedItem() {
|
|
37
|
+
const { selectedOption, options } = this.props;
|
|
38
|
+
const { itemWidths, viewportHorizontal } = this.state;
|
|
39
|
+
|
|
40
|
+
if (!selectedOption || !options.length || !this.scroll) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const selectedIndex = options.findIndex(
|
|
45
|
+
option => option.id === selectedOption.id,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (selectedIndex === -1) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
25
51
|
|
|
26
52
|
let itemStart = 0;
|
|
27
53
|
|
|
28
54
|
_.forEach(itemWidths, (itemWidth, index) => {
|
|
29
|
-
if (index <
|
|
55
|
+
if (index < selectedIndex) {
|
|
30
56
|
itemStart += itemWidth;
|
|
31
57
|
}
|
|
32
58
|
});
|
|
33
59
|
|
|
34
|
-
const itemEnd = itemStart + itemWidths[
|
|
35
|
-
|
|
36
|
-
LayoutAnimation.easeInEaseOut();
|
|
37
|
-
onOptionSelected(option);
|
|
60
|
+
const itemEnd = itemStart + itemWidths[selectedIndex];
|
|
38
61
|
|
|
62
|
+
// Ensure item is in view
|
|
39
63
|
if (itemEnd > _.last(viewportHorizontal)) {
|
|
40
64
|
const extraScroll = itemEnd - _.last(viewportHorizontal);
|
|
41
|
-
|
|
42
|
-
this.scroll.scrollTo({
|
|
43
|
-
x: extraScroll + scrollOffset,
|
|
44
|
-
y: 0,
|
|
45
|
-
animated: true,
|
|
46
|
-
});
|
|
65
|
+
this.scroll.scrollTo({ x: extraScroll, y: 0, animated: true });
|
|
47
66
|
}
|
|
48
67
|
|
|
49
68
|
if (itemStart < _.head(viewportHorizontal)) {
|
|
50
|
-
const extraScroll = _.head(viewportHorizontal) - itemStart;
|
|
51
|
-
|
|
52
69
|
this.scroll.scrollTo({
|
|
53
|
-
x:
|
|
70
|
+
x: itemStart,
|
|
54
71
|
y: 0,
|
|
55
72
|
animated: true,
|
|
56
73
|
});
|
|
57
74
|
}
|
|
58
75
|
}
|
|
59
76
|
|
|
60
|
-
handleItemLayout(key, width) {
|
|
61
|
-
const { itemWidths } = this.state;
|
|
62
|
-
|
|
63
|
-
this.setState({
|
|
64
|
-
itemWidths: {
|
|
65
|
-
...itemWidths,
|
|
66
|
-
[key]: width,
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
77
|
handleScroll({
|
|
72
78
|
nativeEvent: {
|
|
73
79
|
contentOffset: { x },
|
|
74
80
|
layoutMeasurement: { width },
|
|
75
81
|
},
|
|
76
82
|
}) {
|
|
77
|
-
this.setState({
|
|
83
|
+
this.setState({ viewportHorizontal: [x, width] });
|
|
78
84
|
}
|
|
79
85
|
|
|
80
86
|
handleScrollLayout({
|
|
@@ -85,6 +91,24 @@ class TabMenu extends PureComponent {
|
|
|
85
91
|
this.setState({ viewportHorizontal: [0, width] });
|
|
86
92
|
}
|
|
87
93
|
|
|
94
|
+
handleItemLayout(key, width) {
|
|
95
|
+
const { itemWidths } = this.state;
|
|
96
|
+
|
|
97
|
+
this.setState({
|
|
98
|
+
itemWidths: {
|
|
99
|
+
...itemWidths,
|
|
100
|
+
[key]: width,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
handleOptionSelected(option) {
|
|
106
|
+
const { onOptionSelected } = this.props;
|
|
107
|
+
|
|
108
|
+
LayoutAnimation.easeInEaseOut();
|
|
109
|
+
onOptionSelected(option);
|
|
110
|
+
}
|
|
111
|
+
|
|
88
112
|
renderOption(option, key) {
|
|
89
113
|
const { selectedOption } = this.props;
|
|
90
114
|
|
|
@@ -95,7 +119,7 @@ class TabMenu extends PureComponent {
|
|
|
95
119
|
key={key}
|
|
96
120
|
isSelected={isSelected}
|
|
97
121
|
item={option}
|
|
98
|
-
onItemPressed={option => this.handleOptionSelected(
|
|
122
|
+
onItemPressed={option => this.handleOptionSelected(option)}
|
|
99
123
|
onLayoutMeasured={width => this.handleItemLayout(key, width)}
|
|
100
124
|
/>
|
|
101
125
|
);
|
|
@@ -113,6 +137,7 @@ class TabMenu extends PureComponent {
|
|
|
113
137
|
ref={ref => {
|
|
114
138
|
this.scroll = ref;
|
|
115
139
|
}}
|
|
140
|
+
scrollEventThrottle={16}
|
|
116
141
|
showsHorizontalScrollIndicator={false}
|
|
117
142
|
style={style.list}
|
|
118
143
|
>
|
package/package.json
CHANGED