Skip to content

Commit 0d2bf88

Browse files
fix: add utilities for accoridon root
1 parent b52edf9 commit 0d2bf88

File tree

28 files changed

+149
-124
lines changed

28 files changed

+149
-124
lines changed

.github/workflows/dependency-review.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
name: 'Dependency review'
1111
on:
1212
pull_request:
13-
branches: [ "main" ]
13+
branches: ['main']
1414

1515
# If using a dependency submission action in this workflow this permission will need to be set to:
1616
#

.github/workflows/greetings.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ jobs:
99
issues: write
1010
pull-requests: write
1111
steps:
12-
- uses: actions/first-interaction@v1
13-
with:
14-
repo-token: ${{ secrets.GITHUB_TOKEN }}
15-
issue-message: "Message that will be displayed on users' first issue"
16-
pr-message: "Message that will be displayed on users' first pull request"
12+
- uses: actions/first-interaction@v1
13+
with:
14+
repo-token: ${{ secrets.GITHUB_TOKEN }}
15+
issue-message: "Message that will be displayed on users' first issue"
16+
pr-message: "Message that will be displayed on users' first pull request"

apps/playground/src/app/app.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
>
1414
Collapse All
1515
</button>
16+
<button
17+
(click)="accordionRoot.toogleAllValue()"
18+
class="bg-yellow-500 text-white py-2 px-4 rounded-md hover:bg-yellow-600 focus:outline-none focus:ring-2 focus:ring-yellow-400"
19+
>
20+
Toogle All
21+
</button>
1622
</div>
1723
<ngx-primer-accordion-root
1824
#accordionRoot
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
<ng-content>
2-
3-
</ng-content>
1+
<ng-content> </ng-content>

packages/primitives/accordion/src/components/accordion-content/accordion-content.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import { NgxPrimerIdGeneratorDirective } from '@ngx-primer/primitive/utilities';
3737
inputs: ['ngxPrimerIdAttr'],
3838
},
3939
{
40-
directive: NgxPrimerAccordionContentContextDirective
41-
}
40+
directive: NgxPrimerAccordionContentContextDirective,
41+
},
4242
],
4343
})
4444
export class NgxPrimerAccordionContentComponent<T> implements OnInit {
@@ -95,7 +95,7 @@ export class NgxPrimerAccordionContentComponent<T> implements OnInit {
9595
protected runInitializationFn(doneFn?: <P>(args?: P) => void): void {
9696
if (doneFn) {
9797
doneFn({
98-
context: this.accordionContentContext
98+
context: this.accordionContentContext,
9999
});
100100
}
101101
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<ng-content>
2-
</ng-content>
1+
<ng-content> </ng-content>

packages/primitives/accordion/src/components/accordion-item/accordion-item.component.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import { NgxPrimerIdGeneratorDirective } from '@ngx-primer/primitive/utilities';
4747
inputs: ['ngxPrimerIdAttr'],
4848
},
4949
{
50-
directive: NgxPrimerAccordionItemContextDirective
51-
}
50+
directive: NgxPrimerAccordionItemContextDirective,
51+
},
5252
],
5353
})
5454
export class NgxPrimerAccordionItemComponent<T> implements OnInit {
@@ -91,16 +91,15 @@ export class NgxPrimerAccordionItemComponent<T> implements OnInit {
9191
}
9292
);
9393

