Skip to content

Commit 120c565

Browse files
committed
refactor(validation-container): Cached slot generation
1 parent 603846f commit 120c565

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/components/validation-container/validation-container.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { html, LitElement, nothing, type TemplateResult } from 'lit';
22
import { property, state } from 'lit/decorators.js';
3+
import { cache } from 'lit/directives/cache.js';
34
import { ifDefined } from 'lit/directives/if-defined.js';
45
import { addThemingController } from '../../theming/theming-controller.js';
56
import { registerComponent } from '../common/definitions/register.js';
@@ -25,6 +26,7 @@ interface ValidationContainerConfig {
2526

2627
const VALIDATION_SLOTS_SELECTOR = 'slot:not([name="helper-text"])';
2728
const ALL_SLOTS_SELECTOR = 'slot';
29+
const QUERY_CONFIG: AssignedNodesOptions = { flatten: true };
2830

2931
function getValidationSlots(
3032
element: IgcValidationContainerComponent
@@ -38,7 +40,7 @@ function hasProjection(element: IgcValidationContainerComponent): boolean {
3840
const allSlots =
3941
element.renderRoot.querySelectorAll<HTMLSlotElement>(ALL_SLOTS_SELECTOR);
4042
return Array.from(allSlots).every((slot) =>
41-
isEmpty(slot.assignedElements({ flatten: true }))
43+
isEmpty(slot.assignedElements(QUERY_CONFIG))
4244
);
4345
}
4446

@@ -47,15 +49,14 @@ function hasProjectedValidation(
4749
slotName?: string
4850
): boolean {
4951
const slots = Array.from(getValidationSlots(element));
50-
const config: AssignedNodesOptions = { flatten: true };
5152

5253
if (slotName) {
5354
return slots
5455
.filter((slot) => slot.name === slotName)
55-
.some((slot) => !isEmpty(slot.assignedElements(config)));
56+
.some((slot) => !isEmpty(slot.assignedElements(QUERY_CONFIG)));
5657
}
5758

58-
return slots.some((slot) => !isEmpty(slot.assignedElements(config)));
59+
return slots.some((slot) => !isEmpty(slot.assignedElements(QUERY_CONFIG)));
5960
}
6061

6162
/* blazorSuppress */
@@ -208,9 +209,9 @@ export default class IgcValidationContainerComponent extends LitElement {
208209
}
209210

210211
protected override render(): TemplateResult {
211-
const slots = this.invalid
212-
? this._renderValidationSlots(this.target.validity)
213-
: nothing;
212+
const slots = cache(
213+
this.invalid ? this._renderValidationSlots(this.target.validity) : nothing
214+
);
214215

215216
return html`
216217
<div part=${partMap({ 'helper-text': true, empty: hasProjection(this) })}>

0 commit comments

Comments
 (0)