-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2599 from epam/feature/data-table-columns-group
[DataTable]: columns group.
- Loading branch information
Showing
15 changed files
with
476 additions
and
9 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
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
49 changes: 49 additions & 0 deletions
49
uui-components/src/table/columnsConfigurationModal/columnsGroupsUtils.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,49 @@ | ||
import { DataColumnGroupProps, DataColumnProps } from '@epam/uui-core'; | ||
|
||
interface GroupOfColumns<TItem, TId> { | ||
type: 'group'; | ||
group: DataColumnGroupProps; | ||
columns: DataColumnProps<TItem, TId>[]; | ||
} | ||
|
||
const getGroupsByKey = (groups: DataColumnGroupProps[]) => { | ||
if (!groups) { | ||
return null; | ||
} | ||
const gRec = groups.reduce<Record<string, DataColumnGroupProps>>( | ||
(g, group) => ({ ...g, [group.key]: group }), | ||
{}, | ||
); | ||
return !Object.keys(gRec).length ? null : gRec; | ||
}; | ||
|
||
export const isGroupOfColumns = <TItem, TId>( | ||
item: GroupOfColumns<TItem, TId> | DataColumnProps<TItem, TId>, | ||
): item is GroupOfColumns<TItem, TId> => 'type' in item && item.type === 'group'; | ||
|
||
export const getGroupsWithColumns = <TItem, TId>(columnGroups: DataColumnGroupProps[], columns: DataColumnProps<TItem, TId>[]) => { | ||
const columnGroupsByKey = getGroupsByKey(columnGroups); | ||
if (!columnGroupsByKey) { | ||
return columns; | ||
} | ||
|
||
return columns.reduce<Array<GroupOfColumns<TItem, TId> | DataColumnProps<TItem, TId>>>((columnsAndGroups, column) => { | ||
if (column.group) { | ||
const group = columnGroupsByKey[column.group]; | ||
if (!group) { | ||
throw new Error(`The '${column.group}' group mentioned in the '${column.key}' column is undefined.`); | ||
} | ||
const lastItem = columnsAndGroups[columnsAndGroups.length - 1]; | ||
if (lastItem && isGroupOfColumns(lastItem) && lastItem.group.key === column.group) { | ||
lastItem.columns.push(column); | ||
return columnsAndGroups; | ||
} | ||
|
||
columnsAndGroups.push({ type: 'group', group: columnGroupsByKey[column.group], columns: [column] }); | ||
return columnsAndGroups; | ||
} | ||
|
||
columnsAndGroups.push(column); | ||
return columnsAndGroups; | ||
}, []); | ||
}; |
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
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
Oops, something went wrong.