Skip to content
Open
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
26 changes: 18 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var propTypes = {
imagesLoadedOptions: PropTypes.object,
elementType: PropTypes.string,
onLayoutComplete: PropTypes.func,
onRemoveComplete: PropTypes.func
onRemoveComplete: PropTypes.func,
updateOnEachComponentUpdate: PropTypes.bool
};

var MasonryComponent = createReactClass({
Expand All @@ -41,7 +42,8 @@ var MasonryComponent = createReactClass({
onLayoutComplete: function() {
},
onRemoveComplete: function() {
}
},
updateOnEachComponentUpdate: true
};
},

Expand Down Expand Up @@ -214,7 +216,9 @@ var MasonryComponent = createReactClass({
this.masonry.reloadItems();
}

this.masonry.layout();
if (this.props.updateOnEachComponentUpdate || reloadItems) {
this.reloadLayout();
}
},

derefImagesLoaded: function() {
Expand All @@ -237,7 +241,7 @@ var MasonryComponent = createReactClass({
if (this.props.onImagesLoaded) {
this.props.onImagesLoaded(instance);
}
this.masonry.layout();
this.reloadLayout();
}.bind(this), 100);

var imgLoad = imagesloaded(this.masonryContainer, this.props.imagesLoadedOptions).on(event, handler);
Expand All @@ -248,6 +252,14 @@ var MasonryComponent = createReactClass({
};
},

reloadLayout: debounce(
function() {
this.masonry.layout();
}, 100, {
leading: true
}
),

initializeResizableChildren: function() {
if (!this.props.enableResizableChildren) {
return;
Expand All @@ -261,9 +273,7 @@ var MasonryComponent = createReactClass({
},

listenToElementResize: function(el) {
this.erd.listenTo(el, function() {
this.masonry.layout()
}.bind(this))
this.erd.listenTo(el, this.reloadLayout);
},

destroyErd: function() {
Expand Down Expand Up @@ -300,7 +310,7 @@ var MasonryComponent = createReactClass({
}
this.masonry.destroy();
},

setRef: function(n) {
this.masonryContainer = n;
},
Expand Down
17 changes: 11 additions & 6 deletions spec/react-masonry-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ describe('React Masonry Component', function() {
onLayoutComplete: function() {
},
onRemoveComplete: function() {
}
},
updateOnEachComponentUpdate: true
});
});

Expand Down Expand Up @@ -442,7 +443,7 @@ describe('React Masonry Component', function() {
failureMessage(index, 'top', expectedPosition.top + 'px', element.style.top, phase))
}

it('should correctly layout remaining elements when first element is removed [columnWidth empty]', function() {
it('should correctly layout remaining elements when first element is removed [columnWidth empty]', function(done) {
let wrapperContext;
class Wrapper extends React.Component {
constructor() {
Expand Down Expand Up @@ -479,11 +480,15 @@ describe('React Masonry Component', function() {
}

wrapperContext.setState({items: localChildrenElements.slice(1)});
const secondElements = div.querySelectorAll('.item');

for (let i = 0; i < secondElements.length; i++) {
expectElementPositionToMatch(secondElements[i], secondPositions[i], i, 'after removal');
}
setTimeout(function(){
const secondElements = div.querySelectorAll('.item');

for (let i = 0; i < secondElements.length; i++) {
expectElementPositionToMatch(secondElements[i], secondPositions[i], i, 'after removal');
}
done();
}, 500);
});

it('should correctly layout remaining elements when first element is removed [columnWidth fixed]', function() {
Expand Down
1 change: 1 addition & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface MasonryPropTypes {
style?: Object;
onLayoutComplete?: (instance: any) => void;
onRemoveComplete?: (instance: any) => void;
updateOnEachComponentUpdate?: boolean;
}

declare const Masonry: ComponentClass<MasonryPropTypes>;
Expand Down