94-
public readonly value = model<T|null>(null, {
94+
public readonly value = model<T | null>(null, {
9595
alias: 'ngxPrimerAccordionItemValue',
9696
});
9797

98-
9998
/**
10099
* Whether the accordion item is disabled.
101100
*/
102101
public readonly disabled = model(false, {
103-
alias: 'ngxPrimerAccordionItemDisabled'
102+
alias: 'ngxPrimerAccordionItemDisabled',
104103
});
105104

106105
public readonly isOpen = computed<boolean>(() =>
@@ -168,9 +167,11 @@ export class NgxPrimerAccordionItemComponent<T> implements OnInit {
168167
protected runInitializationFn(doneFn?: <P>(args?: P) => void): void {
169168
if (doneFn) {
170169
// ensure context being initalized
171-
setTimeout(() => doneFn({
172-
context: this.accordionItemContext
173-
}));
170+
setTimeout(() =>
171+
doneFn({
172+
context: this.accordionItemContext,
173+
})
174+
);
174175
}
175176
}
176177

@@ -184,7 +185,7 @@ export class NgxPrimerAccordionItemComponent<T> implements OnInit {
184185
?.instance as NgxPrimerAccordionRootComponent<T>;
185186
}
186187

187-
focus(){
188+
focus() {
188189
this.accordionTrigger()?.focus();
189190
}
190191
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<ng-content>
2-
</ng-content>
1+
<ng-content> </ng-content>

packages/primitives/accordion/src/components/accordion-root/accordion-root.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('NgxPrimerAccordionRootComponent', () => {
5858

5959
for (const key in config) {
6060
const keyName = key as keyof typeof config;
61-
if(typeof config[keyName] === 'function') return; // skip function;
61+
if (typeof config[keyName] === 'function') return; // skip function;
6262
expect(component.accordionConfig[keyName]).toEqual(config[keyName]);
6363
}
6464
});

packages/primitives/accordion/src/components/accordion-root/accordion-root.component.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
569569
this.toggleAll(false);
570570
}
571571

572+
public toogleAllValue() {
573+
this.toggleAll();
574+
}
575+
572576
/**
573577
* Toggles the open/closed state of all accordion items based on the provided `isOpen` value.
574578
*
@@ -583,20 +587,18 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
583587
* - `false`: Collapses all accordion items.
584588
* @returns {void} This method does not return anything. It only updates the open/closed state of all items.
585589
*/
586-
private toggleAll(isOpen: boolean) {
590+
private toggleAll(isOpen?: boolean) {
587591
if (this.type() === 'Single' || this.disabled()) return;
588592

589593
this.accordionItems().forEach(({ disabled, value }) => {
590-
const isItemDisabled = disabled();
591-
if (isItemDisabled) return;
592-
this.toogleMultiple(value(), !isOpen);
594+
if (disabled()) return;
595+
this.toogleMultiple(value(), isOpen ?? this.isOpen(value()));
593596
});
594597
}
595598

596-
597599
/**
598600
* Expands the accordion with the given value(s).
599-
*
601+
*
600602
* If the value is currently collapsed, it will be expanded.
601603
* If the value is currently expanded, it will remain expanded.
602604
*
@@ -608,7 +610,7 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
608610

609611
/**
610612
* Collapses the specified value(s) in the accordion.
611-
*
613+
*
612614
* If the value is currently expanded, it will be collapsed.
613615
* If the value is currently collapsed, it will remain collapsed.
614616
*
@@ -618,13 +620,12 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
618620
this.toggleValue(value, false);
619621
}
620622

621-
622623
/**
623624
* Toggles the expansion state of the given value(s).
624-
*
625+
*
625626
* If the value is currently collapsed, it will be expanded.
626627
* If the value is currently expanded, it will be collapsed.
627-
*
628+
*
628629
* @protected
629630
* @param {T | T[]} value - The value or array of values to toggle.
630631
* @param {boolean} isExpanding - A flag indicating whether to expand (true) or collapse (false) the value(s).
@@ -649,7 +650,7 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
649650

650651
/**
651652
* Enables the specified value(s) by updating their disable state.
652-
*
653+
*
653654
* If the value is currently disabled, it will be enabled.
654655
* If the value is currently enabled, it will remain enabled.
655656
*
@@ -663,10 +664,10 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
663664

664665
/**
665666
* Disables the specified value(s) by updating their disable state.
666-
*
667+
*
667668
* If the value is currently enabled, it will be disabled.
668669
* If the value is currently disabled, it will remain disabled.
669-
*
670+
*
670671
* @public
671672
* @method
672673
* @param value - The value or array of values to be disabled.
@@ -678,28 +679,33 @@ export class NgxPrimerAccordionRootComponent<T> implements OnInit {
678679

679680
/**
680681
* Updates the disable state of the specified value(s).
681-
*
682+
*
682683
* If the value is currently disabled, it will be enabled.
683684
* If the value is currently enabled, it will be disabled.
684-
*
685+
*
685686
* @protected
686687
* @param {T | T[]} value - The value or array of values to update.
687688
* @param {boolean} enable - A flag indicating whether to enable (true) or disable (false) the value(s).
688689
*/
689690
protected updateDisableState(value: T | T[], enable: boolean) {
690691
const values = Array.isArray(value) ? value : [value];
691-
692-
const accordionItems = this.accordionItems().filter((item) => values.includes(item.value()));
693-
694-
const update = (item: NgxPrimerAccordionItemComponent<T>, enable: boolean) => {
692+
693+
const accordionItems = this.accordionItems().filter((item) =>
694+
values.includes(item.value())
695+
);
696+
697+
const update = (
698+
item: NgxPrimerAccordionItemComponent<T>,
699+
enable: boolean
700+
) => {
695701
const isDisabled = item.disabled();
696702
const shouldDisable = !enable;
697-
703+
698704
if (isDisabled !== shouldDisable) {
699705
item.disabled.set(shouldDisable ? true : false);
700706
}
701707
};
702-
708+
703709
accordionItems.forEach((item) => update(item, enable));
704710
}
705711
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<ng-content>
2-
</ng-content>
1+
<ng-content> </ng-content>

packages/primitives/accordion/src/components/accordion-trigger/accordion-trigger.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ export class NgxPrimerAccordionTriggerComponent<T> implements OnInit {
235235
}
236236

237237
@HostListener('click')
238-
toogle() {
239-
if (this.accordionRoot?.disabled() || this.accordionItem?.disabled()) return;
238+
public toogle() {
239+
if (this.accordionRoot?.disabled() || this.accordionItem?.disabled())
240+
return;
240241
this.accordionRoot?.toggle(this.accordionItem?.value() as T);
241242
}
242243

@@ -326,8 +327,9 @@ export class NgxPrimerAccordionTriggerComponent<T> implements OnInit {
326327
?.instance as NgxPrimerAccordionContentComponent<T>;
327328
}
328329

329-
focus() {
330-
if(this.accordionRoot?.disabled() || this.accordionItem?.disabled()) return;
330+
public focus() {
331+
if (this.accordionRoot?.disabled() || this.accordionItem?.disabled())
332+
return;
331333
(this.viewContainerRef.element.nativeElement as HTMLElement).focus({
332334
preventScroll: false,
333335
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './accordion-root/accordion-root.component';
22
export * from './accordion-item/accordion-item.component';
33
export * from './accordion-content/accordion-content.component';
4-
export * from './accordion-trigger/accordion-trigger.component';
4+
export * from './accordion-trigger/accordion-trigger.component';

packages/primitives/accordion/src/configs/accordion-config.spec.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { NgxPrimerAccordionConfig, NgxPrimerAccordionOrientation, NgxPrimerAccordionType, defaultAccordionConfig, provideAccordionConfig } from './accordion-config';
1+
import {
2+
NgxPrimerAccordionConfig,
3+
NgxPrimerAccordionOrientation,
4+
NgxPrimerAccordionType,
5+
defaultAccordionConfig,
6+
provideAccordionConfig,
7+
} from './accordion-config';
28

39
import { NgxPrimerAccordionConfigToken } from '../tokens/accordion-config.token';
410
import { TestBed } from '@angular/core/testing';
@@ -52,10 +58,12 @@ describe('AccordionConfig', () => {
5258

5359
it('should provide custom config', () => {
5460
TestBed.configureTestingModule({
55-
providers: [provideAccordionConfig({
56-
type: NgxPrimerAccordionType.Single,
57-
collapsible: false,
58-
})],
61+
providers: [
62+
provideAccordionConfig({
63+
type: NgxPrimerAccordionType.Single,
64+
collapsible: false,
65+
}),
66+
],
5967
});
6068
const injectedConfig = TestBed.inject(NgxPrimerAccordionConfigToken);
6169
expect(injectedConfig.type).toBe(NgxPrimerAccordionType.Single);
@@ -69,7 +77,9 @@ describe('AccordionConfig', () => {
6977
const injectedConfig = TestBed.inject(NgxPrimerAccordionConfigToken);
7078
expect(injectedConfig.type).toBe(NgxPrimerAccordionType.Multiple);
7179
expect(injectedConfig.collapsible).toBe(true);
72-
expect(injectedConfig.orientation).toBe(NgxPrimerAccordionOrientation.Vertical);
80+
expect(injectedConfig.orientation).toBe(
81+
NgxPrimerAccordionOrientation.Vertical
82+
);
7383
expect(injectedConfig.theme.builtIn).toBe(true);
7484
});
75-
});
85+
});

packages/primitives/accordion/src/configs/accordion-config.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ export interface NgxPrimerAccordionConfig {
2020
builtIn: boolean;
2121
};
2222
// Overloaded method signatures
23-
updateConfig(callbackFn: (config: NgxPrimerAccordionConfig) => Partial<NgxPrimerAccordionConfig>): void;
23+
updateConfig(
24+
callbackFn: (
25+
config: NgxPrimerAccordionConfig
26+
) => Partial<NgxPrimerAccordionConfig>
27+
): void;
2428
updateConfig(newConfig: Partial<NgxPrimerAccordionConfig>): void;
25-
resetConfig(target: NgxPrimerAccordionConfig): void
29+
resetConfig(target: NgxPrimerAccordionConfig): void;
2630
}
2731
export const defaultAccordionConfig: () => NgxPrimerAccordionConfig = () => ({
2832
type: 'Multiple',
@@ -31,13 +35,20 @@ export const defaultAccordionConfig: () => NgxPrimerAccordionConfig = () => ({
3135
theme: {
3236
builtIn: true,
3337
},
34-
updateConfig(callbackFn: Partial<NgxPrimerAccordionConfig> | ((config: NgxPrimerAccordionConfig) => Partial<NgxPrimerAccordionConfig>)): void {
35-
const updatedConfig = typeof callbackFn === 'function' ? callbackFn(this) : {...callbackFn};
36-
Object.assign(this, {...updatedConfig});
38+
updateConfig(
39+
callbackFn:
40+
| Partial<NgxPrimerAccordionConfig>
41+
| ((
42+
config: NgxPrimerAccordionConfig
43+
) => Partial<NgxPrimerAccordionConfig>)
44+
): void {
45+
const updatedConfig =
46+
typeof callbackFn === 'function' ? callbackFn(this) : { ...callbackFn };
47+
Object.assign(this, { ...updatedConfig });
3748
},
3849
resetConfig(target: NgxPrimerAccordionConfig): void {
3950
Object.assign(target, defaultAccordionConfig());
40-
}
51+
},
4152
});
4253
export function provideAccordionConfig(
4354
config: Partial<NgxPrimerAccordionConfig>

0 commit comments

Comments
 (0)