Răsfoiți Sursa

refactor(admin-ui): Use common ng-select selection strategy

Michael Bromley 4 ani în urmă
părinte
comite
62bac9275a

+ 2 - 4
packages/admin-ui/src/lib/catalog/src/components/product-search-input/product-search-input.component.ts

@@ -1,16 +1,14 @@
 import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, ViewChild } from '@angular/core';
 import { NgSelectComponent, SELECTION_MODEL_FACTORY } from '@ng-select/ng-select';
-import { SearchProducts } from '@vendure/admin-ui/core';
+import { SearchProducts, SingleSearchSelectionModelFactory } from '@vendure/admin-ui/core';
 import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
 
-import { ProductSearchSelectionModelFactory } from './product-search-selection-model';
-
 @Component({
     selector: 'vdr-product-search-input',
     templateUrl: './product-search-input.component.html',
     styleUrls: ['./product-search-input.component.scss'],
     changeDetection: ChangeDetectionStrategy.OnPush,
-    providers: [{ provide: SELECTION_MODEL_FACTORY, useValue: ProductSearchSelectionModelFactory }],
+    providers: [{ provide: SELECTION_MODEL_FACTORY, useValue: SingleSearchSelectionModelFactory }],
 })
 export class ProductSearchInputComponent {
     @Input() facetValueResults: SearchProducts.FacetValues[];

+ 0 - 56
packages/admin-ui/src/lib/catalog/src/components/product-search-input/product-search-selection-model.ts

@@ -1,56 +0,0 @@
-import { NgOption, SelectionModel } from '@ng-select/ng-select';
-
-/**
- * A custom SelectionModel for the NgSelect component which only allows a single
- * search term at a time.
- */
-export class ProductSearchSelectionModel implements SelectionModel {
-    private _selected: NgOption[] = [];
-
-    get value(): NgOption[] {
-        return this._selected;
-    }
-
-    select(item: NgOption, multiple: boolean, groupAsModel: boolean) {
-        item.selected = true;
-        if (groupAsModel || !item.children) {
-            if ((item.value as any).label) {
-                const isSearchTerm = (i: any) => !!i.value.label;
-                const searchTerms = this._selected.filter(isSearchTerm);
-                if (searchTerms.length > 0) {
-                    // there is already a search term, so replace it with this new one.
-                    this._selected = this._selected.filter(i => !isSearchTerm(i)).concat(item);
-                } else {
-                    this._selected.push(item);
-                }
-            } else {
-                this._selected.push(item);
-            }
-        }
-    }
-
-    unselect(item: NgOption, multiple: boolean) {
-        this._selected = this._selected.filter(x => x !== item);
-        item.selected = false;
-    }
-
-    clear(keepDisabled: boolean) {
-        this._selected = keepDisabled ? this._selected.filter(x => x.disabled) : [];
-    }
-
-    private _setChildrenSelectedState(children: NgOption[], selected: boolean) {
-        children.forEach(x => (x.selected = selected));
-    }
-
-    private _removeChildren(parent: NgOption) {
-        this._selected = this._selected.filter(x => x.parent !== parent);
-    }
-
-    private _removeParent(parent: NgOption) {
-        this._selected = this._selected.filter(x => x !== parent);
-    }
-}
-
-export function ProductSearchSelectionModelFactory() {
-    return new ProductSearchSelectionModel();
-}

+ 0 - 1
packages/admin-ui/src/lib/catalog/src/public_api.ts

@@ -19,7 +19,6 @@ export * from './components/product-assets/product-assets.component';
 export * from './components/product-detail/product-detail.component';
 export * from './components/product-list/product-list.component';
 export * from './components/product-search-input/product-search-input.component';
-export * from './components/product-search-input/product-search-selection-model';
 export * from './components/product-variants-editor/product-variants-editor.component';
 export * from './components/product-variants-list/product-variants-list.component';
 export * from './components/product-variants-table/product-variants-table.component';

+ 1 - 0
packages/admin-ui/src/lib/core/src/public_api.ts

@@ -12,6 +12,7 @@ export * from './common/generated-types';
 export * from './common/introspection-result-wrapper';
 export * from './common/introspection-result';
 export * from './common/language-translation-strings';
+export * from './common/single-search-selection-model';
 export * from './common/utilities/configurable-operation-utils';
 export * from './common/utilities/create-updated-translatable';
 export * from './common/utilities/find-translation';