Skip to content

Commit

Permalink
feat(forms): make DefaultValueAccessor directive standalone
Browse files Browse the repository at this point in the history
This commit make the DefaultValueAccessor directive standalone allowing it to be used as hostDirective

Fixes: angular#48607
  • Loading branch information
JeanMeche committed Jan 2, 2023
1 parent 27da733 commit 4f3f214
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/forms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class DefaultValueAccessor extends BaseControlValueAccessor implements Co
constructor(renderer: Renderer2, elementRef: ElementRef, _compositionMode: boolean);
writeValue(value: any): void;
// (undocumented)
static ɵdir: i0.ɵɵDirectiveDeclaration<DefaultValueAccessor, "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]", never, {}, {}, never, never, false, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<DefaultValueAccessor, "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]", never, {}, {}, never, never, true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<DefaultValueAccessor, [null, null, { optional: true; }]>;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/forms/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ export {NgSelectOption, SelectControlValueAccessor} from './directives/select_co
export {NgSelectMultipleOption, SelectMultipleControlValueAccessor} from './directives/select_multiple_control_value_accessor';
export {CALL_SET_DISABLED_STATE} from './directives/shared';

export const SHARED_FORM_DIRECTIVES: Type<any>[] = [
const SHARED_FORM_DIRECTIVES: Type<any>[] = [
NgNoValidate,
NgSelectOption,
NgSelectMultipleOption,
DefaultValueAccessor,
NumberValueAccessor,
RangeValueAccessor,
CheckboxControlValueAccessor,
Expand Down Expand Up @@ -78,8 +77,8 @@ export const REACTIVE_DRIVEN_DIRECTIVES: Type<any>[] =
*/
@NgModule({
declarations: SHARED_FORM_DIRECTIVES,
imports: [RadioControlRegistryModule],
exports: SHARED_FORM_DIRECTIVES,
imports: [RadioControlRegistryModule, DefaultValueAccessor],
exports: [...SHARED_FORM_DIRECTIVES, DefaultValueAccessor],
})
export class ɵInternalFormsSharedModule {
}
Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/directives/default_value_accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export const COMPOSITION_BUFFER_MODE = new InjectionToken<boolean>('CompositionE
'(compositionstart)': '$any(this)._compositionStart()',
'(compositionend)': '$any(this)._compositionEnd($event.target.value)'
},
providers: [DEFAULT_VALUE_ACCESSOR]
providers: [DEFAULT_VALUE_ACCESSOR],
standalone: true,
})
export class DefaultValueAccessor extends BaseControlValueAccessor implements ControlValueAccessor {
/** Whether the user is creating a composition string (IME events). */
Expand Down
21 changes: 19 additions & 2 deletions packages/forms/test/directives_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {SimpleChange} from '@angular/core';
import {fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
import {Component, SimpleChange} from '@angular/core';
import {fakeAsync, flushMicrotasks, TestBed, tick} from '@angular/core/testing';
import {AbstractControl, CheckboxControlValueAccessor, ControlValueAccessor, DefaultValueAccessor, FormArray, FormArrayName, FormControl, FormControlDirective, FormControlName, FormGroup, FormGroupDirective, FormGroupName, NgControl, NgForm, NgModel, NgModelGroup, SelectControlValueAccessor, SelectMultipleControlValueAccessor, ValidationErrors, Validator, Validators} from '@angular/forms';
import {selectValueAccessor} from '@angular/forms/src/directives/shared';
import {composeValidators} from '@angular/forms/src/validators';
Expand Down Expand Up @@ -39,6 +39,23 @@ class CustomValidatorDirective implements Validator {
defaultAccessor = new DefaultValueAccessor(null!, null!, null!);
});

describe('DefaultValueAccessor', () => {
it('DefaultValueAccessor should be available as a standalone directive', () => {
@Component({
selector: 'test-component',
hostDirectives: [DefaultValueAccessor],
template: `<input />`,
})
class TestComponent {
}

const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();

expect(fixture.componentInstance).toBeTruthy();
});
})

describe('shared', () => {
describe('selectValueAccessor', () => {
let dir: NgControl;
Expand Down

0 comments on commit 4f3f214

Please sign in to comment.