Skip to content

Commit 9ba3d84

Browse files
authored
Merge pull request hoogi91#60 from hoogi91/feature/improve-table-markup
Improve table header markup
2 parents 1b756bd + bdedbfc commit 9ba3d84

4 files changed

Lines changed: 48 additions & 9 deletions

File tree

Classes/ViewHelpers/Cell/RenderViewHelper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function initializeArguments(): void
3939
true
4040
);
4141
$this->registerArgument('isHeader', 'bool', 'True to render <th> otherwise it will be <td>', false, false);
42+
$this->registerArgument('scope', 'string', 'scope attribute value', false);
4243
}
4344

4445
/**
@@ -60,8 +61,13 @@ public static function renderStatic(
6061
$attributes .= $cell->getColspan() > 0 ? ' colspan="' . $cell->getColspan() . '"' : '';
6162
}
6263

64+
$isHeader = (bool)($arguments['isHeader'] ?? 0);
65+
if ($isHeader === true && isset($arguments['scope'])) {
66+
$attributes = ' scope="' . $arguments['scope'] . '"' . ($attributes ?? '');
67+
}
68+
6369
return sprintf(
64-
(bool)($arguments['isHeader'] ?? 0) === true ? '<th%s>%s</th>' : '<td%s>%s</td>',
70+
$isHeader === true ? '<th%s>%s</th>' : '<td%s>%s</td>',
6571
$attributes ?? '',
6672
$renderChildrenClosure()
6773
);

Resources/Private/Partials/Rows.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<f:for each="{rowsData}" as="rowData">
77
<tr>
88
<f:for each="{rowData}" as="value" iteration="columnIterator">
9-
<s:cell.render cell="{value}" isHeader="{columnIterator.isFirst} && {firstColumnIsHeader}">
9+
<s:cell.render cell="{value}"
10+
scope="{headerScope}"
11+
isHeader="({columnIterator.isFirst} && {firstColumnIsHeader}) || {forceHeader}">
1012
<s:cell.value.formatted cell="{value}"/>
1113
</s:cell.render>
1214
</f:for>

Resources/Private/Partials/Table.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
class="table{f:if(condition: '{data.tx_spreadsheets_ignore_styles} && {data.table_class}', then: ' table-{data.table_class}')}">
77
<f:if condition="{spreadsheet.headData}">
88
<thead>
9-
<f:render partial="Rows" section="Main" arguments="{rowsData: spreadsheet.headData}"/>
9+
<f:render partial="Rows" section="Main"
10+
arguments="{rowsData: spreadsheet.headData, forceHeader: 1, headerScope: 'col'}"/>
1011
</thead>
1112
</f:if>
1213
<f:if condition="{spreadsheet.bodyData}">
1314
<tbody>
1415
<f:render partial="Rows" section="Main"
15-
arguments="{rowsData: spreadsheet.bodyData, firstColumnIsHeader: spreadsheet.firstColumnIsHeader}"/>
16+
arguments="{rowsData: spreadsheet.bodyData, firstColumnIsHeader: spreadsheet.firstColumnIsHeader, headerScope: 'row'}"/>
1617
</tbody>
1718
</f:if>
1819
<f:if condition="{spreadsheet.footData}">

Tests/Functional/ViewHelpers/CellRenderViewHelperTest.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ class CellRenderViewHelperTest extends AbstractViewHelperTestCase
1212
/**
1313
* @dataProvider cellProvider
1414
*/
15-
public function testRender(string $expectedAttributes, ?array $cellMock, bool $isHeader = false): void
16-
{
15+
public function testRender(
16+
string $expectedAttributes,
17+
?array $cellMock,
18+
?string $scope = null,
19+
bool $isHeader = false
20+
): void {
1721
if ($cellMock !== null) {
1822
$cellValue = $this->createConfiguredMock(CellDataValueObject::class, $cellMock);
1923
}
@@ -24,8 +28,8 @@ public function testRender(string $expectedAttributes, ?array $cellMock, bool $i
2428
$expectedAttributes !== '' ? ' ' . $expectedAttributes : ''
2529
),
2630
$this->getView(
27-
'<test:cell.render cell="{cell}" isHeader="{isHeader}">content</test:cell.render>',
28-
['cell' => $cellValue ?? null, 'isHeader' => $isHeader]
31+
'<test:cell.render cell="{cell}" scope="{scope}" isHeader="{isHeader}">content</test:cell.render>',
32+
['cell' => $cellValue ?? null, 'scope' => $scope ?? null, 'isHeader' => $isHeader]
2933
)->render()
3034
);
3135
}
@@ -35,7 +39,33 @@ public function testRender(string $expectedAttributes, ?array $cellMock, bool $i
3539
*/
3640
public function testRenderAsHeader(string $expectedAttributes, ?array $cellMock): void
3741
{
38-
$this->testRender($expectedAttributes, $cellMock, true);
42+
$this->testRender($expectedAttributes, $cellMock, null, true);
43+
}
44+
45+
/**
46+
* @dataProvider cellProvider
47+
*/
48+
public function testRenderWithRowScope(string $expectedAttributes, ?array $cellMock): void
49+
{
50+
$this->testRender($expectedAttributes, $cellMock, 'row');
51+
}
52+
53+
/**
54+
* @dataProvider cellProvider
55+
*/
56+
public function testRenderAsHeaderWithRowScope(string $expectedAttributes, ?array $cellMock): void
57+
{
58+
$expectedAttributes = trim('scope="row" ' . $expectedAttributes);
59+
$this->testRender($expectedAttributes, $cellMock, 'row', true);
60+
}
61+
62+
/**
63+
* @dataProvider cellProvider
64+
*/
65+
public function testRenderAsHeaderWithColScope(string $expectedAttributes, ?array $cellMock): void
66+
{
67+
$expectedAttributes = trim('scope="col" ' . $expectedAttributes);
68+
$this->testRender($expectedAttributes, $cellMock, 'col', true);
3969
}
4070

4171
/**

0 commit comments

Comments
 (0)