5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
- import { computed , signal } from '@angular/core' ;
8
+ import { computed } from '@angular/core' ;
9
9
import { SignalLike , WritableSignalLike } from '../signal-like/signal-like' ;
10
- import { ListFocus , ListFocusInputs , ListFocusItem } from '../list-focus/list-focus' ;
11
10
12
11
/** Represents an item that can be expanded or collapsed. */
13
- export interface ExpansionItem extends ListFocusItem {
12
+ export interface ExpansionItem {
14
13
/** Whether the item is expandable. */
15
14
expandable : SignalLike < boolean > ;
16
15
17
16
/** Used to uniquely identify an expansion item. */
18
17
expansionId : SignalLike < string > ;
18
+
19
+ /** Whether the expansion is disabled. */
20
+ disabled : SignalLike < boolean > ;
19
21
}
20
22
21
23
export interface ExpansionControl extends ExpansionItem { }
@@ -30,10 +32,9 @@ export class ExpansionControl {
30
32
/** Whether this item can be expanded. */
31
33
readonly isExpandable = computed ( ( ) => this . inputs . expansionManager . isExpandable ( this ) ) ;
32
34
33
- constructor ( readonly inputs : ExpansionItem & { expansionManager : ListExpansion < ExpansionItem > } ) {
35
+ constructor ( readonly inputs : ExpansionItem & { expansionManager : ListExpansion } ) {
34
36
this . expansionId = inputs . expansionId ;
35
37
this . expandable = inputs . expandable ;
36
- this . element = inputs . element ;
37
38
this . disabled = inputs . disabled ;
38
39
}
39
40
@@ -54,28 +55,31 @@ export class ExpansionControl {
54
55
}
55
56
56
57
/** Represents the required inputs for an expansion behavior. */
57
- export interface ListExpansionInputs < T extends ExpansionItem > extends ListFocusInputs < T > {
58
+ export interface ListExpansionInputs {
58
59
/** Whether multiple items can be expanded at once. */
59
60
multiExpandable : SignalLike < boolean > ;
60
61
61
62
/** An array of ids of the currently expanded items. */
62
63
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 > ;
63
70
}
64
71
65
72
/** Manages the expansion state of a list of items. */
66
- export class ListExpansion < T extends ExpansionItem > {
73
+ export class ListExpansion {
67
74
/** A signal holding an array of ids of the currently expanded items. */
68
75
expandedIds : WritableSignalLike < string [ ] > ;
69
76
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 ;
75
79
}
76
80
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 ) {
79
83
if ( ! this . isExpandable ( item ) ) return ;
80
84
if ( this . isExpanded ( item ) ) return ;
81
85
if ( ! this . inputs . multiExpandable ( ) ) {
@@ -84,18 +88,15 @@ export class ListExpansion<T extends ExpansionItem> {
84
88
this . expandedIds . update ( ids => ids . concat ( item . expansionId ( ) ) ) ;
85
89
}
86
90
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 ) {
89
93
if ( this . isExpandable ( item ) ) {
90
94
this . expandedIds . update ( ids => ids . filter ( id => id !== item . expansionId ( ) ) ) ;
91
95
}
92
96
}
93
97
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 ) {
99
100
this . expandedIds ( ) . includes ( item . expansionId ( ) ) ? this . close ( item ) : this . open ( item ) ;
100
101
}
101
102
@@ -116,12 +117,12 @@ export class ListExpansion<T extends ExpansionItem> {
116
117
}
117
118
118
119
/** Checks whether the specified item is expandable / collapsible. */
119
- isExpandable ( item : T ) {
120
+ isExpandable ( item : ExpansionItem ) {
120
121
return ! this . inputs . disabled ( ) && ! item . disabled ( ) && item . expandable ( ) ;
121
122
}
122
123
123
124
/** Checks whether the specified item is currently expanded. */
124
- isExpanded ( item : T ) : boolean {
125
+ isExpanded ( item : ExpansionItem ) : boolean {
125
126
return this . expandedIds ( ) . includes ( item . expansionId ( ) ) ;
126
127
}
127
128
}
0 commit comments