Skip to content

Commit 743c714

Browse files
committed
Pull cell editability code out into one place
1 parent e5bcb27 commit 743c714

3 files changed

Lines changed: 17 additions & 25 deletions

File tree

src/object-cell.jsx

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

6+
import { cellIsEditable } from './utilities'
7+
68
import TextDrawer from './drawers/text.jsx'
79
import TextEditor from './editors/text.jsx'
810

@@ -76,10 +78,7 @@ class ObjectCell extends React.Component {
7678
this.beginEdit()
7779
}
7880
editable(objectId) {
79-
const { isReadOnly, editor } = this.props.column
80-
const editorIsSet = !(editor === false)
81-
const readOnly = typeof isReadOnly === 'function' ? isReadOnly(objectId) : (isReadOnly === true)
82-
return editorIsSet && !readOnly
81+
return cellIsEditable(objectId, this.props.column)
8382
}
8483
beginEdit = (editReplaceOverride) => {
8584
if (!this.props.disabled && this.editable(this.getCellRef().objectId)) {

src/object-table.jsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import JQuery from 'jquery'
55
import ClassNames from 'classnames'
66
import Clone from 'clone'
77

8-
import { dictCount, dictFirstKey } from './utilities.js'
8+
import { dictCount, dictFirstKey, cellIsEditable } from './utilities.js'
99
import { stringValue, deserializeCells } from './clipboard.js'
1010
import TextEditor from './editors/text.jsx'
1111
import TextDrawer from './drawers/text.jsx'
@@ -248,21 +248,6 @@ class ObjectTable extends React.PureComponent {
248248
return cols.find(col => col.key === key)
249249
}
250250

251-
cellIsEditable(objectId, column) {
252-
if (typeof column === 'string') {
253-
column = this.getColumnFromKey(column)
254-
}
255-
if (column) {
256-
if (typeof column.isReadOnly === 'boolean') {
257-
return !column.isReadOnly
258-
} else if (typeof column.isReadOnly === 'function') {
259-
return !column.isReadOnly(objectId)
260-
}
261-
return true
262-
}
263-
return false
264-
}
265-
266251
handleKeyPress = (event) => {
267252
if (this.state.editing === null) {
268253
let editRow = this.getSelectedFirstVisibleRow()
@@ -276,7 +261,7 @@ class ObjectTable extends React.PureComponent {
276261
switch (event.which) {
277262
// case 'enter':
278263
case 13:
279-
if (this.cellIsEditable(editRow, editColumn) && editObject && !editObject.disabled) {
264+
if (cellIsEditable(editRow, this.getColumnFromKey(editColumn)) && editObject && !editObject.disabled) {
280265
this.setState(state => {
281266
state.editing = {
282267
columnKey: editColumn,
@@ -290,7 +275,7 @@ class ObjectTable extends React.PureComponent {
290275
break
291276

292277
default:
293-
if (this.cellIsEditable(editRow, editColumn) && editObject && !editObject.disabled) {
278+
if (cellIsEditable(editRow, this.getColumnFromKey(editColumn)) && editObject && !editObject.disabled) {
294279
this.setState(state => {
295280
state.editing = {
296281
columnKey: editColumn,
@@ -406,7 +391,7 @@ class ObjectTable extends React.PureComponent {
406391
case 'backspace':
407392
let editColumn = this.getSelectedFirstVisibleColumn()
408393
let editRow = this.getSelectedFirstVisibleRow()
409-
if (selectedCells > 0 && this.cellIsEditable(editRow, editColumn)) {
394+
if (selectedCells > 0 && cellIsEditable(editRow, this.getColumnFromKey(editColumn))) {
410395
this.setState(state => {
411396
state.editing = {
412397
columnKey: editColumn,
@@ -691,7 +676,7 @@ class ObjectTable extends React.PureComponent {
691676
pastingColumn = true
692677
if (pastingColumnIndex < pasteData[pastingRowIndex].length) {
693678
newSelectionColumns[column.key] = true
694-
if (column.editor !== false && !row.disabled && this.cellIsEditable(row.id, column)) {
679+
if (!row.disabled && cellIsEditable(row.id, column)) {
695680
let editor = column.editor || TextEditor
696681
let validated = editor.validate(
697682
pasteData[pastingRowIndex][pastingColumnIndex],
@@ -732,7 +717,7 @@ class ObjectTable extends React.PureComponent {
732717
if (typeof this.state.selectedColumns[column.key] === 'undefined') {
733718
continue
734719
}
735-
if (column.editor !== false && !row.disabled && this.cellIsEditable(row.id, column)) {
720+
if (!row.disabled && cellIsEditable(row.id, column)) {
736721
let editor = column.editor || TextEditor
737722
let validated = editor.validate(
738723
pasteData[pasteRow][pasteColumn],

src/utilities.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ function dictFirstKey(dict) {
1313
return undefined
1414
}
1515

16+
function cellIsEditable(objectId, column) {
17+
const { isReadOnly, editor } = column
18+
const editorIsSet = editor !== false
19+
const readOnly = typeof isReadOnly === 'function' ? isReadOnly(objectId) : (isReadOnly === true)
20+
return editorIsSet && !readOnly
21+
}
22+
1623
export {
1724
dictCount,
1825
dictFirstKey,
26+
cellIsEditable,
1927
}

0 commit comments

Comments
 (0)