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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Add notifyPageChange function to List",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "alebet@microsoft.com"
}
75 changes: 49 additions & 26 deletions packages/office-ui-fabric-react/src/components/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export class List extends BaseComponent<IListProps, IListState> implements IList
const {
page: {
items,
startIndex
startIndex
},
...divProps
} = pageProps;
Expand Down Expand Up @@ -570,13 +570,15 @@ export class List extends BaseComponent<IListProps, IListState> implements IList
}

let newListState = this._buildPages(props);
let oldListPages = this.state.pages;
let oldListPages = this.state.pages!;

this._notifyPageChanges(oldListPages, newListState.pages!);

this.setState(newListState, () => {
// If we weren't provided with the page height, measure the pages
if (!props.getPageHeight) {
// If measured version is invalid since we've updated the DOM
const heightsChanged = this._updatePageMeasurements(oldListPages as IPage[], newListState.pages as IPage[]);
const heightsChanged = this._updatePageMeasurements(newListState.pages!);

// On first render, we should re-measure so that we don't get a visual glitch.
if (heightsChanged) {
Expand All @@ -598,7 +600,48 @@ export class List extends BaseComponent<IListProps, IListState> implements IList
});
}

private _updatePageMeasurements(oldPages: IPage[], newPages: IPage[]) {
/**
* Notify consumers that the rendered pages have changed
* @param oldPages The old pages
* @param newPages The new pages
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add props?

* @param props The props to use
*/
private _notifyPageChanges(oldPages: IPage[], newPages: IPage[], props: IListProps = this.props) {
const {
onPageAdded,
onPageRemoved
} = props;

if (onPageAdded || onPageRemoved) {
const renderedIndexes: {
[index: number]: IPage;
} = {};

for (const page of oldPages) {
if (page.items) {
renderedIndexes[page.startIndex] = page;
}
}

for (const page of newPages) {
if (page.items) {
if (!renderedIndexes[page.startIndex]) {
this._onPageAdded(page);
} else {
delete renderedIndexes[page.startIndex];
}
}
}

for (let index in renderedIndexes) {
if (renderedIndexes.hasOwnProperty(index)) {
this._onPageRemoved(renderedIndexes[index]);
}
}
}
}

private _updatePageMeasurements(pages: IPage[]) {
const renderedIndexes: {
[index: number]: IPage;
} = {};
Expand All @@ -610,31 +653,11 @@ export class List extends BaseComponent<IListProps, IListState> implements IList
return heightChanged;
}

for (let i = 0; i < oldPages.length; i++) {
let page = oldPages[i];

if (page.items) {
renderedIndexes[page.startIndex] = page;
}
}

for (let i = 0; i < newPages.length; i++) {
let page = newPages[i];
for (let i = 0; i < pages.length; i++) {
let page = pages[i];

if (page.items) {
heightChanged = this._measurePage(page) || heightChanged;

if (!renderedIndexes[page.startIndex]) {
this._onPageAdded(page);
} else {
delete renderedIndexes[page.startIndex];
}
}
}

for (let index in renderedIndexes) {
if (renderedIndexes.hasOwnProperty(index)) {
this._onPageRemoved(renderedIndexes[index]);
}
}

Expand Down