11import { html , nothing } from "lit" ;
2- import { property , query , queryAssignedElements } from "lit/decorators.js" ;
2+ import { property , query , queryAssignedElements , state } from "lit/decorators.js" ;
33import { ifDefined } from "lit/directives/if-defined.js" ;
44import SgdsElement from "../../base/sgds-element" ;
55import { SgdsCheckbox } from "../Checkbox/sgds-checkbox" ;
@@ -36,13 +36,16 @@ export class SgdsDataTableRow extends SgdsElement {
3636 } ;
3737
3838 /** Arbitrary data associated with this row. Returned in event detail on row selection. */
39- @property ( { type : Object } ) rowData : Record < string , unknown > = { } ;
39+ @property ( { type : Object , reflect : true } ) rowData : Record < string , unknown > = { } ;
4040
4141 /** When true, the row has an expandable content area toggled by a chevron. */
42- @property ( { type : Boolean } ) expand = false ;
42+ @property ( { type : Boolean , reflect : true } ) expand = false ;
4343
4444 /** When true, the expandable content area is open. */
45- @property ( { type : Boolean } ) open = false ;
45+ @property ( { type : Boolean , reflect : true } ) open = false ;
46+
47+ /** When true, the row is checked. */
48+ @property ( { type : Boolean , reflect : true } ) checked = false ;
4649
4750 /** @internal — set by `sgds-data-table` to show a checkbox cell on this row. */
4851 @property ( { type : Boolean } ) showCheckbox = false ;
@@ -62,11 +65,17 @@ export class SgdsDataTableRow extends SgdsElement {
6265 @queryAssignedElements ( { flatten : true } )
6366 private _assignedCells ! : Array < SgdsDataTableCell | SgdsDataTableHead > ;
6467
68+ @state ( ) private _isHeaderRow = false ;
69+
6570 /** The checkbox rendered inside this row, if `showCheckbox` is true. */
6671 get checkbox ( ) : SgdsCheckbox | null {
6772 return this . _checkboxEl ?? null ;
6873 }
6974
75+ connectedCallback ( ) {
76+ super . connectedCallback ( ) ;
77+ }
78+
7079 firstUpdated ( ) {
7180 if ( this . _expandableBody ) {
7281 this . _expandableBody . hidden = ! this . open ;
@@ -108,6 +117,13 @@ export class SgdsDataTableRow extends SgdsElement {
108117 }
109118 }
110119
120+ @watch ( "checked" )
121+ async handleCheckedChange ( ) {
122+ if ( this . checked ) {
123+ this . emit ( "i-sgds-change" , { detail : { checked : true } } ) ;
124+ }
125+ }
126+
111127 /** Opens the expandable content area. */
112128 public async show ( ) {
113129 if ( this . open ) return ;
@@ -123,6 +139,8 @@ export class SgdsDataTableRow extends SgdsElement {
123139 }
124140
125141 private _onSlotChange ( ) {
142+ const cells = this . _assignedCells ?? [ ] ;
143+ this . _isHeaderRow = cells . some ( cell => cell instanceof SgdsDataTableHead ) ;
126144 this . requestUpdate ( ) ;
127145 }
128146
@@ -187,8 +205,8 @@ export class SgdsDataTableRow extends SgdsElement {
187205 </ td > ` ;
188206 }
189207
190- private _renderExpandCell ( isHeaderRow : boolean ) {
191- if ( isHeaderRow ) {
208+ private _renderExpandCell ( ) {
209+ if ( this . _isHeaderRow ) {
192210 return html `< th class ="control-cell " scope ="col "> </ th > ` ;
193211 }
194212
@@ -199,44 +217,43 @@ export class SgdsDataTableRow extends SgdsElement {
199217 </ td > ` ;
200218 }
201219
202- private _renderExpandPlaceholder ( isHeaderRow : boolean ) {
203- return isHeaderRow ? html `< th class ="control-cell " scope ="col "> </ th > ` : html `< td class ="control-cell "> </ td > ` ;
220+ private _renderExpandPlaceholder ( ) {
221+ return this . _isHeaderRow ? html `< th class ="control-cell " scope ="col "> </ th > ` : html `< td class ="control-cell "> </ td > ` ;
204222 }
205223
206- private _renderCheckboxCell ( isHeaderRow : boolean ) {
207- return isHeaderRow
224+ private _renderCheckboxCell ( ) {
225+ return this . _isHeaderRow
208226 ? html `< th class ="control-cell " scope ="col ">
209227 < div class ="data-table-cell checkbox-cell ">
210- < sgds-checkbox @sgds-change =${ this . _onCheckboxChange } > </ sgds-checkbox >
228+ < sgds-checkbox .checked = ${ this . checked } @sgds-change =${ this . _onCheckboxChange } > </ sgds-checkbox >
211229 </ div >
212230 </ th > `
213231 : html `< td class ="control-cell ">
214232 < div class ="data-table-cell checkbox-cell ">
215- < sgds-checkbox @sgds-change =${ this . _onCheckboxChange } > </ sgds-checkbox >
233+ < sgds-checkbox .checked = ${ this . checked } @sgds-change =${ this . _onCheckboxChange } > </ sgds-checkbox >
216234 </ div >
217235 </ td > ` ;
218236 }
219237
220- private _renderHiddenSlotCell ( isHeaderRow : boolean ) {
221- return isHeaderRow
238+ private _renderHiddenSlotCell ( ) {
239+ return this . _isHeaderRow
222240 ? html `< th hidden style ="display:none "> < slot @slotchange =${ this . _onSlotChange } > </ slot > </ th > `
223241 : html `< td hidden style ="display:none "> < slot @slotchange =${ this . _onSlotChange } > </ slot > </ td > ` ;
224242 }
225243
226244 render ( ) {
227245 const cells = this . _assignedCells ?? [ ] ;
228- const isHeaderRow = cells . some ( cell => cell instanceof SgdsDataTableHead ) ;
229246 const totalCols = cells . length + ( this . showCheckbox ? 1 : 0 ) + ( this . expand || this . showExpandPlaceholder ? 1 : 0 ) ;
230247
231248 return html `
232- < tr ?data-header-row =${ isHeaderRow } class =${ this . open ? "active" : "" } >
233- ${ this . _renderHiddenSlotCell ( isHeaderRow ) }
249+ < tr ?data-header-row =${ this . _isHeaderRow } class =${ this . open ? "active" : "" } >
250+ ${ this . _renderHiddenSlotCell ( ) }
234251 ${ this . expand
235- ? this . _renderExpandCell ( isHeaderRow )
252+ ? this . _renderExpandCell ( )
236253 : this . showExpandPlaceholder
237- ? this . _renderExpandPlaceholder ( isHeaderRow )
254+ ? this . _renderExpandPlaceholder ( )
238255 : nothing }
239- ${ this . showCheckbox ? this . _renderCheckboxCell ( isHeaderRow ) : nothing }
256+ ${ this . showCheckbox ? this . _renderCheckboxCell ( ) : nothing }
240257 ${ ( ( ) => {
241258 let visualColumnIndex = 0 ;
242259 return cells . map ( el => {
0 commit comments