Skip to content

Commit e4ab93c

Browse files
author
cat-bro
authored
Merge pull request #15 from uptick/enhance/row-and-cell-shouldComponentUpdate-methods
update shouldComponentUpdate methods for row and cell
2 parents d4452b4 + b4d5187 commit e4ab93c

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

src/object-cell.jsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react'
33
import Clone from 'clone'
44
import classNames from 'classnames'
55

6-
import { cellIsEditable } from './utilities'
6+
import { cellIsEditable, isDifferent } from './utilities'
77

88
import TextDrawer from './drawers/text.jsx'
99
import TextEditor from './editors/text.jsx'
@@ -33,29 +33,17 @@ class ObjectCell extends React.Component {
3333
}
3434

3535
shouldComponentUpdate(nextProps, nextState) {
36-
let isShallowDifferent = function(objectA, objectB, exemptions) {
37-
for (let key in objectA) {
38-
if (exemptions && key in exemptions) {
39-
continue
40-
}
41-
if (objectB[key] !== objectA[key]) {
42-
// console.log('key', key, 'does not equal')
43-
return true
44-
}
45-
}
46-
return false
47-
}
48-
let propsExemptions = {
36+
const propsExemptions = {
4937
'onMouseDownCell': true,
5038
'beginEdit': true,
5139
'updateField': true,
5240
'abortField': true,
5341
'cellError': true,
5442
}
55-
if (isShallowDifferent(this.props, nextProps, propsExemptions) || isShallowDifferent(nextProps, this.props, propsExemptions)) {
43+
if (isDifferent(this.props, nextProps, propsExemptions)) {
5644
return true
5745
}
58-
if (isShallowDifferent(this.state, nextState) || isShallowDifferent(nextState, this.state)) {
46+
if (isDifferent(this.state, nextState)) {
5947
return true
6048
}
6149
return false
@@ -69,7 +57,7 @@ class ObjectCell extends React.Component {
6957
}
7058

7159
handleMouseDown = (event) => {
72-
let button = event.which || event.button
60+
const button = event.which || event.button
7361
event.preventDefault()
7462
if (button === 0) {
7563
this.props.onMouseDownCell(this.getCellRef(), event.clientX, event.clientY, event.shiftKey)
@@ -88,15 +76,15 @@ class ObjectCell extends React.Component {
8876
}
8977

9078
render() {
91-
let classes = classNames('', {
79+
const classes = classNames('', {
9280
'selected': this.props.selected,
9381
'copying': this.props.copying,
9482
'editing': this.props.editing,
9583
})
9684

9785
if (this.props.editing) {
98-
let editor = this.props.column.editor || TextEditor
99-
let editorProps = Clone(this.props.column.editorProps || {})
86+
const editor = this.props.column.editor || TextEditor
87+
const editorProps = Clone(this.props.column.editorProps || {})
10088
editorProps.value = this.props.value
10189
editorProps.update = this.props.updateField
10290
editorProps.abort = this.props.abortField
@@ -126,8 +114,8 @@ class ObjectCell extends React.Component {
126114
</td>
127115
)
128116
} else {
129-
let drawer = this.props.column.drawer || TextDrawer
130-
let drawerProps = Clone(this.props.column.drawerProps || {})
117+
const drawer = this.props.column.drawer || TextDrawer
118+
const drawerProps = Clone(this.props.column.drawerProps || {})
131119

132120
drawerProps.value = this.props.value
133121
drawerProps.column = this.props.column

src/object-row.jsx

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react'
33
import JQuery from 'jquery'
44
import classNames from 'classnames'
55

6-
import { dictCount } from './utilities.js'
6+
import { dictCount, isDifferent } from './utilities.js'
77

88
class ObjectRow extends React.Component {
99
static propTypes = {
@@ -29,8 +29,8 @@ class ObjectRow extends React.Component {
2929
}
3030

3131
shouldComponentUpdate(nextProps, nextState) {
32-
let isMissingColumns = function(propsA, propsB, columnsKey) {
33-
for (let key in propsA[columnsKey]) {
32+
const isMissingColumns = function(propsA, propsB, columnsKey) {
33+
for (const key in propsA[columnsKey]) {
3434
if (key in propsB[columnsKey] === false) {
3535
// console.log('key', key, 'does not exist in both')
3636
return true
@@ -45,20 +45,7 @@ class ObjectRow extends React.Component {
4545
return true
4646
}
4747

48-
let isShallowDifferent = function(objectA, objectB, exemptions) {
49-
for (let key in objectA) {
50-
if (exemptions && key in exemptions) {
51-
continue
52-
}
53-
if (objectB[key] !== objectA[key]) {
54-
// console.log('key', key, 'does not equal')
55-
return true
56-
}
57-
}
58-
return false
59-
}
60-
61-
let propsExemptions = {
48+
const propsExemptions = {
6249
// ignore column we perform above
6350
'selectedColumns': true,
6451
'copyingColumns': true,
@@ -71,18 +58,18 @@ class ObjectRow extends React.Component {
7158
'onMouseDownCell': true,
7259
'beginEdit': true,
7360
}
74-
if (isShallowDifferent(this.props, nextProps, propsExemptions) || isShallowDifferent(nextProps, this.props, propsExemptions)) {
61+
if (isDifferent(this.props, nextProps, propsExemptions)) {
7562
return true
7663
}
77-
if (isShallowDifferent(this.state, nextState) || isShallowDifferent(nextState, this.state)) {
64+
if (isDifferent(this.state, nextState)) {
7865
return true
7966
}
8067
return false
8168
}
8269

8370
colInRanges(column, columns, rows) {
84-
let numRangeColumns = dictCount(columns)
85-
let numRangeRows = dictCount(rows)
71+
const numRangeColumns = dictCount(columns)
72+
const numRangeRows = dictCount(rows)
8673
if (numRangeColumns === 0 && numRangeRows === 0) {
8774
return false
8875
} else if (columns !== null && rows === null) {

src/utilities.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function dictCount(dict) {
22
let count = 0
3-
for (let _ in dict) {
3+
for (const _ in dict) {
44
count++
55
}
66
return count
77
}
88

99
function dictFirstKey(dict) {
10-
for (let dictKey in dict) {
10+
for (const dictKey in dict) {
1111
return dictKey
1212
}
1313
return undefined
@@ -20,8 +20,21 @@ function cellIsEditable(object, column) {
2020
return editorIsSet && !readOnly
2121
}
2222

23+
function isDifferent(objectA, objectB, exemptions) {
24+
for (const key in objectA) {
25+
if (exemptions && key in exemptions) {
26+
continue
27+
}
28+
if (JSON.stringify(objectB[key]) !== JSON.stringify(objectA[key])) {
29+
return true
30+
}
31+
}
32+
return false
33+
}
34+
2335
export {
2436
dictCount,
2537
dictFirstKey,
2638
cellIsEditable,
39+
isDifferent,
2740
}

0 commit comments

Comments
 (0)