From e8e785405e22cf8b59a5184cf9c162d116a32a83 Mon Sep 17 00:00:00 2001 From: Fiona Lawrence Date: Fri, 12 May 2017 14:50:23 +1000 Subject: [PATCH 1/2] Disable cells when they meet certain conditions --- src/object-cell.jsx | 14 +++++++++++-- src/object-table.jsx | 50 ++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/object-cell.jsx b/src/object-cell.jsx index fedcf7c..daaab9b 100644 --- a/src/object-cell.jsx +++ b/src/object-cell.jsx @@ -63,8 +63,18 @@ class ObjectCell extends React.Component { handleDoubleClick(event) { this.beginEdit(); } + editable(objectId) { + if (this.props.column.editor !== false) { + if (typeof(this.props.column.isReadOnly) === 'boolean') { + return !this.props.column.isReadOnly + } else if (typeof(this.props.column.isReadOnly) === 'function') { + return !this.props.column.isReadOnly(objectId) + } + } + return (this.props.column.editor !== false) + } 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); } @@ -113,7 +123,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, diff --git a/src/object-table.jsx b/src/object-table.jsx index 220a5a7..d5444cb 100644 --- a/src/object-table.jsx +++ b/src/object-table.jsx @@ -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')) @@ -224,6 +214,33 @@ class ObjectTable extends React.PureComponent { return next; } + getColumnFromKey(key) { + var column = null + for (var columnIndex = 0; columnIndex < this.props.columns.length; columnIndex++) { + var col = this.props.columns[columnIndex]; + if (col.key == key) { + column = col + break + } + } + return column + } + + 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(); @@ -241,7 +258,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, @@ -255,7 +272,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, @@ -371,11 +388,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; @@ -669,7 +687,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], @@ -706,7 +724,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], From 8f35d58bb4f1a7a05a395de718518c91b713ccd1 Mon Sep 17 00:00:00 2001 From: Fiona Lawrence Date: Fri, 12 May 2017 17:34:35 +1000 Subject: [PATCH 2/2] still works if props undefined --- src/object-cell.jsx | 12 ++++-------- src/object-table.jsx | 11 ++--------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/object-cell.jsx b/src/object-cell.jsx index daaab9b..fcb0e29 100644 --- a/src/object-cell.jsx +++ b/src/object-cell.jsx @@ -64,14 +64,10 @@ class ObjectCell extends React.Component { this.beginEdit(); } editable(objectId) { - if (this.props.column.editor !== false) { - if (typeof(this.props.column.isReadOnly) === 'boolean') { - return !this.props.column.isReadOnly - } else if (typeof(this.props.column.isReadOnly) === 'function') { - return !this.props.column.isReadOnly(objectId) - } - } - return (this.props.column.editor !== false) + 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.editable(this.getCellRef().objectId)) diff --git a/src/object-table.jsx b/src/object-table.jsx index d5444cb..575303a 100644 --- a/src/object-table.jsx +++ b/src/object-table.jsx @@ -215,15 +215,8 @@ class ObjectTable extends React.PureComponent { } getColumnFromKey(key) { - var column = null - for (var columnIndex = 0; columnIndex < this.props.columns.length; columnIndex++) { - var col = this.props.columns[columnIndex]; - if (col.key == key) { - column = col - break - } - } - return column + const cols = this.props.columns || [] + return cols.find(col => col.key === key) } cellIsEditable(objectId, column) {