diff --git a/packages/superset-ui-demo/storybook/stories/plugins/legacy-plugin-chart-pivot-table/Stories.tsx b/packages/superset-ui-demo/storybook/stories/plugins/legacy-plugin-chart-pivot-table/Stories.tsx index dc2fc91074..fba000312a 100644 --- a/packages/superset-ui-demo/storybook/stories/plugins/legacy-plugin-chart-pivot-table/Stories.tsx +++ b/packages/superset-ui-demo/storybook/stories/plugins/legacy-plugin-chart-pivot-table/Stories.tsx @@ -30,8 +30,81 @@ export const basic = () => ( ['sum__num', 'other'], ['sum__num', 'All'], ], - html: - '\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
sum__num
stateotherAll
name
Christopher803607803607
David673992673992
James749686749686
Jennifer587540587540
John638450638450
Joshua548044548044
Matthew608212608212
Michael10479961047996
Robert575592575592
William574464574464
All68075836807583
', + html: ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
sum__num
stateotherAll
name
Apache Superset803607803607
David pixel673992673992
pixelApache Superset749686749686
Jennifer587540587540
John638450638450
Joshua548044548044
Matthew608212608212
Michael10479961047996
Robert575592575592
William574464574464
All68075836807583
`, }, }, ]} diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index 4aa7997416..4b028d04ea 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -48,6 +48,9 @@ const propTypes = { verboseMap: PropTypes.objectOf(PropTypes.string), }; +const hasOnlyTextChild = node => + node.childNodes.length === 1 && node.childNodes[0].nodeType === Node.TEXT_NODE; + function PivotTable(element, props) { const { columnFormats, @@ -79,42 +82,31 @@ function PivotTable(element, props) { const cols = Array.isArray(columns[0]) ? columns.map(col => col[0]) : columns; const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; - $container.find('thead tr th').each(function () { - const cellValue = formatDateCellValue( - $(this)[0].textContent, - verboseMap, - dateRegex, - dateFormatter, - ); - $(this)[0].textContent = cellValue; - }); - - $container.find('tbody tr th').each(function () { - const cellValue = formatDateCellValue( - $(this)[0].textContent, - verboseMap, - dateRegex, - dateFormatter, - ); - $(this)[0].textContent = cellValue; + $container.find('th').each(function formatTh() { + if (hasOnlyTextChild(this)) { + const cellValue = formatDateCellValue($(this).text(), verboseMap, dateRegex, dateFormatter); + $(this).text(cellValue); + } }); $container.find('tbody tr').each(function eachRow() { $(this) .find('td') .each(function eachTd(index) { - const tdText = $(this)[0].textContent; - const { textContent, attr } = formatCellValue( - index, - cols, - tdText, - columnFormats, - numberFormat, - dateRegex, - dateFormatter, - ); - $(this)[0].textContent = textContent; - $(this).attr = attr; + if (hasOnlyTextChild(this)) { + const tdText = $(this).text(); + const { textContent, sortAttributeValue } = formatCellValue( + index, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + $(this).text(textContent); + $(this).attr('data-sort', sortAttributeValue); + } }); }); diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts index 1eac30de0a..7e3734ac4c 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts @@ -47,10 +47,8 @@ function formatCellValue( sortAttributeValue = Number.NEGATIVE_INFINITY; } } - // @ts-ignore - const attr = ('data-sort', sortAttributeValue); - return { textContent, attr }; + return { textContent, sortAttributeValue }; } function formatDateCellValue(text: string, verboseMap: any, dateRegex: RegExp, dateFormatter: any) { diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts index 04966a7920..d70f200080 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts @@ -29,7 +29,7 @@ describe('pivot table plugin format cells', () => { const dateFormatter = getTimeFormatterForGranularity('P1D'); it('render number', () => { - const { textContent, attr } = formatCellValue( + const { textContent, sortAttributeValue } = formatCellValue( i, cols, tdText, @@ -39,7 +39,7 @@ describe('pivot table plugin format cells', () => { dateFormatter, ); expect(textContent).toEqual('2.22M'); - expect(attr).toEqual(('data-sort', 2222222)); + expect(sortAttributeValue).toEqual(2222222); }); it('render date', () => { @@ -60,7 +60,7 @@ describe('pivot table plugin format cells', () => { it('render string', () => { tdText = 'some-text'; - const { textContent, attr } = formatCellValue( + const { textContent, sortAttributeValue } = formatCellValue( i, cols, tdText, @@ -70,13 +70,13 @@ describe('pivot table plugin format cells', () => { dateFormatter, ); expect(textContent).toEqual(tdText); - expect(attr).toEqual(('data-sort', tdText)); + expect(sortAttributeValue).toEqual(tdText); }); it('render null', () => { tdText = 'null'; - const { textContent, attr } = formatCellValue( + const { textContent, sortAttributeValue } = formatCellValue( i, cols, tdText, @@ -86,6 +86,6 @@ describe('pivot table plugin format cells', () => { dateFormatter, ); expect(textContent).toEqual(''); - expect(attr).toEqual(('data-sort', Number.NEGATIVE_INFINITY)); + expect(sortAttributeValue).toEqual(Number.NEGATIVE_INFINITY); }); }); diff --git a/yarn.lock b/yarn.lock index 00efc4656e..d340be4158 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4740,14 +4740,6 @@ jest-diff "^26.0.0" pretty-format "^26.0.0" -"@types/jest@^26.0.0": - version "26.0.15" - resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.15.tgz#12e02c0372ad0548e07b9f4e19132b834cb1effe" - integrity sha512-s2VMReFXRg9XXxV+CW9e5Nz8fH2K1aEhwgjUqPPbQd7g95T0laAcvLv032EhFHIa5GHsZ8W7iJEQVaJq6k3Gog== - dependencies: - jest-diff "^26.0.0" - pretty-format "^26.0.0" - "@types/jest@^26.0.4": version "26.0.13" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.13.tgz#5a7b9d5312f5dd521a38329c38ee9d3802a0b85e" @@ -14667,7 +14659,7 @@ jest@^25.5.4: import-local "^3.0.2" jest-cli "^25.5.4" -jest@^26.0.1, jest@^26.6.3: +jest@^26.6.3: version "26.6.3" resolved "https://registry.yarnpkg.com/jest/-/jest-26.6.3.tgz#40e8fdbe48f00dfa1f0ce8121ca74b88ac9148ef" integrity sha512-lGS5PXGAzR4RF7V5+XObhqz2KZIDUA1yD0DG6pBVmy10eh0ZIXQImRuzocsI/N2XZ1GrLFwTS27In2i2jlpq1Q==