From 00f3f79c93325f03d0adfa93d82b3691294b5e42 Mon Sep 17 00:00:00 2001 From: jasonhsieh Date: Mon, 21 Jan 2019 22:13:55 +0800 Subject: [PATCH] update fix/836_columnDefIsArray --- src/utils/columnUtils.js | 26 +++++++++++++------------- stories/index.tsx | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/utils/columnUtils.js b/src/utils/columnUtils.js index 3786c25c..5a41667c 100644 --- a/src/utils/columnUtils.js +++ b/src/utils/columnUtils.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; const offset = 1000; /** Gets a column properties object from an array of columnNames @@ -7,8 +8,7 @@ function getColumnPropertiesFromColumnArray(columnProperties, columns) { return columns.reduce((previous, current, i) => { previous[current] = { id: current, order: offset + i }; return previous; - }, - columnProperties); + }, columnProperties); } /** Gets the column properties object from a react component (rowProperties) that contains child component(s) for columnProperties. @@ -16,28 +16,28 @@ function getColumnPropertiesFromColumnArray(columnProperties, columns) { * @param {Object} rowProperties - An React component that contains the rowProperties and child columnProperties components * @param {Array optional} allColumns - An optional array of colummn names. This will be used to generate the columnProperties when they are not defined in rowProperties */ -export function getColumnProperties(rowProperties, allColumns=[]) { +export function getColumnProperties(rowProperties, allColumns = []) { const children = rowProperties && rowProperties.props && rowProperties.props.children; const columnProperties = {}; // Working against an array of columnProperties if (Array.isArray(children)) { // build one object that contains all of the column properties keyed by id - children.reduce((previous, current, i) => { - if (current) { - previous[current.props.id] = {order: offset + i, ...current.props}; - } - return previous; - }, columnProperties); - - // Working against a lone, columnProperties object + _.reduce(_.flatten(children),(previous, current, i) => { + if (current) { + previous[current.props.id] = { order: offset + i, ...current.props }; + } + return previous; + }, columnProperties); + + // Working against a lone, columnProperties object } else if (children && children.props) { columnProperties[children.props.id] = { order: offset, ...children.props }; } - if(Object.keys(columnProperties).length === 0 && allColumns) { + if (Object.keys(columnProperties).length === 0 && allColumns) { getColumnPropertiesFromColumnArray(columnProperties, allColumns); } - return columnProperties; + return columnProperties; } diff --git a/stories/index.tsx b/stories/index.tsx index fbe503ce..f0b649a1 100644 --- a/stories/index.tsx +++ b/stories/index.tsx @@ -265,6 +265,21 @@ storiesOf('Griddle main', module) ) }) + .add("with filterable set by array of ", () => { + const ids = ["name", "city", "state"]; + return ( +
+ Name is not filterable + + + {ids.map(id => ( + + ))} + + +
+ ); + }) .add('with local and sort set', () => { const sortProperties = [ { id: 'name', sortAscending: true } @@ -1768,4 +1783,4 @@ storiesOf('TypeScript', module) ); - }) + }) \ No newline at end of file