-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formatters): add grid option to auto add custom editor formatter (…
…#248) * feat(formatters): add grid option to auto add custom editor formatter
- Loading branch information
1 parent
e466f62
commit db77d46
Showing
6 changed files
with
107 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/vanilla-bundle/src/components/__tests__/slick-vanilla-utilities.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Column, Editors, Formatter, Formatters } from '@slickgrid-universal/common'; | ||
import { autoAddEditorFormatterToColumnsWithEditor } from '../slick-vanilla-utilities'; | ||
|
||
|
||
describe('Slick-Vanilla-Grid-Bundle / Utilies', () => { | ||
describe('autoAddEditorFormatterToColumnsWithEditor', () => { | ||
let columnDefinitions: Column[]; | ||
const customEditableInputFormatter: Formatter = (_row, _cell, value, columnDef) => { | ||
const isEditableLine = !!columnDef.editor; | ||
value = (value === null || value === undefined) ? '' : value; | ||
return isEditableLine ? `<div class="editing-field">${value}</div>` : value; | ||
}; | ||
|
||
beforeEach(() => { | ||
columnDefinitions = [ | ||
{ id: 'firstName', field: 'firstName', editor: { model: Editors.text } }, | ||
{ id: 'lastName', field: 'lastName', editor: { model: Editors.text }, formatter: Formatters.multiple, params: { formatters: [Formatters.italic, Formatters.bold] } }, | ||
{ id: 'age', field: 'age', type: 'number', formatter: Formatters.multiple }, | ||
{ id: 'address', field: 'address.street', editor: { model: Editors.longText }, formatter: Formatters.complexObject }, | ||
{ id: 'zip', field: 'address.zip', type: 'number', formatter: Formatters.complexObject }, | ||
]; | ||
}); | ||
|
||
it('should have custom editor formatter with correct structure', () => { | ||
autoAddEditorFormatterToColumnsWithEditor(columnDefinitions, customEditableInputFormatter); | ||
|
||
expect(columnDefinitions).toEqual([ | ||
{ id: 'firstName', field: 'firstName', editor: { model: Editors.text }, formatter: customEditableInputFormatter }, | ||
{ id: 'lastName', field: 'lastName', editor: { model: Editors.text }, formatter: Formatters.multiple, params: { formatters: [Formatters.italic, Formatters.bold, customEditableInputFormatter] } }, | ||
{ id: 'age', field: 'age', type: 'number', formatter: Formatters.multiple }, | ||
{ id: 'address', field: 'address.street', editor: { model: Editors.longText }, formatter: Formatters.multiple, params: { formatters: [Formatters.complexObject, customEditableInputFormatter] } }, | ||
{ id: 'zip', field: 'address.zip', type: 'number', formatter: Formatters.complexObject }, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/vanilla-bundle/src/components/slick-vanilla-utilities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Column, Formatter, Formatters } from '@slickgrid-universal/common'; | ||
|
||
/** | ||
* Automatically add a Custom Formatter on all column definitions that have an Editor. | ||
* Instead of manually adding a Custom Formatter on every column definitions that are editables, let's ask the system to do it in an easier automated way. | ||
* It will loop through all column definitions and add an Custom Editor Formatter when necessary, | ||
* also note that if there's already a Formatter on the column definition it will automatically use the Formatters.multiple and add the custom formatter into the `params: formatters: {}}` | ||
*/ | ||
export function autoAddEditorFormatterToColumnsWithEditor(columnDefinitions: Column[], customEditableFormatter: Formatter) { | ||
if (Array.isArray(columnDefinitions)) { | ||
for (const columnDef of columnDefinitions) { | ||
if (columnDef.editor) { | ||
if (columnDef.formatter && columnDef.formatter !== Formatters.multiple) { | ||
const prevFormatter = columnDef.formatter; | ||
columnDef.formatter = Formatters.multiple; | ||
columnDef.params = { ...columnDef.params, formatters: [prevFormatter, customEditableFormatter] }; | ||
} else if (columnDef.formatter && columnDef.formatter === Formatters.multiple && columnDef.params) { | ||
columnDef.params.formatters = [...columnDef.params.formatters, customEditableFormatter]; | ||
} else { | ||
columnDef.formatter = customEditableFormatter; | ||
} | ||
} | ||
} | ||
} | ||
} |