From a2675ced4efe0df7745bf38908efa41d4d7a9841 Mon Sep 17 00:00:00 2001 From: Michael Ficaro Date: Thu, 12 Jul 2018 13:36:50 -0700 Subject: [PATCH] Adding new styling props to FlatList/VirtualizedList for ListHeaderComponent and ListFooterComponent Summary: We ran into a problem trying to style the optional prop `ListHeaderComponent` in the `FlatList` library component. Essentially we wanted to make `ListHeaderComponent` a flex item that filled all of the empty space in the list if there was any. Unfortunately the `ListHeaderComponent` is later wrapped in a `View` that blocked our styling. The `View` component was necessary as it added styling to handle inverting the `FlatList`. Similarly `ListFooterComponent` was handled the same way. We came up the simple solution of adding two new optional props, `ListHeaderComponentStyle` and `ListFooterComponentStyle`, that are of type `ViewStyleProp` that allow users to pass in styling for `ListHeaderComponent` and `ListFooterComponent`. With this change we were able to do something like the following to get the header component to fill all empty space in the `FlatList`. ``` } ListHeaderComponentStyle={{flexGrow: 1}} ... /> ``` This solution will give users a lot more freedom when working with headers and footers. Reviewed By: sahrens Differential Revision: D8777038 fbshipit-source-id: f34116ce68548ea70223e639d0f84a099327f6b3 --- Libraries/Lists/FlatList.js | 10 +++++++- Libraries/Lists/VirtualizedList.js | 24 ++++++++++++++++--- .../__snapshots__/FlatList-test.js.snap | 2 -- .../__snapshots__/SectionList-test.js.snap | 2 -- .../VirtualizedList-test.js.snap | 2 -- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Libraries/Lists/FlatList.js b/Libraries/Lists/FlatList.js index cfa4ae858..adebd267f 100644 --- a/Libraries/Lists/FlatList.js +++ b/Libraries/Lists/FlatList.js @@ -18,7 +18,7 @@ const StyleSheet = require('StyleSheet'); const invariant = require('fbjs/lib/invariant'); -import type {DangerouslyImpreciseStyleProp} from 'StyleSheet'; +import type {DangerouslyImpreciseStyleProp, ViewStyleProp} from 'StyleSheet'; import type { ViewabilityConfig, ViewToken, @@ -88,11 +88,19 @@ type OptionalProps = { * a rendered element. */ ListFooterComponent?: ?(React.ComponentType | React.Element), + /** + * Styling for internal View for ListFooterComponent + */ + ListFooterComponentStyle?: ViewStyleProp, /** * Rendered at the top of all the items. Can be a React Component Class, a render function, or * a rendered element. */ ListHeaderComponent?: ?(React.ComponentType | React.Element), + /** + * Styling for internal View for ListHeaderComponent + */ + ListHeaderComponentStyle?: ViewStyleProp, /** * Optional custom style for multi-item rows generated when numColumns > 1. */ diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 56364823f..ba9224c46 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -31,7 +31,7 @@ const warning = require('fbjs/lib/warning'); const {computeWindowedRenderLimits} = require('VirtualizeUtils'); -import type {DangerouslyImpreciseStyleProp} from 'StyleSheet'; +import type {DangerouslyImpreciseStyleProp, ViewStyleProp} from 'StyleSheet'; import type { ViewabilityConfig, ViewToken, @@ -124,11 +124,19 @@ type OptionalProps = { * a rendered element. */ ListFooterComponent?: ?(React.ComponentType | React.Element), + /** + * Styling for internal View for ListFooterComponent + */ + ListFooterComponentStyle?: ViewStyleProp, /** * Rendered at the top of all the items. Can be a React Component Class, a render function, or * a rendered element. */ ListHeaderComponent?: ?(React.ComponentType | React.Element), + /** + * Styling for internal View for ListHeaderComponent + */ + ListHeaderComponentStyle?: ViewStyleProp, /** * A unique identifier for this list. If there are multiple VirtualizedLists at the same level of * nesting within another VirtualizedList, this key is necessary for virtualization to @@ -756,7 +764,12 @@ class VirtualizedList extends React.PureComponent { - + { // $FlowFixMe - Typing ReactNativeComponent revealed errors element @@ -892,7 +905,12 @@ class VirtualizedList extends React.PureComponent { - + { // $FlowFixMe - Typing ReactNativeComponent revealed errors element diff --git a/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap b/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap index d6d008065..7f523be29 100644 --- a/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap +++ b/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap @@ -60,7 +60,6 @@ exports[`FlatList renders all the bells and whistles 1`] = `
@@ -119,7 +118,6 @@ exports[`FlatList renders all the bells and whistles 1`] = `