fix(forms): Update types for TypeScript nullability support
This reverts commit 6d930d2fc5
.
This commit is contained in:

committed by
Tobias Bosch

parent
bc431888f3
commit
57bc245cb4
@ -18,45 +18,49 @@ import {ValidationErrors} from './validators';
|
||||
* @stable
|
||||
*/
|
||||
export abstract class AbstractControlDirective {
|
||||
get control(): AbstractControl { throw new Error('unimplemented'); }
|
||||
abstract get control(): AbstractControl|null;
|
||||
|
||||
get value(): any { return this.control ? this.control.value : null; }
|
||||
|
||||
get valid(): boolean { return this.control ? this.control.valid : null; }
|
||||
get valid(): boolean|null { return this.control ? this.control.valid : null; }
|
||||
|
||||
get invalid(): boolean { return this.control ? this.control.invalid : null; }
|
||||
get invalid(): boolean|null { return this.control ? this.control.invalid : null; }
|
||||
|
||||
get pending(): boolean { return this.control ? this.control.pending : null; }
|
||||
get pending(): boolean|null { return this.control ? this.control.pending : null; }
|
||||
|
||||
get errors(): ValidationErrors|null { return this.control ? this.control.errors : null; }
|
||||
|
||||
get pristine(): boolean { return this.control ? this.control.pristine : null; }
|
||||
get pristine(): boolean|null { return this.control ? this.control.pristine : null; }
|
||||
|
||||
get dirty(): boolean { return this.control ? this.control.dirty : null; }
|
||||
get dirty(): boolean|null { return this.control ? this.control.dirty : null; }
|
||||
|
||||
get touched(): boolean { return this.control ? this.control.touched : null; }
|
||||
get touched(): boolean|null { return this.control ? this.control.touched : null; }
|
||||
|
||||
get untouched(): boolean { return this.control ? this.control.untouched : null; }
|
||||
get untouched(): boolean|null { return this.control ? this.control.untouched : null; }
|
||||
|
||||
get disabled(): boolean { return this.control ? this.control.disabled : null; }
|
||||
get disabled(): boolean|null { return this.control ? this.control.disabled : null; }
|
||||
|
||||
get enabled(): boolean { return this.control ? this.control.enabled : null; }
|
||||
get enabled(): boolean|null { return this.control ? this.control.enabled : null; }
|
||||
|
||||
get statusChanges(): Observable<any> { return this.control ? this.control.statusChanges : null; }
|
||||
get statusChanges(): Observable<any>|null {
|
||||
return this.control ? this.control.statusChanges : null;
|
||||
}
|
||||
|
||||
get valueChanges(): Observable<any> { return this.control ? this.control.valueChanges : null; }
|
||||
get valueChanges(): Observable<any>|null {
|
||||
return this.control ? this.control.valueChanges : null;
|
||||
}
|
||||
|
||||
get path(): string[] { return null; }
|
||||
get path(): string[]|null { return null; }
|
||||
|
||||
reset(value: any = undefined): void {
|
||||
if (this.control) this.control.reset(value);
|
||||
}
|
||||
|
||||
hasError(errorCode: string, path: string[] = null): boolean {
|
||||
hasError(errorCode: string, path?: string[]): boolean {
|
||||
return this.control ? this.control.hasError(errorCode, path) : false;
|
||||
}
|
||||
|
||||
getError(errorCode: string, path: string[] = null): any {
|
||||
getError(errorCode: string, path?: string[]): any {
|
||||
return this.control ? this.control.getError(errorCode, path) : null;
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective.addFormGroup(this);
|
||||
this.formDirective !.addFormGroup(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
@ -46,7 +46,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
/**
|
||||
* Get the {@link FormGroup} backing this binding.
|
||||
*/
|
||||
get control(): FormGroup { return this.formDirective.getFormGroup(this); }
|
||||
get control(): FormGroup { return this.formDirective !.getFormGroup(this); }
|
||||
|
||||
/**
|
||||
* Get the path to this control group.
|
||||
@ -56,11 +56,13 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
|
||||
/**
|
||||
* Get the {@link Form} to which this group belongs.
|
||||
*/
|
||||
get formDirective(): Form { return this._parent ? this._parent.formDirective : null; }
|
||||
get formDirective(): Form|null { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._asyncValidators);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_checkParentType(): void {}
|
||||
|
@ -17,16 +17,16 @@ import {Form} from './form_interface';
|
||||
*
|
||||
* @stable
|
||||
*/
|
||||
export class ControlContainer extends AbstractControlDirective {
|
||||
export abstract class ControlContainer extends AbstractControlDirective {
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Get the form to which this container belongs.
|
||||
*/
|
||||
get formDirective(): Form { return null; }
|
||||
get formDirective(): Form|null { return null; }
|
||||
|
||||
/**
|
||||
* Get the path to this container.
|
||||
*/
|
||||
get path(): string[] { return null; }
|
||||
get path(): string[]|null { return null; }
|
||||
}
|
||||
|
@ -26,16 +26,16 @@ function unimplemented(): any {
|
||||
*/
|
||||
export abstract class NgControl extends AbstractControlDirective {
|
||||
/** @internal */
|
||||
_parent: ControlContainer = null;
|
||||
name: string = null;
|
||||
valueAccessor: ControlValueAccessor = null;
|
||||
_parent: ControlContainer|null = null;
|
||||
name: string|null = null;
|
||||
valueAccessor: ControlValueAccessor|null = null;
|
||||
/** @internal */
|
||||
_rawValidators: Array<Validator|ValidatorFn> = [];
|
||||
/** @internal */
|
||||
_rawAsyncValidators: Array<AsyncValidator|AsyncValidatorFn> = [];
|
||||
|
||||
get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
|
||||
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }
|
||||
get validator(): ValidatorFn|null { return <ValidatorFn>unimplemented(); }
|
||||
get asyncValidator(): AsyncValidatorFn|null { return <AsyncValidatorFn>unimplemented(); }
|
||||
|
||||
abstract viewToModelUpdate(newValue: any): void;
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ export class NgForm extends ControlContainer implements Form {
|
||||
|
||||
updateModel(dir: NgControl, value: any): void {
|
||||
resolvedPromise.then(() => {
|
||||
const ctrl = <FormControl>this.form.get(dir.path);
|
||||
const ctrl = <FormControl>this.form.get(dir.path !);
|
||||
ctrl.setValue(value);
|
||||
});
|
||||
}
|
||||
|
@ -158,9 +158,9 @@ export class NgModel extends NgControl implements OnChanges,
|
||||
|
||||
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
}
|
||||
|
||||
@ -176,7 +176,7 @@ export class NgModel extends NgControl implements OnChanges,
|
||||
}
|
||||
|
||||
private _isStandalone(): boolean {
|
||||
return !this._parent || (this.options && this.options.standalone);
|
||||
return !this._parent || !!(this.options && this.options.standalone);
|
||||
}
|
||||
|
||||
private _setUpStandalone(): void {
|
||||
|
@ -47,7 +47,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
|
||||
}
|
||||
|
||||
registerOnChange(fn: (_: number) => void): void {
|
||||
registerOnChange(fn: (_: number|null) => void): void {
|
||||
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
||||
}
|
||||
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
|
||||
|
@ -45,7 +45,7 @@ export class RangeValueAccessor implements ControlValueAccessor {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
|
||||
}
|
||||
|
||||
registerOnChange(fn: (_: number) => void): void {
|
||||
registerOnChange(fn: (_: number|null) => void): void {
|
||||
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
||||
}
|
||||
|
||||
|
@ -88,8 +88,8 @@ export class FormControlDirective extends NgControl implements OnChanges {
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if (this._isControlChanged(changes)) {
|
||||
setUpControl(this.form, this);
|
||||
if (this.control.disabled && this.valueAccessor.setDisabledState) {
|
||||
this.valueAccessor.setDisabledState(true);
|
||||
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
|
||||
this.valueAccessor !.setDisabledState !(true);
|
||||
}
|
||||
this.form.updateValueAndValidity({emitEvent: false});
|
||||
}
|
||||
@ -101,9 +101,9 @@ export class FormControlDirective extends NgControl implements OnChanges {
|
||||
|
||||
get path(): string[] { return []; }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
}
|
||||
|
||||
|
@ -125,14 +125,14 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
|
||||
this.update.emit(newValue);
|
||||
}
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
get path(): string[] { return controlPath(this.name, this._parent !); }
|
||||
|
||||
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn {
|
||||
return composeAsyncValidators(this._rawAsyncValidators);
|
||||
return composeAsyncValidators(this._rawAsyncValidators) !;
|
||||
}
|
||||
|
||||
get control(): FormControl { return this._control; }
|
||||
@ -151,8 +151,8 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
|
||||
private _setUpControl() {
|
||||
this._checkParentType();
|
||||
this._control = this.formDirective.addControl(this);
|
||||
if (this.control.disabled && this.valueAccessor.setDisabledState) {
|
||||
this.valueAccessor.setDisabledState(true);
|
||||
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
|
||||
this.valueAccessor !.setDisabledState !(true);
|
||||
}
|
||||
this._added = true;
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ export class FormGroupDirective extends ControlContainer implements Form,
|
||||
private _oldForm: FormGroup;
|
||||
directives: FormControlName[] = [];
|
||||
|
||||
@Input('formGroup') form: FormGroup = null;
|
||||
@Input('formGroup') form: FormGroup = null !;
|
||||
@Output() ngSubmit = new EventEmitter();
|
||||
|
||||
constructor(
|
||||
@ -167,10 +167,10 @@ export class FormGroupDirective extends ControlContainer implements Form,
|
||||
|
||||
private _updateValidators() {
|
||||
const sync = composeValidators(this._validators);
|
||||
this.form.validator = Validators.compose([this.form.validator, sync]);
|
||||
this.form.validator = Validators.compose([this.form.validator !, sync !]);
|
||||
|
||||
const async = composeAsyncValidators(this._asyncValidators);
|
||||
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator, async]);
|
||||
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator !, async !]);
|
||||
}
|
||||
|
||||
private _checkFormPresent() {
|
||||
|
@ -166,7 +166,7 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective.addFormArray(this);
|
||||
this.formDirective !.addFormArray(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
@ -175,17 +175,19 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
|
||||
}
|
||||
}
|
||||
|
||||
get control(): FormArray { return this.formDirective.getFormArray(this); }
|
||||
get control(): FormArray { return this.formDirective !.getFormArray(this); }
|
||||
|
||||
get formDirective(): FormGroupDirective {
|
||||
get formDirective(): FormGroupDirective|null {
|
||||
return this._parent ? <FormGroupDirective>this._parent.formDirective : null;
|
||||
}
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
get validator(): ValidatorFn|null { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
get asyncValidator(): AsyncValidatorFn|null {
|
||||
return composeAsyncValidators(this._asyncValidators);
|
||||
}
|
||||
|
||||
private _checkParentType(): void {
|
||||
if (_hasInvalidParent(this._parent)) {
|
||||
|
@ -15,7 +15,7 @@ export const SELECT_VALUE_ACCESSOR: Provider = {
|
||||
multi: true
|
||||
};
|
||||
|
||||
function _buildValueString(id: string, value: any): string {
|
||||
function _buildValueString(id: string | null, value: any): string {
|
||||
if (id == null) return `${value}`;
|
||||
if (value && typeof value === 'object') value = 'Object';
|
||||
return `${id}: ${value}`.slice(0, 50);
|
||||
@ -118,7 +118,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
|
||||
|
||||
writeValue(value: any): void {
|
||||
this.value = value;
|
||||
const id: string = this._getOptionId(value);
|
||||
const id: string|null = this._getOptionId(value);
|
||||
if (id == null) {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'selectedIndex', -1);
|
||||
}
|
||||
@ -142,7 +142,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
|
||||
_registerOption(): string { return (this._idCounter++).toString(); }
|
||||
|
||||
/** @internal */
|
||||
_getOptionId(value: any): string {
|
||||
_getOptionId(value: any): string|null {
|
||||
for (const id of Array.from(this._optionMap.keys())) {
|
||||
if (this._compareWith(this._optionMap.get(id), value)) return id;
|
||||
}
|
||||
|
@ -149,9 +149,9 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_getOptionId(value: any): string {
|
||||
_getOptionId(value: any): string|null {
|
||||
for (const id of Array.from(this._optionMap.keys())) {
|
||||
if (this._compareWith(this._optionMap.get(id)._value, value)) return id;
|
||||
if (this._compareWith(this._optionMap.get(id) !._value, value)) return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -159,7 +159,7 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
|
||||
/** @internal */
|
||||
_getOptionValue(valueString: string): any {
|
||||
const id: string = _extractId(valueString);
|
||||
return this._optionMap.has(id) ? this._optionMap.get(id)._value : valueString;
|
||||
return this._optionMap.has(id) ? this._optionMap.get(id) !._value : valueString;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,55 +27,55 @@ import {AsyncValidator, AsyncValidatorFn, Validator, ValidatorFn} from './valida
|
||||
|
||||
|
||||
export function controlPath(name: string, parent: ControlContainer): string[] {
|
||||
return [...parent.path, name];
|
||||
return [...parent.path !, name];
|
||||
}
|
||||
|
||||
export function setUpControl(control: FormControl, dir: NgControl): void {
|
||||
if (!control) _throwError(dir, 'Cannot find control with');
|
||||
if (!dir.valueAccessor) _throwError(dir, 'No value accessor for form control with');
|
||||
|
||||
control.validator = Validators.compose([control.validator, dir.validator]);
|
||||
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
|
||||
dir.valueAccessor.writeValue(control.value);
|
||||
control.validator = Validators.compose([control.validator !, dir.validator]);
|
||||
control.asyncValidator = Validators.composeAsync([control.asyncValidator !, dir.asyncValidator]);
|
||||
dir.valueAccessor !.writeValue(control.value);
|
||||
|
||||
// view -> model
|
||||
dir.valueAccessor.registerOnChange((newValue: any) => {
|
||||
dir.valueAccessor !.registerOnChange((newValue: any) => {
|
||||
dir.viewToModelUpdate(newValue);
|
||||
control.markAsDirty();
|
||||
control.setValue(newValue, {emitModelToViewChange: false});
|
||||
});
|
||||
|
||||
// touched
|
||||
dir.valueAccessor.registerOnTouched(() => control.markAsTouched());
|
||||
dir.valueAccessor !.registerOnTouched(() => control.markAsTouched());
|
||||
|
||||
control.registerOnChange((newValue: any, emitModelEvent: boolean) => {
|
||||
// control -> view
|
||||
dir.valueAccessor.writeValue(newValue);
|
||||
dir.valueAccessor !.writeValue(newValue);
|
||||
|
||||
// control -> ngModel
|
||||
if (emitModelEvent) dir.viewToModelUpdate(newValue);
|
||||
});
|
||||
|
||||
if (dir.valueAccessor.setDisabledState) {
|
||||
if (dir.valueAccessor !.setDisabledState) {
|
||||
control.registerOnDisabledChange(
|
||||
(isDisabled: boolean) => { dir.valueAccessor.setDisabledState(isDisabled); });
|
||||
(isDisabled: boolean) => { dir.valueAccessor !.setDisabledState !(isDisabled); });
|
||||
}
|
||||
|
||||
// re-run validation when validator binding changes, e.g. minlength=3 -> minlength=4
|
||||
dir._rawValidators.forEach((validator: Validator | ValidatorFn) => {
|
||||
if ((<Validator>validator).registerOnValidatorChange)
|
||||
(<Validator>validator).registerOnValidatorChange(() => control.updateValueAndValidity());
|
||||
(<Validator>validator).registerOnValidatorChange !(() => control.updateValueAndValidity());
|
||||
});
|
||||
|
||||
dir._rawAsyncValidators.forEach((validator: AsyncValidator | AsyncValidatorFn) => {
|
||||
if ((<Validator>validator).registerOnValidatorChange)
|
||||
(<Validator>validator).registerOnValidatorChange(() => control.updateValueAndValidity());
|
||||
(<Validator>validator).registerOnValidatorChange !(() => control.updateValueAndValidity());
|
||||
});
|
||||
}
|
||||
|
||||
export function cleanUpControl(control: FormControl, dir: NgControl) {
|
||||
dir.valueAccessor.registerOnChange(() => _noControlError(dir));
|
||||
dir.valueAccessor.registerOnTouched(() => _noControlError(dir));
|
||||
dir.valueAccessor !.registerOnChange(() => _noControlError(dir));
|
||||
dir.valueAccessor !.registerOnTouched(() => _noControlError(dir));
|
||||
|
||||
dir._rawValidators.forEach((validator: any) => {
|
||||
if (validator.registerOnValidatorChange) {
|
||||
@ -105,9 +105,9 @@ function _noControlError(dir: NgControl) {
|
||||
|
||||
function _throwError(dir: AbstractControlDirective, message: string): void {
|
||||
let messageEnd: string;
|
||||
if (dir.path.length > 1) {
|
||||
messageEnd = `path: '${dir.path.join(' -> ')}'`;
|
||||
} else if (dir.path[0]) {
|
||||
if (dir.path !.length > 1) {
|
||||
messageEnd = `path: '${dir.path!.join(' -> ')}'`;
|
||||
} else if (dir.path ![0]) {
|
||||
messageEnd = `name: '${dir.path}'`;
|
||||
} else {
|
||||
messageEnd = 'unspecified name attribute';
|
||||
@ -115,11 +115,12 @@ function _throwError(dir: AbstractControlDirective, message: string): void {
|
||||
throw new Error(`${message} ${messageEnd}`);
|
||||
}
|
||||
|
||||
export function composeValidators(validators: Array<Validator|Function>): ValidatorFn {
|
||||
export function composeValidators(validators: Array<Validator|Function>): ValidatorFn|null {
|
||||
return validators != null ? Validators.compose(validators.map(normalizeValidator)) : null;
|
||||
}
|
||||
|
||||
export function composeAsyncValidators(validators: Array<Validator|Function>): AsyncValidatorFn {
|
||||
export function composeAsyncValidators(validators: Array<Validator|Function>): AsyncValidatorFn|
|
||||
null {
|
||||
return validators != null ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
|
||||
null;
|
||||
}
|
||||
@ -147,12 +148,12 @@ export function isBuiltInAccessor(valueAccessor: ControlValueAccessor): boolean
|
||||
|
||||
// TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
|
||||
export function selectValueAccessor(
|
||||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor {
|
||||
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor|null {
|
||||
if (!valueAccessors) return null;
|
||||
|
||||
let defaultAccessor: ControlValueAccessor;
|
||||
let builtinAccessor: ControlValueAccessor;
|
||||
let customAccessor: ControlValueAccessor;
|
||||
let defaultAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let builtinAccessor: ControlValueAccessor|undefined = undefined;
|
||||
let customAccessor: ControlValueAccessor|undefined = undefined;
|
||||
valueAccessors.forEach((v: ControlValueAccessor) => {
|
||||
if (v.constructor === DefaultValueAccessor) {
|
||||
defaultAccessor = v;
|
||||
|
Reference in New Issue
Block a user