55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.dev/license
77 */
8- import { computed , signal } from '@angular/core' ;
8+ import { computed } from '@angular/core' ;
99import { SignalLike , WritableSignalLike } from '../signal-like/signal-like' ;
10- import { ListFocus , ListFocusInputs , ListFocusItem } from '../list-focus/list-focus' ;
1110
1211/** Represents an item that can be expanded or collapsed. */
13- export interface ExpansionItem extends ListFocusItem {
12+ export interface ExpansionItem {
1413 /** Whether the item is expandable. */
1514 expandable : SignalLike < boolean > ;
1615
1716 /** Used to uniquely identify an expansion item. */
1817 expansionId : SignalLike < string > ;
18+
19+ /** Whether the expansion is disabled. */
20+ disabled : SignalLike < boolean > ;
1921}
2022
2123export interface ExpansionControl extends ExpansionItem { }
@@ -30,10 +32,9 @@ export class ExpansionControl {
3032 /** Whether this item can be expanded. */
3133 readonly isExpandable = computed ( ( ) => this . inputs . expansionManager . isExpandable ( this ) ) ;
3234
33- constructor ( readonly inputs : ExpansionItem & { expansionManager : ListExpansion < ExpansionItem > } ) {
35+ constructor ( readonly inputs : ExpansionItem & { expansionManager : ListExpansion } ) {
3436 this . expansionId = inputs . expansionId ;
3537 this . expandable = inputs . expandable ;
36- this . element = inputs . element ;
3738 this . disabled = inputs . disabled ;
3839 }
3940
@@ -54,28 +55,31 @@ export class ExpansionControl {
5455}
5556
5657/** Represents the required inputs for an expansion behavior. */
57- export interface ListExpansionInputs < T extends ExpansionItem > extends ListFocusInputs < T > {
58+ export interface ListExpansionInputs {
5859 /** Whether multiple items can be expanded at once. */
5960 multiExpandable : SignalLike < boolean > ;
6061
6162 /** An array of ids of the currently expanded items. */
6263 expandedIds : WritableSignalLike < string [ ] > ;
64+
65+ /** An array of expansion items. */
66+ items : SignalLike < ExpansionItem [ ] > ;
67+
68+ /** Whether all expansions are disabled. */
69+ disabled : SignalLike < boolean > ;
6370}
6471
6572/** Manages the expansion state of a list of items. */
66- export class ListExpansion < T extends ExpansionItem > {
73+ export class ListExpansion {
6774 /** A signal holding an array of ids of the currently expanded items. */
6875 expandedIds : WritableSignalLike < string [ ] > ;
6976
70- /** The currently active (focused) item in the list. */
71- activeItem = computed ( ( ) => this . inputs . focusManager . activeItem ( ) ) ;
72-
73- constructor ( readonly inputs : ListExpansionInputs < T > & { focusManager : ListFocus < T > } ) {
74- this . expandedIds = inputs . expandedIds ?? signal ( [ ] ) ;
77+ constructor ( readonly inputs : ListExpansionInputs ) {
78+ this . expandedIds = inputs . expandedIds ;
7579 }
7680
77- /** Opens the specified item, or the currently active item if none is specified . */
78- open ( item : T = this . activeItem ( ) ) {
81+ /** Opens the specified item. */
82+ open ( item : ExpansionItem ) {
7983 if ( ! this . isExpandable ( item ) ) return ;
8084 if ( this . isExpanded ( item ) ) return ;
8185 if ( ! this . inputs . multiExpandable ( ) ) {
@@ -84,18 +88,15 @@ export class ListExpansion<T extends ExpansionItem> {
8488 this . expandedIds . update ( ids => ids . concat ( item . expansionId ( ) ) ) ;
8589 }
8690
87- /** Closes the specified item, or the currently active item if none is specified . */
88- close ( item : T = this . activeItem ( ) ) {
91+ /** Closes the specified item. */
92+ close ( item : ExpansionItem ) {
8993 if ( this . isExpandable ( item ) ) {
9094 this . expandedIds . update ( ids => ids . filter ( id => id !== item . expansionId ( ) ) ) ;
9195 }
9296 }
9397
94- /**
95- * Toggles the expansion state of the specified item,
96- * or the currently active item if none is specified.
97- */
98- toggle ( item : T = this . activeItem ( ) ) {
98+ /** Toggles the expansion state of the specified item. */
99+ toggle ( item : ExpansionItem ) {
99100 this . expandedIds ( ) . includes ( item . expansionId ( ) ) ? this . close ( item ) : this . open ( item ) ;
100101 }
101102
@@ -116,12 +117,12 @@ export class ListExpansion<T extends ExpansionItem> {
116117 }
117118
118119 /** Checks whether the specified item is expandable / collapsible. */
119- isExpandable ( item : T ) {
120+ isExpandable ( item : ExpansionItem ) {
120121 return ! this . inputs . disabled ( ) && ! item . disabled ( ) && item . expandable ( ) ;
121122 }
122123
123124 /** Checks whether the specified item is currently expanded. */
124- isExpanded ( item : T ) : boolean {
125+ isExpanded ( item : ExpansionItem ) : boolean {
125126 return this . expandedIds ( ) . includes ( item . expansionId ( ) ) ;
126127 }
127128}
0 commit comments