Skip to content

Commit

Permalink
In beforeCreateCol hook add support for return false #5691
Browse files Browse the repository at this point in the history
  • Loading branch information
pnowak committed May 22, 2019
1 parent 49818bd commit 3157d3d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
2 changes: 1 addition & 1 deletion handsontable.d.ts
Expand Up @@ -1878,7 +1878,7 @@ declare namespace Handsontable {
beforeContextMenuSetItems?: (menuItems: contextMenu.MenuItemConfig[]) => void;
beforeContextMenuShow?: (context: plugins.ContextMenu) => void;
beforeCopy?: (data: CellValue[][], coords: plugins.RangeType[]) => void | boolean;
beforeCreateCol?: (index: number, amount: number, source?: ChangeSource) => void;
beforeCreateCol?: (index: number, amount: number, source?: ChangeSource) => void | boolean;
beforeCreateRow?: (index: number, amount: number, source?: ChangeSource) => void;
beforeCut?: (data: CellValue[][], coords: plugins.RangeType[]) => void | boolean;
beforeDetachChild?: (parent: RowObject, element: RowObject) => void;
Expand Down
48 changes: 25 additions & 23 deletions src/dataMap.js
Expand Up @@ -344,38 +344,40 @@ class DataMap {
let numberOfCreatedCols = 0;
let currentIndex;

this.instance.runHooks('beforeCreateCol', columnIndex, amount, source);
const beforeCreateColHook = this.instance.runHooks('beforeCreateCol', columnIndex, amount, source);

currentIndex = columnIndex;

const maxCols = this.instance.getSettings().maxCols;
while (numberOfCreatedCols < amount && this.instance.countCols() < maxCols) {
const constructor = columnFactory(this.GridSettings, this.priv.columnsSettingConflicts);

if (typeof columnIndex !== 'number' || columnIndex >= this.instance.countCols()) {
if (rlen > 0) {
for (let r = 0; r < rlen; r++) {
if (typeof data[r] === 'undefined') {
data[r] = [];
if (beforeCreateColHook !== false) {
const maxCols = this.instance.getSettings().maxCols;
while (numberOfCreatedCols < amount && this.instance.countCols() < maxCols) {
const constructor = columnFactory(this.GridSettings, this.priv.columnsSettingConflicts);

if (typeof columnIndex !== 'number' || columnIndex >= this.instance.countCols()) {
if (rlen > 0) {
for (let r = 0; r < rlen; r++) {
if (typeof data[r] === 'undefined') {
data[r] = [];
}
data[r].push(null);
}
data[r].push(null);
} else {
data.push([null]);
}
// Add new column constructor
this.priv.columnSettings.push(constructor);

} else {
data.push([null]);
for (let row = 0; row < rlen; row++) {
data[row].splice(currentIndex, 0, null);
}
// Add new column constructor at given index
this.priv.columnSettings.splice(currentIndex, 0, constructor);
}
// Add new column constructor
this.priv.columnSettings.push(constructor);

} else {
for (let row = 0; row < rlen; row++) {
data[row].splice(currentIndex, 0, null);
}
// Add new column constructor at given index
this.priv.columnSettings.splice(currentIndex, 0, constructor);
numberOfCreatedCols += 1;
currentIndex += 1;
}

numberOfCreatedCols += 1;
currentIndex += 1;
}

this.instance.runHooks('afterCreateCol', columnIndex, numberOfCreatedCols, source);
Expand Down
10 changes: 10 additions & 0 deletions src/pluginHooks.js
Expand Up @@ -173,6 +173,16 @@ const REGISTERED_HOOKS = [
* @param {Number} amount Number of newly created columns in the data source array.
* @param {String} [source] String that identifies source of hook call
* ([list of all available sources]{@link http://docs.handsontable.com/tutorial-using-callbacks.html#page-source-definition}).
* @returns {*} If returns `false` then operation of the creating the columns is canceled.
* @example
* ```js
* // To cancel a create column action, just return `false`.
* new Handsontable(element, {
* beforeCreateCol: function(data, coords) {
* return false;
* }
* });
* ```
*/
'beforeCreateCol',

Expand Down
15 changes: 15 additions & 0 deletions test/e2e/Core_alter.spec.js
Expand Up @@ -1130,6 +1130,21 @@ describe('Core_alter', () => {
expect(outputAfter).toEqual([2, 1, 'customSource']);
});

it('should not create columns when beforeCreateCol return false', () => {
handsontable({
data: arrayOfArrays(),
beforeCreateCol() {
return false;
},
});

const countedColumns = countCols();

alter('insert_col', 2, 1, 'customSource');

expect(countCols()).toBe(countedColumns);
});

it('should not create column header together with the column, if headers were NOT specified explicitly', () => {

handsontable({
Expand Down

0 comments on commit 3157d3d

Please sign in to comment.