2016-11-22 10:11:12 +08:00
|
|
|
import React from 'react';
|
2016-06-22 13:18:43 +08:00
|
|
|
import assign from 'object-assign';
|
2016-11-22 10:11:12 +08:00
|
|
|
import Column from './Column';
|
|
|
|
import ColumnGroup from './ColumnGroup';
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
export function flatArray(data: Object[] = [], childrenName = 'children') {
|
|
|
|
const result: Object[] = [];
|
2016-03-16 16:39:53 +08:00
|
|
|
const loop = (array) => {
|
|
|
|
array.forEach(item => {
|
2016-06-22 13:18:43 +08:00
|
|
|
const newItem = assign({}, item);
|
2016-03-16 16:39:53 +08:00
|
|
|
delete newItem[childrenName];
|
|
|
|
result.push(newItem);
|
|
|
|
if (item[childrenName] && item[childrenName].length > 0) {
|
|
|
|
loop(item[childrenName]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
loop(data);
|
|
|
|
return result;
|
|
|
|
}
|
2016-09-09 13:55:41 +08:00
|
|
|
|
|
|
|
export function treeMap(tree: Object[], mapper: Function, childrenName = 'children') {
|
|
|
|
return tree.map((node, index) => {
|
|
|
|
const extra = {};
|
|
|
|
if (node[childrenName]) {
|
|
|
|
extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
|
|
|
|
}
|
|
|
|
return assign({}, mapper(node, index), extra);
|
|
|
|
});
|
|
|
|
}
|
2016-11-22 10:11:12 +08:00
|
|
|
|
|
|
|
export function normalizeColumns(elements) {
|
|
|
|
const columns: any[] = [];
|
|
|
|
React.Children.forEach(elements, (element: React.ReactElement<any>) => {
|
|
|
|
if (!isColumnElement(element)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const column = assign({}, element.props);
|
|
|
|
if (element.key) {
|
|
|
|
column.key = element.key;
|
|
|
|
}
|
|
|
|
if (element.type as any === ColumnGroup) {
|
|
|
|
column.children = normalizeColumns(column.children);
|
|
|
|
}
|
|
|
|
columns.push(column);
|
|
|
|
});
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isColumnElement(element) {
|
|
|
|
return element && (element.type === Column || element.type === ColumnGroup);
|
|
|
|
}
|