Skip to content

Commit

Permalink
fix(table): dataLabel prop allows for customized data-label on header
Browse files Browse the repository at this point in the history
fixes #2941
  • Loading branch information
AllenBW committed Sep 30, 2019
1 parent 6d10b75 commit 119d1ff
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
Expand Up @@ -9,6 +9,7 @@ export interface HeaderCellProps {
isVisible?: boolean;
scope?: string;
textCenter?: boolean;
dataLabel?: string;
}

export const HeaderCell: React.FunctionComponent<HeaderCellProps> = ({
Expand All @@ -17,6 +18,7 @@ export const HeaderCell: React.FunctionComponent<HeaderCellProps> = ({
isVisible,
scope = '',
textCenter = false,
dataLabel = '',
...props
}: HeaderCellProps ) => {
const Component = component as any;
Expand Down
Expand Up @@ -135,6 +135,7 @@ export interface ICell {
data?: any;
header?: any;
cell?: any;
dataLabel?: string;
}

export interface IRowCell {
Expand Down
Expand Up @@ -171,6 +171,16 @@ describe('headerUtils', () => {
expect(result[0].cell.formatters.find((formatter) => formatter.name === 'testFunc')).toBeDefined();
expect(result[0].cell.transforms.find((transform) => transform.name === 'testFunc')).toBeDefined();
});

describe('custom dataLabel', () => {
const cells = [{ title: 'expanded first', dataLabel: 'compact first' }, { title: 'expanded second'}] as ICell[];
const mixed = calculateColumns(cells, {});
cells.forEach((oneCell: ICell, key) => {
test(`${oneCell}`, () => {
expect(mixed[key].props['data-label']).toBe(oneCell.dataLabel ||oneCell.title);
});
});
});
});
});

Expand Down
Expand Up @@ -59,14 +59,16 @@ const generateCell = ({ cellFormatters, cellTransforms, columnTransforms, cell }
/**
* Function to map custom simple object properties to expected format with property, header, cell, extra params
* and props.
* @param {*} column to be shown in header - either string or object with title, transformers and formatters (for cels as well).
* @param {*} column to be shown in header - either string or object with title, transformers and formatters (for cells as well).
* @param {*} extra additional object with callbacks for specific formatters.
* @param {*} key cell key to be shown in data-key.
* @param {*} props additional props for each cell.
* @returns {*} object with property, extraParams, header, cell and props.
*/
const mapHeader = (column: ICell, extra: any, key: number, ...props: any) => {
const title = (column.hasOwnProperty('title') ? column.title : column) as string | ICell;
const dataLabel = (column.hasOwnProperty('dataLabel') ? column.dataLabel : typeof title === 'string' ? title : `column-${key}`) as string | ICell;
console.error(dataLabel);
return {
property:
(typeof title === 'string' &&
Expand All @@ -80,7 +82,7 @@ const mapHeader = (column: ICell, extra: any, key: number, ...props: any) => {
header: generateHeader(column, title),
cell: generateCell(column, extra),
props: {
'data-label': typeof title === 'string' ? title : `column-${key}`,
'data-label': dataLabel,
'data-key': key,
...(column.hasOwnProperty('props') ? column.props : {}),
...props
Expand Down

0 comments on commit 119d1ff

Please sign in to comment.