Skip to content

Commit 1a7e397

Browse files
committed
Add handling a new data receiving
1 parent 15e7ec4 commit 1a7e397

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

src/SortableList.js

+69-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import Row from './Row';
66
const AUTOSCROLL_AREA_HEIGTH = 60;
77
const AUTOSCROLL_INTERVAL = 100;
88

9+
function uniqueRowKey(key) {
10+
return `${key}${uniqueRowKey.id}`
11+
}
12+
13+
uniqueRowKey.id = 0
14+
915
export default class SortableList extends Component {
1016
static propTypes = {
1117
data: PropTypes.object.isRequired,
@@ -42,6 +48,7 @@ export default class SortableList extends Component {
4248
state = {
4349
order: this.props.order || Object.keys(this.props.data),
4450
rowsLayouts: null,
51+
containerLayout: null,
4552
data: this.props.data,
4653
activeRowKey: null,
4754
activeRowIndex: null,
@@ -54,37 +61,37 @@ export default class SortableList extends Component {
5461
};
5562

5663
componentDidMount() {
57-
Promise.all([...this._rowsLayouts])
58-
.then((rowsLayouts) => {
59-
// Can get correct container’s layout only after rows’s layouts.
60-
this._container.measure((x, y, width, height, pageX, pageY) => {
61-
const rowsLayoutsByKey = {};
62-
let contentHeight = 0;
63-
64-
rowsLayouts.forEach(({rowKey, layout}) => {
65-
rowsLayoutsByKey[rowKey] = layout;
66-
contentHeight += layout.height;
67-
});
64+
this._onLayotRows();
65+
}
6866

69-
this.setState({
70-
containerLayout: {x, y, width, height, pageX, pageY},
71-
rowsLayouts: rowsLayoutsByKey,
72-
contentHeight,
73-
}, () => {
74-
Animated.spring(this.state.style.opacity, {
75-
toValue: 1,
76-
}).start(() => (this._areRowsAnimated = true));
77-
});
67+
componentWillReceiveProps(nextProps) {
68+
const {data, order} = this.state;
69+
const {data: nextData, order: nextOrder} = nextProps;
70+
71+
if (data && nextData && !shallowEqual(data, nextData)) {
72+
this._animateRowsDisappearance(() => {
73+
uniqueRowKey.id++;
74+
this._areRowsAnimated = false;
75+
this._rowsLayouts = [];
76+
this.setState({
77+
data: nextData,
78+
containerLayout: null,
79+
rowsLayouts: null,
80+
order: nextOrder || Object.keys(nextData)
7881
});
7982
});
83+
84+
} else if (order && nextOrder && !shallowEqual(order, nextOrder)) {
85+
this.setState({order: nextOrder});
86+
}
8087
}
8188

82-
componentWillReceiveProps(nextProps) {
83-
const {order} = this.props;
84-
const {order: nextOrder} = nextProps;
89+
componentDidUpdate(prevProps, prevState) {
90+
const {data} = this.state;
91+
const {data: prevData} = prevState;
8592

86-
if (order && nextOrder && !shallowEqual(order, nextOrder)) {
87-
this.setState({order: nextOrder});
93+
if (data && prevData && !shallowEqual(data, prevData)) {
94+
this._onLayotRows();
8895
}
8996
}
9097

@@ -195,7 +202,7 @@ export default class SortableList extends Component {
195202

196203
return (
197204
<Row
198-
key={key}
205+
key={uniqueRowKey(key)}
199206
ref={this._onRefRow.bind(this, key)}
200207
animated={this._areRowsAnimated && !active}
201208
disabled={!sortingEnabled}
@@ -217,6 +224,42 @@ export default class SortableList extends Component {
217224
});
218225
}
219226

227+
_onLayotRows() {
228+
Promise.all([...this._rowsLayouts])
229+
.then((rowsLayouts) => {
230+
// Can get correct container’s layout only after rows’s layouts.
231+
this._container.measure((x, y, width, height, pageX, pageY) => {
232+
const rowsLayoutsByKey = {};
233+
let contentHeight = 0;
234+
235+
rowsLayouts.forEach(({rowKey, layout}) => {
236+
rowsLayoutsByKey[rowKey] = layout;
237+
contentHeight += layout.height;
238+
});
239+
240+
this.setState({
241+
containerLayout: {x, y, width, height, pageX, pageY},
242+
rowsLayouts: rowsLayoutsByKey,
243+
contentHeight,
244+
}, () => {
245+
this._animateRowsAppearance(() => (this._areRowsAnimated = true));
246+
});
247+
});
248+
});
249+
}
250+
251+
_animateRowsAppearance(onAnimationEnd) {
252+
Animated.timing(this.state.style.opacity, {
253+
toValue: 1,
254+
}).start(onAnimationEnd);
255+
}
256+
257+
_animateRowsDisappearance(onAnimationEnd) {
258+
Animated.timing(this.state.style.opacity, {
259+
toValue: 0,
260+
}).start(onAnimationEnd);
261+
}
262+
220263
_scroll(animated) {
221264
this._scrollView.scrollTo({...this._contentOffset, animated});
222265
}

0 commit comments

Comments
 (0)