Revert "feat: enhance variant attribute handling across components for improved selection and display"

This reverts commit 56f602d876.
This commit is contained in:
2026-08-10 16:17:09 -03:00
parent c645feba80
commit 0769bb8683
9 changed files with 35 additions and 191 deletions

View File

@@ -38,22 +38,4 @@ describe('VariantSelectorComponent', () => {
servicio: 'Cena',
});
});
it('renders labels while matching variants by value', async () => {
await TestBed.configureTestingModule({
imports: [VariantSelectorComponent],
}).compileComponents();
const fixture = TestBed.createComponent(VariantSelectorComponent);
fixture.componentRef.setInput('variants', [
{
value: 1,
values: { event_date: { value: '2', label: '10/10/2026' } },
},
]);
fixture.detectChanges();
const option = fixture.nativeElement.querySelector('option') as HTMLOptionElement;
expect(option.textContent?.trim()).toBe('10/10/2026');
expect((fixture.componentInstance as any).selectedValues()).toEqual({ event_date: '2' });
});
});

View File

@@ -9,9 +9,8 @@ import {
untracked,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { VariantAttributeValue } from '../../../core/services/catalog/catalog.interface';
type VariantSelectionValue = string | string[];
export type VariantAttributeValue = string | string[];
export interface VariantSelectorVariant {
value: unknown;
@@ -21,7 +20,7 @@ export interface VariantSelectorVariant {
interface VariantSelectorOption {
key: string;
label: string;
value: VariantSelectionValue;
value: VariantAttributeValue;
}
interface VariantSelectorGroup {
@@ -44,7 +43,7 @@ export class VariantSelectorComponent {
readonly disabled = input(false);
readonly compact = input(false);
protected readonly selectedValues = signal<Record<string, VariantSelectionValue>>({});
protected readonly selectedValues = signal<Record<string, VariantAttributeValue>>({});
protected readonly attributeKeys = computed(() =>
Array.from(new Set(this.variants().flatMap((variant) => Object.keys(variant.values)))),
);
@@ -57,10 +56,7 @@ export class VariantSelectorComponent {
const previousKeys = keys.slice(0, index);
const compatibleVariants = variants.filter((variant) =>
previousKeys.every((previousKey) =>
this.sameValue(
this.selectionValue(variant.values[previousKey]),
selectedValues[previousKey],
),
this.sameValue(variant.values[previousKey], selectedValues[previousKey]),
),
);
@@ -86,13 +82,13 @@ export class VariantSelectorComponent {
const selected =
variants.find((variant) => Object.is(variant.value, selectedVariant)) ?? variants[0];
this.selectedValues.set(this.selectionValues(selected.values));
this.selectedValues.set({ ...selected.values });
if (!Object.is(selected.value, selectedVariant)) this.selectedVariant.set(selected.value);
});
});
}
protected onValueChange(key: string, value: VariantSelectionValue): void {
protected onValueChange(key: string, value: VariantAttributeValue): void {
const variants = this.variants();
const keys = this.attributeKeys();
const changedIndex = keys.indexOf(key);
@@ -103,7 +99,7 @@ export class VariantSelectorComponent {
const previousKeys = keys.slice(0, index);
const compatibleVariants = variants.filter((variant) =>
previousKeys.every((previousKey) =>
this.sameValue(this.selectionValue(variant.values[previousKey]), values[previousKey]),
this.sameValue(variant.values[previousKey], values[previousKey]),
),
);
const options = this.optionsFor(compatibleVariants, currentKey);
@@ -117,7 +113,7 @@ export class VariantSelectorComponent {
const matchingVariant = variants.find((variant) =>
keys.every((attributeKey) =>
this.sameValue(this.selectionValue(variant.values[attributeKey]), values[attributeKey]),
this.sameValue(variant.values[attributeKey], values[attributeKey]),
),
);
@@ -129,19 +125,14 @@ export class VariantSelectorComponent {
const options = new Map<string, VariantSelectorOption>();
for (const variant of variants) {
const attributeValue = variant.values[key];
if (attributeValue === undefined) continue;
const value = this.selectionValue(attributeValue);
if (value === undefined || value === '' || (Array.isArray(value) && value.length === 0)) {
continue;
}
const value = variant.values[key];
if (value === undefined || value === '') continue;
const optionKey = this.valueKey(value);
if (!options.has(optionKey)) {
options.set(optionKey, {
key: optionKey,
label: this.selectionLabel(attributeValue),
label: Array.isArray(value) ? value.join(', ') : value,
value,
});
}
@@ -151,48 +142,18 @@ export class VariantSelectorComponent {
}
private sameValue(
left: VariantSelectionValue | undefined,
right: VariantSelectionValue | undefined,
left: VariantAttributeValue | undefined,
right: VariantAttributeValue | undefined,
): boolean {
return (
left !== undefined && right !== undefined && this.valueKey(left) === this.valueKey(right)
);
}
private valueKey(value: VariantSelectionValue): string {
private valueKey(value: VariantAttributeValue): string {
return JSON.stringify(value);
}
private selectionValues(
values: Record<string, VariantAttributeValue>,
): Record<string, VariantSelectionValue> {
return Object.fromEntries(
Object.entries(values).flatMap(([key, value]) => {
const selectionValue = this.selectionValue(value);
return selectionValue === undefined ? [] : [[key, selectionValue]];
}),
);
}
private selectionValue(
value: VariantAttributeValue | undefined,
): VariantSelectionValue | undefined {
if (value === undefined) return undefined;
if (typeof value === 'string') return value;
if (Array.isArray(value)) {
return value.map((item) => (typeof item === 'string' ? item : item.value));
}
return value.value;
}
private selectionLabel(value: VariantAttributeValue): string {
if (typeof value === 'string') return value;
if (Array.isArray(value)) {
return value.map((item) => (typeof item === 'string' ? item : item.label)).join(', ');
}
return value.label;
}
private formatVariantLabel(key: string): string {
const label = key.replace(/[_-]+/g, ' ');
return label.charAt(0).toUpperCase() + label.slice(1);