Skip to content

Commit 2a834de

Browse files
jcurtisgitim
authored andcommitted
Add renderHeader which acts the same as renderFooter but at the top (#91)
* Add renderHeader which acts the same as renderFooter but at the top * Keep trailing comma * Revert undefined check fix for separate PR
1 parent 7d995f5 commit 2a834de

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ A RefreshControl that works the same way as a ScrollView's refreshControl.
4141
- **renderRow** (function)<br />
4242
`({key, index, data, disabled, active}) => renderable`<br />
4343
Takes a row key, row index, data entry from the data source and its statuses disabled, active and should return a renderable component to be rendered as the row. The child component will receive a method called `toggleRowActive` (only if `manuallyActivateRows={true}`) to manually activate the row. Useful if you have multiple touch responders in your view.<br />
44+
- **renderHeader?** (function)<br />
45+
`() => renderable`<br />
46+
Renders returned component at the top of the list.
4447
- **renderFooter?** (function)<br />
4548
`() => renderable`<br />
4649
Renders returned component at the bottom of the list.

src/SortableList.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class SortableList extends Component {
2929
manuallyActivateRows: PropTypes.bool,
3030

3131
renderRow: PropTypes.func.isRequired,
32+
renderHeader: PropTypes.func,
3233
renderFooter: PropTypes.func,
3334

3435
onChangeOrder: PropTypes.func,
@@ -76,6 +77,11 @@ export default class SortableList extends Component {
7677
});
7778
});
7879

80+
if (this.props.renderHeader && !this.props.horizontal) {
81+
this._headerLayout = new Promise((resolve) => {
82+
this._resolveHeaderLayout = resolve;
83+
});
84+
}
7985
if (this.props.renderFooter && !this.props.horizontal) {
8086
this._footerLayout = new Promise((resolve) => {
8187
this._resolveFooterLayout = resolve;
@@ -200,6 +206,7 @@ export default class SortableList extends Component {
200206
scrollEventThrottle={2}
201207
scrollEnabled={scrollEnabled}
202208
onScroll={this._onScroll}>
209+
{this._renderHeader()}
203210
<View style={innerContainerStyle}>
204211
{this._renderRows()}
205212
</View>
@@ -266,6 +273,20 @@ export default class SortableList extends Component {
266273
});
267274
}
268275

276+
_renderHeader() {
277+
if (!this.props.renderHeader || this.props.horizontal) {
278+
return null;
279+
}
280+
281+
const {headerLayout} = this.state;
282+
283+
return (
284+
<View onLayout={!headerLayout ? this._onLayoutHeader : null}>
285+
{this.props.renderHeader()}
286+
</View>
287+
);
288+
}
289+
269290
_renderFooter() {
270291
if (!this.props.renderFooter || this.props.horizontal) {
271292
return null;
@@ -281,8 +302,8 @@ export default class SortableList extends Component {
281302
}
282303

283304
_onUpdateLayouts() {
284-
Promise.all([this._footerLayout, ...Object.values(this._rowsLayouts)])
285-
.then(([footerLayout, ...rowsLayouts]) => {
305+
Promise.all([this._headerLayout, this._footerLayout, ...Object.values(this._rowsLayouts)])
306+
.then(([headerLayout, footerLayout, ...rowsLayouts]) => {
286307
// Can get correct container’s layout only after rows’s layouts.
287308
this._container.measure((x, y, width, height, pageX, pageY) => {
288309
const rowsLayoutsByKey = {};
@@ -298,6 +319,7 @@ export default class SortableList extends Component {
298319
this.setState({
299320
containerLayout: {x, y, width, height, pageX, pageY},
300321
rowsLayouts: rowsLayoutsByKey,
322+
headerLayout,
301323
footerLayout,
302324
contentHeight,
303325
contentWidth,
@@ -524,6 +546,10 @@ export default class SortableList extends Component {
524546
this._resolveRowLayout[rowKey]({rowKey, layout});
525547
}
526548

549+
_onLayoutHeader = ({nativeEvent: {layout}}) => {
550+
this._resolveHeaderLayout(layout);
551+
};
552+
527553
_onLayoutFooter = ({nativeEvent: {layout}}) => {
528554
this._resolveFooterLayout(layout);
529555
};

0 commit comments

Comments
 (0)