Skip to content

Commit 4f90945

Browse files
shleewhitealex-judidoodchyun
authored
AdvancedTable: set checkbox column width to min-content (#2761)
Co-authored-by: Alex <alex-ju@users.noreply.github.com> Co-authored-by: Cristiano Rastelli <cristiano.rastelli@hashicorp.com> Co-authored-by: Dylan Hyun <dylan.hyun@hashicorp.com>
1 parent e53afd3 commit 4f90945

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

.changeset/purple-poems-battle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
`AdvancedTable` - Always set the select checkbox column width to `min-content` so it does not grow when the `AdvancedTable` is narrower than the container

packages/components/src/components/hds/advanced-table/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,17 @@ export default class HdsAdvancedTable extends Component<HdsAdvancedTableSignatur
251251
get gridTemplateColumns(): string {
252252
const { isSelectable, columns } = this.args;
253253

254-
// if there is no custom column widths, each column is the same width and they take up the available space (except the select checkbox)
255-
if (!this.columnWidths) {
256-
return `${isSelectable ? 'auto ' : ''}repeat(${columns.length}, 1fr)`;
257-
}
258-
259-
// if there is a select checkbox, the first column is 'auto' width to hug the checkbox content
260-
let style = isSelectable ? 'auto' : '';
254+
// if there is a select checkbox, the first column has a 'min-content' width to hug the checkbox content
255+
let style = isSelectable ? 'min-content ' : '';
261256

262-
// check the custom column widths, if the current column has a custom width use the custom width. otherwise take the available space.
263-
for (let i = 0; i < this.columnWidths.length; i++) {
264-
style += ` ${this.columnWidths[i] ? this.columnWidths[i] : '1fr'}`;
257+
if (!this.columnWidths) {
258+
// if there are no custom column widths, each column is the same width and they take up the available space
259+
style += `repeat(${columns.length}, 1fr)`;
260+
} else {
261+
// check the custom column widths, if the current column has a custom width use the custom width. otherwise take the available space.
262+
for (let i = 0; i < this.columnWidths.length; i++) {
263+
style += ` ${this.columnWidths[i] ? this.columnWidths[i] : '1fr'}`;
264+
}
265265
}
266266

267267
return style;

showcase/app/styles/showcase-pages/advanced-table.scss

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ body.components-advanced-table {
1818
overflow-x: auto;
1919
}
2020

21+
.shw-component-advanced-table-narrow-wrapper {
22+
width: fit-content;
23+
}
24+
2125
// align table cell content for icon plus text in cell
2226
.shw-component-advanced-table-cell-content-div {
2327
display: flex;

showcase/app/templates/components/advanced-table.hbs

+24
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,30 @@
909909

910910
<Shw::Divider @level={{2}} />
911911

912+
<Shw::Text::H3>Wide container with a narrow AdvancedTable</Shw::Text::H3>
913+
914+
<div class="shw-component-advanced-table-narrow-wrapper">
915+
<Hds::AdvancedTable
916+
@isSelectable={{true}}
917+
@onSelectionChange={{this.onMultiSelectUsersSelectionChange__demo3}}
918+
@model={{this.model.userDataShort}}
919+
@columns={{array (hash key="id" label="ID" width="150px") (hash key="name" label="Name" width="150px")}}
920+
>
921+
<:body as |B|>
922+
<B.Tr
923+
@selectionKey={{B.data.id}}
924+
@isSelected={{B.data.isSelected}}
925+
@selectionAriaLabelSuffix="row #{{B.data.id}}"
926+
>
927+
<B.Td>{{B.data.id}}</B.Td>
928+
<B.Td>{{B.data.name}}</B.Td>
929+
</B.Tr>
930+
</:body>
931+
</Hds::AdvancedTable>
932+
</div>
933+
934+
<Shw::Divider @level={{2}} />
935+
912936
<Shw::Text::H3>AdvancedTable with <code>colspan/rowspan</code> attributes</Shw::Text::H3>
913937

914938
<Hds::AdvancedTable

0 commit comments

Comments
 (0)