-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcollectionFormatter.ts
36 lines (32 loc) · 1.29 KB
/
collectionFormatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { arrayToCsvFormatter } from './arrayToCsvFormatter.js';
import type { Formatter } from './../interfaces/index.js';
import { findOrDefault } from '../services/index.js';
/**
* Looks up values from the columnDefinition.params.collection property and displays the label in CSV or string format
* @example
* // the grid will display 'foo' and 'bar' and not 1 and 2 from your dataset
* { params: { collection: [{ value: 1, label: 'foo'}, {value: 2, label: 'bar' }] }}
* const dataset = [1, 2];
*/
export const collectionFormatter: Formatter = (row, cell, value, columnDef, dataContext, grid) => {
if (!value || !columnDef || !columnDef.params || !columnDef.params.collection || !columnDef.params.collection.length) {
return value;
}
const {
params,
params: { collection },
} = columnDef;
const labelName = params.customStructure ? params.customStructure.label : 'label';
const valueName = params.customStructure ? params.customStructure.value : 'value';
if (Array.isArray(value)) {
return arrayToCsvFormatter(
row,
cell,
value.map((v: any) => findOrDefault(collection, (c: any) => c[valueName] === v)[labelName]),
columnDef,
dataContext,
grid
);
}
return findOrDefault(collection, (c: any) => c[valueName] === value)[labelName] || '';
};