Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ A RefreshControl that works the same way as a ScrollView's refreshControl.
- **renderRow** (function)<br />
`({key, index, data, disabled, active}) => renderable`<br />
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 />
- **renderHeader?** (function)<br />
`() => renderable`<br />
Renders returned component at the top of the list.
- **renderFooter?** (function)<br />
`() => renderable`<br />
Renders returned component at the bottom of the list.
Expand Down
30 changes: 28 additions & 2 deletions src/SortableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class SortableList extends Component {
manuallyActivateRows: PropTypes.bool,

renderRow: PropTypes.func.isRequired,
renderHeader: PropTypes.func,
renderFooter: PropTypes.func,

onChangeOrder: PropTypes.func,
Expand Down Expand Up @@ -76,6 +77,11 @@ export default class SortableList extends Component {
});
});

if (this.props.renderHeader && !this.props.horizontal) {
this._headerLayout = new Promise((resolve) => {
this._resolveHeaderLayout = resolve;
});
}
if (this.props.renderFooter && !this.props.horizontal) {
this._footerLayout = new Promise((resolve) => {
this._resolveFooterLayout = resolve;
Expand Down Expand Up @@ -200,6 +206,7 @@ export default class SortableList extends Component {
scrollEventThrottle={2}
scrollEnabled={scrollEnabled}
onScroll={this._onScroll}>
{this._renderHeader()}
<View style={innerContainerStyle}>
{this._renderRows()}
</View>
Expand Down Expand Up @@ -266,6 +273,20 @@ export default class SortableList extends Component {
});
}

_renderHeader() {
if (!this.props.renderHeader || this.props.horizontal) {
return null;
}

const {headerLayout} = this.state;

return (
<View onLayout={!headerLayout ? this._onLayoutHeader : null}>
{this.props.renderHeader()}
</View>
);
}

_renderFooter() {
if (!this.props.renderFooter || this.props.horizontal) {
return null;
Expand All @@ -281,8 +302,8 @@ export default class SortableList extends Component {
}

_onUpdateLayouts() {
Promise.all([this._footerLayout, ...Object.values(this._rowsLayouts)])
.then(([footerLayout, ...rowsLayouts]) => {
Promise.all([this._headerLayout, this._footerLayout, ...Object.values(this._rowsLayouts)])
.then(([headerLayout, footerLayout, ...rowsLayouts]) => {
// Can get correct container’s layout only after rows’s layouts.
this._container.measure((x, y, width, height, pageX, pageY) => {
const rowsLayoutsByKey = {};
Expand All @@ -298,6 +319,7 @@ export default class SortableList extends Component {
this.setState({
containerLayout: {x, y, width, height, pageX, pageY},
rowsLayouts: rowsLayoutsByKey,
headerLayout,
footerLayout,
contentHeight,
contentWidth,
Expand Down Expand Up @@ -524,6 +546,10 @@ export default class SortableList extends Component {
this._resolveRowLayout[rowKey]({rowKey, layout});
}

_onLayoutHeader = ({nativeEvent: {layout}}) => {
this._resolveHeaderLayout(layout);
};

_onLayoutFooter = ({nativeEvent: {layout}}) => {
this._resolveFooterLayout(layout);
};
Expand Down