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
10 changes: 8 additions & 2 deletions src/object-cell.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ class ObjectCell extends React.Component {
handleDoubleClick(event) {
this.beginEdit();
}
editable(objectId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't matter much, coz I know it works, but will this also work?

editable(objectId) {
  const { isReadOnly, editor } = this.props.column;
  const editorIsSet = !!editor; // bang bang! you're a boolean.
  const readOnly = typeof(isReadOnly) === 'function' ? isReadOnly(objectId) : isReadOnly;
  return editorIsSet && !readOnly;
}

Or, closer to your original code:

editable(objectId) {
  const editorIsSet = this.props.column.editor !== false;
  let editable = editorIsSet;
  if (editorIsSet) {
    if (typeof(this.props.column.isReadOnly) === 'boolean') {
      editable = !this.props.column.isReadOnly;
    } else if (typeof(this.props.column.isReadOnly) === 'function') {
      editable = !this.props.column.isReadOnly(objectId);
    }
  }
  return editable;
}

const { isReadOnly, editor } = this.props.column
const editorIsSet = !(editor === false)
const readOnly = typeof(isReadOnly) === 'function' ? isReadOnly(objectId) : (isReadOnly === true)
return editorIsSet && !readOnly
}
beginEdit(editReplaceOverride) {
if (!this.props.disabled && this.props.column.editor !== false)
if (!this.props.disabled && this.editable(this.getCellRef().objectId))
this.props.beginEdit(this.getCellRef(), editReplaceOverride);
}

Expand Down Expand Up @@ -113,7 +119,7 @@ class ObjectCell extends React.Component {

var cellProps = {
className: classNames(classes + ' drawer ' + drawer.className, {
uneditable: (this.props.column.editor === false),
uneditable: (!this.editable(this.getCellRef().objectId)),
}),
onMouseDown: this.handleMouseDown,
onDoubleClick: this.handleDoubleClick,
Expand Down
43 changes: 27 additions & 16 deletions src/object-table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ class ObjectTable extends React.PureComponent {
}
});
}

columnIsEditable(columnKey) {
for (var columnIndex = 0; columnIndex < this.props.columns.length; columnIndex++) {
var column = this.props.columns[columnIndex];
if (column.key == columnKey) {
return (column.editor !== false);
}
}
return false;
}
getEventCellRef(event) {
var cell = JQuery(event.target);
if (!cell.is('td'))
Expand Down Expand Up @@ -224,6 +214,26 @@ class ObjectTable extends React.PureComponent {
return next;
}

getColumnFromKey(key) {
Copy link
Contributor

Choose a reason for hiding this comment

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

See if this works:

getColumnFromKey(key) {
  const cols = this.props.columns || [];
  return cols.find(col => col.key === key);
}

const cols = this.props.columns || []
return cols.find(col => col.key === key)
}

cellIsEditable(objectId, column) {
if (typeof(column) === 'string'){
column = this.getColumnFromKey(column)
}
if (column) {
if (typeof(column.isReadOnly) === 'boolean') {
return !column.isReadOnly
} else if (typeof(column.isReadOnly) === 'function') {
return !column.isReadOnly(objectId)
}
return true
}
return false
}

handleKeyPress(event) {
if (this.state.editing === null) {
var editRow = this.getSelectedFirstVisibleRow();
Expand All @@ -241,7 +251,7 @@ class ObjectTable extends React.PureComponent {

// case 'enter':
case 13:
if (this.columnIsEditable(editColumn) && editObject && !editObject.disabled) {
if (this.cellIsEditable(editRow, editColumn) && editObject && !editObject.disabled) {
this.setState(state => {
state.editing = {
columnKey: editColumn,
Expand All @@ -255,7 +265,7 @@ class ObjectTable extends React.PureComponent {
break;

default:
if (this.columnIsEditable(editColumn) && editObject && !editObject.disabled) {
if (this.cellIsEditable(editRow, editColumn) && editObject && !editObject.disabled) {
this.setState(state => {
state.editing = {
columnKey: editColumn,
Expand Down Expand Up @@ -371,11 +381,12 @@ class ObjectTable extends React.PureComponent {

case 'backspace':
var editColumn = reactClass.getSelectedFirstVisibleColumn();
if (selectedCells > 0 && reactClass.columnIsEditable(editColumn)) {
var editRow = reactClass.getSelectedFirstVisibleRow()
if (selectedCells > 0 && reactClass.cellIsEditable(editRow, editColumn)) {
this.setState(state => {
state.editing = {
columnKey: editColumn,
objectId: reactClass.getSelectedFirstVisibleRow(),
objectId: editRow,
};
state.editReplace = '';
return state;
Expand Down Expand Up @@ -669,7 +680,7 @@ class ObjectTable extends React.PureComponent {
pastingColumn = true;
if (pastingColumnIndex < pasteData[pastingRowIndex].length) {
newSelectionColumns[column.key] = true;
if (column.editor !== false && !row.disabled) {
if (column.editor !== false && !row.disabled && this.cellIsEditable(row.id, column)) {
var editor = column.editor || TextEditor;
var validated = editor.validate(
pasteData[pastingRowIndex][pastingColumnIndex],
Expand Down Expand Up @@ -706,7 +717,7 @@ class ObjectTable extends React.PureComponent {
var column = reactClass.props.columns[columnIndex];
if (typeof reactClass.state.selectedColumns[column.key] == 'undefined')
continue;
if (column.editor !== false && !row.disabled) {
if (column.editor !== false && !row.disabled && this.cellIsEditable(row.id, column)) {
var editor = column.editor || TextEditor;
var validated = editor.validate(
pasteData[pasteRow][pasteColumn],
Expand Down