Skip to content

Commit 933a860

Browse files
authored
Support auto height layout for vertical carousel (#1294)
1 parent ab60fff commit 933a860

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

demo/src/screens/componentScreens/CarouselVerticalScreen.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const BACKGROUND_COLORS = [
2222
Colors.purple60
2323
];
2424

25-
const pageHeight = Constants.windowHeight / 2;
25+
const pageHeight = Constants.windowHeight / 3;
2626

2727
class CarouselVerticalScreen extends Component<{}, State> {
2828
carousel = React.createRef<typeof Carousel>();
@@ -101,6 +101,17 @@ class CarouselVerticalScreen extends Component<{}, State> {
101101
</Carousel>
102102
{this.renderAnimatedCounter()}
103103
</View>
104+
105+
<View centerH flex marginT-s3>
106+
<Text h3 marginB-s2>
107+
iOS Widgets Carousel
108+
</Text>
109+
<Carousel horizontal={false} containerStyle={styles.widgetsCarousel}>
110+
<View flex bg-blue30/>
111+
<View flex bg-orange30/>
112+
<View flex bg-green30/>
113+
</Carousel>
114+
</View>
104115
</View>
105116
);
106117
}
@@ -130,6 +141,13 @@ const styles = StyleSheet.create({
130141
position: 'absolute',
131142
top: 20,
132143
left: 20
144+
},
145+
widgetsCarousel: {
146+
height: 150,
147+
width: 300,
148+
backgroundColor: Colors.grey60,
149+
borderRadius: 24,
150+
overflow: 'hidden'
133151
}
134152
});
135153

src/components/carousel/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
234234
pageHeight
235235
});
236236

237-
this.setState({containerWidth, pageWidth, initialOffset});
237+
this.setState({containerWidth, pageWidth, pageHeight, initialOffset});
238238
};
239239

240240
shouldAllowAccessibilityLayout() {
@@ -327,7 +327,9 @@ class Carousel extends Component<CarouselProps, CarouselState> {
327327

328328
renderChild = (child: ReactNode, key: Key): JSX.Element | undefined => {
329329
if (child) {
330-
const paddingLeft = this.props.horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
330+
const {pageWidth, pageHeight} = this.state;
331+
const {horizontal} = this.props;
332+
const paddingLeft = horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
331333
const index = Number(key);
332334
const length = presenter.getChildrenLength(this.props);
333335
const containerMarginHorizontal = this.getContainerMarginHorizontal();
@@ -338,7 +340,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
338340
return (
339341
<View
340342
style={{
341-
width: this.state.pageWidth,
343+
width: pageWidth,
344+
height: !horizontal ? pageHeight : undefined,
342345
paddingLeft,
343346
marginLeft,
344347
marginRight,

0 commit comments

Comments
 (0)