From ead64cdf8d917fa0d6a20271e42378f38e5f2407 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Feb 2021 16:35:48 +0100 Subject: Support custom value in ng-select --- client/src/app/shared/shared-forms/select/index.ts | 3 +- .../select/select-custom-input.component.html | 14 ---- .../select/select-custom-input.component.scss | 0 .../select/select-custom-input.component.ts | 44 ------------- .../select/select-custom-value.component.html | 14 ++++ .../select/select-custom-value.component.ts | 76 ++++++++++++++++++++++ .../select/select-options.component.html | 1 + .../select/select-options.component.ts | 1 + .../select/select-shared.component.scss | 15 +++++ 9 files changed, 109 insertions(+), 59 deletions(-) delete mode 100644 client/src/app/shared/shared-forms/select/select-custom-input.component.html delete mode 100644 client/src/app/shared/shared-forms/select/select-custom-input.component.scss delete mode 100644 client/src/app/shared/shared-forms/select/select-custom-input.component.ts create mode 100644 client/src/app/shared/shared-forms/select/select-custom-value.component.html create mode 100644 client/src/app/shared/shared-forms/select/select-custom-value.component.ts (limited to 'client/src/app/shared/shared-forms/select') diff --git a/client/src/app/shared/shared-forms/select/index.ts b/client/src/app/shared/shared-forms/select/index.ts index 33459b23b..e387e1f48 100644 --- a/client/src/app/shared/shared-forms/select/index.ts +++ b/client/src/app/shared/shared-forms/select/index.ts @@ -1,4 +1,5 @@ export * from './select-channel.component' +export * from './select-checkbox.component' +export * from './select-custom-value.component' export * from './select-options.component' export * from './select-tags.component' -export * from './select-checkbox.component' diff --git a/client/src/app/shared/shared-forms/select/select-custom-input.component.html b/client/src/app/shared/shared-forms/select/select-custom-input.component.html deleted file mode 100644 index 84fa15c3d..000000000 --- a/client/src/app/shared/shared-forms/select/select-custom-input.component.html +++ /dev/null @@ -1,14 +0,0 @@ - - - {{ channel.label }} - - - - diff --git a/client/src/app/shared/shared-forms/select/select-custom-input.component.scss b/client/src/app/shared/shared-forms/select/select-custom-input.component.scss deleted file mode 100644 index e69de29bb..000000000 diff --git a/client/src/app/shared/shared-forms/select/select-custom-input.component.ts b/client/src/app/shared/shared-forms/select/select-custom-input.component.ts deleted file mode 100644 index ba6fef8ad..000000000 --- a/client/src/app/shared/shared-forms/select/select-custom-input.component.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Component, forwardRef, Input } from '@angular/core' -import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' - -@Component({ - selector: 'my-select-custom-input', - styleUrls: [ './select-custom-input.component.scss' ], - templateUrl: './select-custom-input.component.html', - providers: [ - { - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => SelectCustomInputComponent), - multi: true - } - ] -}) -export class SelectCustomInputComponent implements ControlValueAccessor { - @Input() items: any[] = [] - - selectedId: number - - // ng-select options - bindLabel = 'label' - bindValue = 'id' - clearable = false - searchable = false - - propagateChange = (_: any) => { /* empty */ } - - writeValue (id: number) { - this.selectedId = id - } - - registerOnChange (fn: (_: any) => void) { - this.propagateChange = fn - } - - registerOnTouched () { - // Unused - } - - onModelChange () { - this.propagateChange(this.selectedId) - } -} diff --git a/client/src/app/shared/shared-forms/select/select-custom-value.component.html b/client/src/app/shared/shared-forms/select/select-custom-value.component.html new file mode 100644 index 000000000..5fdf432ff --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-custom-value.component.html @@ -0,0 +1,14 @@ +
+ + + +
diff --git a/client/src/app/shared/shared-forms/select/select-custom-value.component.ts b/client/src/app/shared/shared-forms/select/select-custom-value.component.ts new file mode 100644 index 000000000..a8e5ad0d3 --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-custom-value.component.ts @@ -0,0 +1,76 @@ +import { Component, forwardRef, Input, OnChanges } from '@angular/core' +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' +import { SelectOptionsItem } from './select-options.component' + +@Component({ + selector: 'my-select-custom-value', + styleUrls: [ './select-shared.component.scss' ], + templateUrl: './select-custom-value.component.html', + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => SelectCustomValueComponent), + multi: true + } + ] +}) +export class SelectCustomValueComponent implements ControlValueAccessor, OnChanges { + @Input() items: SelectOptionsItem[] = [] + @Input() clearable = false + @Input() searchable = false + @Input() groupBy: string + @Input() labelForId: string + + customValue: number | string = '' + selectedId: number | string + + itemsWithCustom: SelectOptionsItem[] = [] + + ngOnChanges () { + this.itemsWithCustom = this.getItems() + } + + propagateChange = (_: any) => { /* empty */ } + + writeValue (id: number | string) { + this.selectedId = id + + if (this.isSelectedIdInItems() !== true) { + this.selectedId = 'other' + this.customValue = id + } + } + + registerOnChange (fn: (_: any) => void) { + this.propagateChange = fn + } + + registerOnTouched () { + // Unused + } + + onModelChange () { + if (this.selectedId === 'other') { + return this.propagateChange(this.customValue) + } + + return this.propagateChange(this.selectedId) + } + + isSelectedIdInItems () { + return !!this.items.find(i => i.id === this.selectedId) + } + + getItems () { + const other: SelectOptionsItem = { + id: 'other', + label: $localize`Custom value...` + } + + return this.items.concat([ other ]) + } + + isCustomValue () { + return this.selectedId === 'other' + } +} diff --git a/client/src/app/shared/shared-forms/select/select-options.component.html b/client/src/app/shared/shared-forms/select/select-options.component.html index 6d0d7e925..3b1761255 100644 --- a/client/src/app/shared/shared-forms/select/select-options.component.html +++ b/client/src/app/shared/shared-forms/select/select-options.component.html @@ -6,6 +6,7 @@ [clearable]="clearable" [labelForId]="labelForId" [searchable]="searchable" + [searchFn]="searchFn" bindLabel="label" bindValue="id" diff --git a/client/src/app/shared/shared-forms/select/select-options.component.ts b/client/src/app/shared/shared-forms/select/select-options.component.ts index f0abd1a68..51a3f515d 100644 --- a/client/src/app/shared/shared-forms/select/select-options.component.ts +++ b/client/src/app/shared/shared-forms/select/select-options.component.ts @@ -27,6 +27,7 @@ export class SelectOptionsComponent implements ControlValueAccessor { @Input() searchable = false @Input() groupBy: string @Input() labelForId: string + @Input() searchFn: any selectedId: number | string diff --git a/client/src/app/shared/shared-forms/select/select-shared.component.scss b/client/src/app/shared/shared-forms/select/select-shared.component.scss index 0b4c6b784..1a4192b55 100644 --- a/client/src/app/shared/shared-forms/select/select-shared.component.scss +++ b/client/src/app/shared/shared-forms/select/select-shared.component.scss @@ -30,3 +30,18 @@ ng-select ::ng-deep { width: 20px; } } + +.root { + display:flex; + + > my-select-options { + flex-grow: 1; + } +} + +input[type=text] { + margin-left: 5px; + + @include peertube-input-text($form-base-input-width); + display: block; +} -- cgit v1.2.3