-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/select): support for angular forms (#738)
Co-authored-by: Tiago Viegas <tiago.viegas@siemens.com>
- Loading branch information
1 parent
e3c7193
commit c0bb78f
Showing
15 changed files
with
286 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/angular-test-app/src/preview-examples/select-ng-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-example', | ||
template: ` | ||
<ix-select name="ix-select-control" [(ngModel)]="value"> | ||
<ix-select-item label="Item 1" value="1"></ix-select-item> | ||
<ix-select-item label="Item 2" value="2"></ix-select-item> | ||
<ix-select-item label="Item 3" value="3"></ix-select-item> | ||
<ix-select-item label="Item 4" value="4"></ix-select-item> | ||
</ix-select> | ||
`, | ||
}) | ||
export default class Select { | ||
value = '1'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Directive, ElementRef } from '@angular/core'; | ||
import { NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
|
||
import { ValueAccessor } from './value-accessor'; | ||
|
||
@Directive({ | ||
/* tslint:disable-next-line:directive-selector */ | ||
selector: 'ix-select[ngModel],ix-select[formControlName],ix-select[formControl]', | ||
host: { | ||
'(valueChange)': 'handleChangeEvent($event.target.value)' | ||
}, | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: SelectValueAccessor, | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class SelectValueAccessor extends ValueAccessor { | ||
constructor(el: ElementRef) { | ||
super(el); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Directive, ElementRef, HostListener } from '@angular/core'; | ||
import { ControlValueAccessor } from '@angular/forms'; | ||
|
||
@Directive({}) | ||
export class ValueAccessor implements ControlValueAccessor { | ||
|
||
private onChange: (value: any) => void = () => {/**/}; | ||
private onTouched: () => void = () => {/**/}; | ||
protected lastValue: any; | ||
|
||
constructor(protected el: ElementRef) {} | ||
|
||
writeValue(value: any) { | ||
this.el.nativeElement.value = this.lastValue = value == null ? '' : value; | ||
} | ||
|
||
handleChangeEvent(value: any) { | ||
if (value !== this.lastValue) { | ||
this.lastValue = value; | ||
this.onChange(value); | ||
} | ||
} | ||
|
||
@HostListener('focusout') | ||
_handleBlurEvent() { | ||
this.onTouched(); | ||
} | ||
|
||
registerOnChange(fn: (value: any) => void) { | ||
this.onChange = fn; | ||
} | ||
registerOnTouched(fn: () => void) { | ||
this.onTouched = fn; | ||
} | ||
|
||
setDisabledState(isDisabled: boolean) { | ||
this.el.nativeElement.disabled = isDisabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.