Skip to content

Commit

Permalink
feat: 支持angular表单
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Jun 12, 2019
1 parent 93536aa commit 9d7d77a
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions projects/select/src/lib/select.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { Component, OnInit, Input, QueryList, ContentChildren, AfterContentInit, OnDestroy, EventEmitter, Output } from '@angular/core';
import {
Component,
OnInit,
Input,
QueryList,
ContentChildren,
AfterContentInit,
OnDestroy, EventEmitter,
Output,
forwardRef,
ChangeDetectionStrategy,
ChangeDetectorRef
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS } from '@angular/forms';

import { startWith, switchMap, filter } from 'rxjs/operators';
import { Subject, merge, Subscription, fromEvent } from 'rxjs';

import { OptionComponent, DamingOptionSelectionChange } from './option/option.component';


@Component({
selector: 'daming-select',
templateUrl: './select.component.html',
styleUrls: ['./select.component.scss']
styleUrls: ['./select.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectComponent),
multi: true,
},
// {
// provide: NG_VALIDATORS,
// useExisting: forwardRef(() => SelectComponent),
// multi: true,
// }
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SelectComponent implements OnInit, OnDestroy, AfterContentInit {
export class SelectComponent implements OnInit, OnDestroy, AfterContentInit, ControlValueAccessor {

// tslint:disable-next-line: variable-name
private readonly _destroy = new Subject<void>();
Expand All @@ -32,9 +59,10 @@ export class SelectComponent implements OnInit, OnDestroy, AfterContentInit {
public placeHolder: string;

@Input()
public set value(_value: string) {
if (_value && _value !== this._value) {
this._value = _value;
public set value(newValue: string) {
if (newValue && newValue !== this._value) {
this._value = newValue;
this._changeDetectorRef.markForCheck();
}
}

Expand All @@ -50,7 +78,14 @@ export class SelectComponent implements OnInit, OnDestroy, AfterContentInit {
return this._open;
}

constructor() {
// tslint:disable-next-line: variable-name
private _onChange: (value: any) => void = () => {};
// tslint:disable-next-line: variable-name
private _onTouched = () => {};

constructor(
private _changeDetectorRef: ChangeDetectorRef
) {
}

ngOnInit() {
Expand All @@ -72,10 +107,26 @@ export class SelectComponent implements OnInit, OnDestroy, AfterContentInit {
).subscribe((option: DamingOptionSelectionChange) => {
this.value = option.value;
this.open = false;
this.valueChange.next(this.value);
this._onChange(this.value);
this.valueChange.emit(this.value);
});
}

writeValue(value: string): void {
this.value = value;
}
registerOnChange(fn: (value: any) => void): void {
this._onChange = fn;
}

registerOnTouched(fn: () => void): void {
this._onTouched = fn;
}

setDisabledState?(isDisabled: boolean): void {

}

toggle(event) {
event.stopPropagation();
this.open = !this.open;
Expand Down

0 comments on commit 9d7d77a

Please sign in to comment.