feat(radio): support radio button sharing a control
This commit is contained in:
@ -25,7 +25,7 @@ export {NgForm} from './directives/ng_form';
|
||||
export {NgModel} from './directives/ng_model';
|
||||
export {NgModelGroup} from './directives/ng_model_group';
|
||||
export {NumberValueAccessor} from './directives/number_value_accessor';
|
||||
export {RadioButtonState, RadioControlValueAccessor} from './directives/radio_control_value_accessor';
|
||||
export {RadioControlValueAccessor} from './directives/radio_control_value_accessor';
|
||||
export {FormControlDirective} from './directives/reactive_directives/form_control_directive';
|
||||
export {FormControlName} from './directives/reactive_directives/form_control_name';
|
||||
export {FormGroupDirective} from './directives/reactive_directives/form_group_directive';
|
||||
|
@ -9,6 +9,7 @@ import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators';
|
||||
import {ControlContainer} from './control_container';
|
||||
import {Form} from './form_interface';
|
||||
import {NgControl} from './ng_control';
|
||||
import {NgModel} from './ng_model';
|
||||
import {NgModelGroup} from './ng_model_group';
|
||||
import {composeAsyncValidators, composeValidators, setUpControl, setUpFormGroup} from './shared';
|
||||
|
||||
@ -107,20 +108,21 @@ export class NgForm extends ControlContainer implements Form {
|
||||
|
||||
get controls(): {[key: string]: AbstractControl} { return this.form.controls; }
|
||||
|
||||
addControl(dir: NgControl): FormControl {
|
||||
addControl(dir: NgModel): FormControl {
|
||||
const ctrl = new FormControl();
|
||||
PromiseWrapper.scheduleMicrotask(() => {
|
||||
const container = this._findContainer(dir.path);
|
||||
setUpControl(ctrl, dir);
|
||||
container.registerControl(dir.name, ctrl);
|
||||
ctrl.updateValueAndValidity({emitEvent: false});
|
||||
dir._control = <FormControl>container.registerControl(dir.name, ctrl);
|
||||
setUpControl(dir.control, dir);
|
||||
dir.control.updateValueAndValidity({emitEvent: false});
|
||||
});
|
||||
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
getControl(dir: NgControl): FormControl { return <FormControl>this.form.find(dir.path); }
|
||||
getControl(dir: NgModel): FormControl { return <FormControl>this.form.find(dir.path); }
|
||||
|
||||
removeControl(dir: NgControl): void {
|
||||
removeControl(dir: NgModel): void {
|
||||
PromiseWrapper.scheduleMicrotask(() => {
|
||||
var container = this._findContainer(dir.path);
|
||||
if (isPresent(container)) {
|
||||
|
@ -36,7 +36,7 @@ export class RadioControlRegistry {
|
||||
select(accessor: RadioControlValueAccessor) {
|
||||
this._accessors.forEach((c) => {
|
||||
if (this._isSameGroup(c, accessor) && c[1] !== accessor) {
|
||||
c[1].fireUncheck();
|
||||
c[1].fireUncheck(accessor.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -48,16 +48,6 @@ export class RadioControlRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The value provided by the forms API for radio buttons.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export class RadioButtonState {
|
||||
constructor(public checked: boolean, public value: string) {}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The accessor for writing a radio control value and listening to changes that is used by the
|
||||
* {@link NgModel}, {@link FormControlDirective}, and {@link FormControlName} directives.
|
||||
@ -66,13 +56,12 @@ export class RadioButtonState {
|
||||
* ```
|
||||
* @Component({
|
||||
* template: `
|
||||
* <input type="radio" name="food" [(ngModel)]="foodChicken">
|
||||
* <input type="radio" name="food" [(ngModel)]="foodFish">
|
||||
* <input type="radio" name="food" [(ngModel)]="food" value="chicken">
|
||||
* <input type="radio" name="food" [(ngModel)]="food" value="fish">
|
||||
* `
|
||||
* })
|
||||
* class FoodCmp {
|
||||
* foodChicken = new RadioButtonState(true, "chicken");
|
||||
* foodFish = new RadioButtonState(false, "fish");
|
||||
* food = 'chicken';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
@ -85,14 +74,16 @@ export class RadioButtonState {
|
||||
export class RadioControlValueAccessor implements ControlValueAccessor,
|
||||
OnDestroy, OnInit {
|
||||
/** @internal */
|
||||
_state: RadioButtonState;
|
||||
_state: boolean;
|
||||
/** @internal */
|
||||
_control: NgControl;
|
||||
@Input() name: string;
|
||||
/** @internal */
|
||||
_fn: Function;
|
||||
onChange = () => {};
|
||||
onTouched = () => {};
|
||||
onTouched = () => {}
|
||||
|
||||
@Input() name: string;
|
||||
@Input() value: any;
|
||||
|
||||
constructor(
|
||||
private _renderer: Renderer, private _elementRef: ElementRef,
|
||||
@ -106,21 +97,21 @@ export class RadioControlValueAccessor implements ControlValueAccessor,
|
||||
ngOnDestroy(): void { this._registry.remove(this); }
|
||||
|
||||
writeValue(value: any): void {
|
||||
this._state = value;
|
||||
if (isPresent(value) && value.checked) {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'checked', true);
|
||||
this._state = value === this.value;
|
||||
if (isPresent(value)) {
|
||||
this._renderer.setElementProperty(this._elementRef.nativeElement, 'checked', this._state);
|
||||
}
|
||||
}
|
||||
|
||||
registerOnChange(fn: (_: any) => {}): void {
|
||||
this._fn = fn;
|
||||
this.onChange = () => {
|
||||
fn(new RadioButtonState(true, this._state.value));
|
||||
fn(this.value);
|
||||
this._registry.select(this);
|
||||
};
|
||||
}
|
||||
|
||||
fireUncheck(): void { this._fn(new RadioButtonState(false, this._state.value)); }
|
||||
fireUncheck(value: any): void { this.writeValue(value); }
|
||||
|
||||
registerOnTouched(fn: () => {}): void { this.onTouched = fn; }
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
|
||||
export {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES, RadioButtonState} from './directives';
|
||||
export {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES} from './directives';
|
||||
export {AbstractControlDirective} from './directives/abstract_control_directive';
|
||||
export {CheckboxControlValueAccessor} from './directives/checkbox_value_accessor';
|
||||
export {ControlContainer} from './directives/control_container';
|
||||
|
@ -282,7 +282,7 @@ export abstract class AbstractControl {
|
||||
*/
|
||||
export class FormControl extends AbstractControl {
|
||||
/** @internal */
|
||||
_onChange: Function;
|
||||
_onChange: Function[] = [];
|
||||
|
||||
constructor(
|
||||
value: any = null, validator: ValidatorFn|ValidatorFn[] = null,
|
||||
@ -312,7 +312,9 @@ export class FormControl extends AbstractControl {
|
||||
} = {}): void {
|
||||
emitModelToViewChange = isPresent(emitModelToViewChange) ? emitModelToViewChange : true;
|
||||
this._value = value;
|
||||
if (isPresent(this._onChange) && emitModelToViewChange) this._onChange(this._value);
|
||||
if (this._onChange.length && emitModelToViewChange) {
|
||||
this._onChange.forEach((changeFn) => changeFn(this._value));
|
||||
}
|
||||
this.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
|
||||
}
|
||||
|
||||
@ -329,7 +331,7 @@ export class FormControl extends AbstractControl {
|
||||
/**
|
||||
* Register a listener for change events.
|
||||
*/
|
||||
registerOnChange(fn: Function): void { this._onChange = fn; }
|
||||
registerOnChange(fn: Function): void { this._onChange.push(fn); }
|
||||
}
|
||||
|
||||
/**
|
||||
@ -364,9 +366,11 @@ export class FormGroup extends AbstractControl {
|
||||
/**
|
||||
* Register a control with the group's list of controls.
|
||||
*/
|
||||
registerControl(name: string, control: AbstractControl): void {
|
||||
registerControl(name: string, control: AbstractControl): AbstractControl {
|
||||
if (this.controls[name]) return this.controls[name];
|
||||
this.controls[name] = control;
|
||||
control.setParent(this);
|
||||
return control;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user