Skip to content

Commit 80ef32f

Browse files
nandorojonecolas
authored andcommitted
[fix] ScrollView support for 'centerContent' prop
Fix necolas#2331 Close necolas#2332
1 parent 511e553 commit 80ef32f

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

packages/react-native-web-docs/src/pages/docs/components/scoll-view.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ import { ScrollView } from 'react-native';
3131
All the props supported by [View]({{ '/docs/view' | url }}).
3232
{% endcall %}
3333

34+
{% call macro.prop('centerContent', '?boolean') %}
35+
When `true`, the scroll view automatically centers the content when the content is smaller than the scroll view bounds; when the content is larger than the scroll view, this property has no effect.
36+
{% endcall %}
37+
3438
{% call macro.prop('contentContainerStyle', '?Style') %}
3539
These styles will be applied to the scroll view content container which wraps all of the child views.
3640
{% endcall %}
3741

38-
3942
{% call macro.prop('disableScrollViewPanResponder', '?boolean = false') %}
4043
When `true`, the default `PanResponder` on the `ScrollView` is disabled, and full control over pointers inside the `ScrollView` is left to its child components. This is meant to be used when native "snap-to" scrolling behavior is needed.
4144
{% endcall %}

packages/react-native-web/src/exports/ScrollView/__tests__/__snapshots__/index-test.js.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`components/ScrollView prop "centerContent" with 1`] = `
4+
<div
5+
class="css-view-175oi2r r-WebkitOverflowScrolling-150rngu r-flexDirection-eqz5dr r-flexGrow-16y2uox r-flexShrink-1wbh5a2 r-overflowX-11yh6sk r-overflowY-1rnoaur r-transform-1sncvnh"
6+
style="background-color: rgb(0, 0, 255);"
7+
>
8+
<div
9+
class="css-view-175oi2r r-flexGrow-16y2uox r-justifyContent-1777fci"
10+
/>
11+
</div>
12+
`;
13+
14+
exports[`components/ScrollView prop "centerContent" without 1`] = `
15+
<div
16+
class="css-view-175oi2r r-WebkitOverflowScrolling-150rngu r-flexDirection-eqz5dr r-flexGrow-16y2uox r-flexShrink-1wbh5a2 r-overflowX-11yh6sk r-overflowY-1rnoaur r-transform-1sncvnh"
17+
style="background-color: rgb(0, 0, 255);"
18+
>
19+
<div
20+
class="css-view-175oi2r"
21+
/>
22+
</div>
23+
`;
24+
325
exports[`components/ScrollView prop "refreshControl" with 1`] = `
426
<div
527
id="refresh-control"

packages/react-native-web/src/exports/ScrollView/__tests__/index-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import { findDOMNode } from 'react-dom';
66
import { render } from '@testing-library/react';
77

88
describe('components/ScrollView', () => {
9+
describe('prop "centerContent"', () => {
10+
test('without', () => {
11+
const { container } = render(
12+
<ScrollView style={{ backgroundColor: 'blue' }} />
13+
);
14+
expect(container.firstChild).toMatchSnapshot();
15+
});
16+
17+
test('with', () => {
18+
const { container } = render(
19+
<ScrollView centerContent style={{ backgroundColor: 'blue' }} />
20+
);
21+
expect(container.firstChild).toMatchSnapshot();
22+
});
23+
});
24+
925
describe('prop "onScroll"', () => {
1026
test('is called when element scrolls', () => {
1127
const onScroll = jest.fn();

packages/react-native-web/src/exports/ScrollView/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import React from 'react';
2222

2323
type ScrollViewProps = {
2424
...ViewProps,
25+
centerContent?: boolean,
2526
contentContainerStyle?: ViewStyle,
2627
horizontal?: boolean,
2728
keyboardDismissMode?: 'none' | 'interactive' | 'on-drag',
@@ -136,6 +137,7 @@ const ScrollView = ((createReactClass({
136137
forwardedRef,
137138
keyboardDismissMode,
138139
onScroll,
140+
centerContent,
139141
/* eslint-enable */
140142
...other
141143
} = this.props;
@@ -189,10 +191,11 @@ const ScrollView = ((createReactClass({
189191
children={children}
190192
collapsable={false}
191193
ref={this._setInnerViewRef}
192-
style={StyleSheet.compose(
194+
style={[
193195
horizontal && styles.contentContainerHorizontal,
196+
centerContent && styles.contentContainerCenterContent,
194197
contentContainerStyle
195-
)}
198+
]}
196199
/>
197200
);
198201

@@ -329,6 +332,10 @@ const styles = StyleSheet.create({
329332
contentContainerHorizontal: {
330333
flexDirection: 'row'
331334
},
335+
contentContainerCenterContent: {
336+
justifyContent: 'center',
337+
flexGrow: 1
338+
},
332339
stickyHeader: {
333340
position: 'sticky',
334341
top: 0,

0 commit comments

Comments
 (0)