src/radio/radio-group.component.ts
class: RadioGroup
selector: ibm-radio-group
source: src/forms/radio.component.ts
Ex:
* <ibm-radio-group [(ngModel)]="radio">
* <ibm-radio *ngFor="let one of manyRadios" [value]="one">
* Radio {{one}}
* </ibm-radio>
* </ibm-radio-group>
*
* Radio selected: {{radio}}
*
* manyRadios = ["one", "two", "three", "four", "five", "six"];
*
Also see: Radio
AfterContentInit
AfterViewInit
ControlValueAccessor
providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: RadioGroup, multi: true
}
|
selector | ibm-radio-group |
template |
|
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
Accessors |
ariaLabel
|
Used to set the
Type : |
Defined in src/radio/radio-group.component.ts:83
|
ariaLabelledby
|
Used to set the
Type : |
Defined in src/radio/radio-group.component.ts:88
|
disabled
|
Set to true to disable the whole radio group
Default value : |
Defined in src/radio/radio-group.component.ts:156
|
labelPlacement
|
Type :
Default value : |
Defined in src/radio/radio-group.component.ts:77
|
name
|
Replaces the name associated with the
Type : |
Defined in src/radio/radio-group.component.ts:142
|
orientation
|
Type :
Default value : |
Defined in src/radio/radio-group.component.ts:75
|
selected
|
Sets the passed in |
Defined in src/radio/radio-group.component.ts:105
|
skeleton
|
Returns the skeleton value in the
Type : |
Defined in src/radio/radio-group.component.ts:162
|
value
|
Sets the value/state of the selected
Type : |
Defined in src/radio/radio-group.component.ts:122
|
change
|
Emits event notifying other classes of a change using a $event Type: EventEmitter<RadioChange>
|
Defined in src/radio/radio-group.component.ts:93
|
class.bx--form-item |
class.bx--form-item:
|
Default value : true
|
Defined in src/radio/radio-group.component.ts:177
|
Binds 'bx--form-item' value to the class for |
checkSelectedRadio |
checkSelectedRadio()
|
Defined in src/radio/radio-group.component.ts:207
|
Updates the selected
Returns :
void
|
emitChangeEvent | ||||||
emitChangeEvent(event: RadioChange)
|
||||||
Defined in src/radio/radio-group.component.ts:232
|
||||||
Creates a class of
Parameters :
Returns :
void
|
ngAfterContentInit |
ngAfterContentInit()
|
Defined in src/radio/radio-group.component.ts:264
|
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Defined in src/radio/radio-group.component.ts:274
|
Returns :
void
|
Public registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Defined in src/radio/radio-group.component.ts:281
|
||||||
Used to set method to propagate changes back to the form.
Parameters :
Returns :
void
|
Public registerOnTouched | ||||||||
registerOnTouched(fn: any)
|
||||||||
Defined in src/radio/radio-group.component.ts:289
|
||||||||
Registers a callback to be triggered when the control has been touched.
Parameters :
Returns :
void
|
Protected updateChildren |
updateChildren()
|
Defined in src/radio/radio-group.component.ts:303
|
Returns :
void
|
Protected updateRadioChangeHandler |
updateRadioChangeHandler()
|
Defined in src/radio/radio-group.component.ts:309
|
Returns :
void
|
updateRadioNames |
updateRadioNames()
|
Defined in src/radio/radio-group.component.ts:238
|
Returns :
void
|
updateRadios |
updateRadios()
|
Defined in src/radio/radio-group.component.ts:246
|
Synchronizes radio properties.
Returns :
void
|
updateSelectedRadioFromValue |
updateSelectedRadioFromValue()
|
Defined in src/radio/radio-group.component.ts:216
|
Use the value of the
Returns :
void
|
writeValue | ||||||
writeValue(value: any)
|
||||||
Defined in src/radio/radio-group.component.ts:260
|
||||||
Updates the value of the
Parameters :
Returns :
void
|
Protected _disabled |
_disabled:
|
Default value : false
|
Defined in src/radio/radio-group.component.ts:186
|
Reflects whether or not the input is disabled and cannot be selected. |
Protected _name |
_name:
|
Default value : `radio-group-${RadioGroup.radioGroupCount++}`
|
Defined in src/radio/radio-group.component.ts:202
|
The name attribute associated with the |
Protected _selected |
_selected:
|
Type : Radio
|
Default value : null
|
Defined in src/radio/radio-group.component.ts:198
|
The |
Protected _skeleton |
_skeleton:
|
Default value : false
|
Defined in src/radio/radio-group.component.ts:190
|
Reflects whether or not the dropdown is loading. |
Protected _value |
_value:
|
Type : any
|
Default value : null
|
Defined in src/radio/radio-group.component.ts:194
|
The value of the selected option within the |
Protected isInitialized |
isInitialized:
|
Default value : false
|
Defined in src/radio/radio-group.component.ts:182
|
To track whether the |
onTouched |
onTouched:
|
Type : function
|
Default value : () => {}
|
Defined in src/radio/radio-group.component.ts:296
|
Needed to properly implement ControlValueAccessor. |
propagateChange |
propagateChange:
|
Default value : (_: any) => {}
|
Defined in src/radio/radio-group.component.ts:301
|
Method set in registerOnChange to propagate changes back to the form. |
Static radioGroupCount |
radioGroupCount:
|
Type : number
|
Default value : 0
|
Defined in src/radio/radio-group.component.ts:73
|
Used for creating the |
radios |
radios:
|
Type : QueryList<Radio>
|
Decorators :
@ContentChildren(undefined)
|
Defined in src/radio/radio-group.component.ts:99
|
The |
selected | ||||
getselected()
|
||||
Defined in src/radio/radio-group.component.ts:114
|
||||
Returns the |
||||
setselected(selected)
|
||||
Defined in src/radio/radio-group.component.ts:105
|
||||
Sets the passed in
Parameters :
Returns :
void
|
value | ||||||
getvalue()
|
||||||
Defined in src/radio/radio-group.component.ts:134
|
||||||
Returns the value/state of the selected |
||||||
setvalue(newValue: any)
|
||||||
Defined in src/radio/radio-group.component.ts:122
|
||||||
Sets the value/state of the selected
Parameters :
Returns :
void
|
name | ||||||
getname()
|
||||||
Defined in src/radio/radio-group.component.ts:149
|
||||||
Returns the associated name of the |
||||||
setname(name: string)
|
||||||
Defined in src/radio/radio-group.component.ts:142
|
||||||
Replaces the name associated with the
Parameters :
Returns :
void
|
skeleton | ||||||
setskeleton(value: any)
|
||||||
Defined in src/radio/radio-group.component.ts:169
|
||||||
Sets the skeleton value for all
Parameters :
Returns :
void
|
import {
AfterContentInit,
ContentChildren,
Component,
EventEmitter,
forwardRef,
Input,
Output,
QueryList,
HostBinding,
AfterViewInit
} from "@angular/core";
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
import { Radio } from "./radio.component";
import { RadioChange } from "./radio-change.class";
/**
* [See demo](../../?path=/story/radio--basic)
*
* class: RadioGroup
*
* selector: `ibm-radio-group`
*
* source: `src/forms/radio.component.ts`
*
*
* Ex:
* ```html
* <ibm-radio-group [(ngModel)]="radio">
* <ibm-radio *ngFor="let one of manyRadios" [value]="one">
* Radio {{one}}
* </ibm-radio>
* </ibm-radio-group>
*
* Radio selected: {{radio}}
* ```
*
* ```typescript
* manyRadios = ["one", "two", "three", "four", "five", "six"];
* ```
*
* Also see: [`Radio`](#ibm-radio)
*
* <example-url>../../iframe.html?id=radio--basic</example-url>
*/
@Component({
selector: "ibm-radio-group",
template: `
<div
class="bx--radio-button-group"
[attr.aria-label]="ariaLabel"
[attr.aria-labelledby]="ariaLabelledby"
[ngClass]="{
'bx--radio-button-group--vertical': orientation === 'vertical',
'bx--radio-button-group--label-left': orientation === 'vertical' && labelPlacement === 'left'
}"
role="radiogroup">
<ng-content></ng-content>
</div>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: RadioGroup,
multi: true
}
]
})
export class RadioGroup implements AfterContentInit, AfterViewInit, ControlValueAccessor {
/**
* Used for creating the `RadioGroup` 'name' property dynamically.
*/
static radioGroupCount = 0;
@Input() orientation: "horizontal" | "vertical" = "horizontal";
@Input() labelPlacement: "right" | "left" = "right";
/**
* Used to set the `aria-label` attribute on the radio group element.
*/
// tslint:disable-next-line:no-input-rename
@Input() ariaLabel: string;
/**
* Used to set the `aria-labelledby` attribute on the radio group element.
*/
// tslint:disable-next-line:no-input-rename
@Input() ariaLabelledby: string;
/**
* Emits event notifying other classes of a change using a `RadioChange` class.
*/
@Output() change: EventEmitter<RadioChange> = new EventEmitter<RadioChange>();
/**
* The `Radio` input items in the `RadioGroup`.
*/
// tslint:disable-next-line:no-forward-ref
@ContentChildren(forwardRef(() => Radio)) radios: QueryList<Radio>;
/**
* Sets the passed in `Radio` item as the selected input within the `RadioGroup`.
*/
@Input()
set selected(selected: Radio | null) {
this._selected = selected;
this.value = selected ? selected.value : null;
this.checkSelectedRadio();
}
/**
* Returns the `Radio` that is selected within the `RadioGroup`.
*/
get selected() {
return this._selected;
}
/**
* Sets the value/state of the selected `Radio` within the `RadioGroup` to the passed in value.
*/
@Input()
set value(newValue: any) {
if (this._value !== newValue) {
this._value = newValue;
this.updateSelectedRadioFromValue();
this.checkSelectedRadio();
}
}
/**
* Returns the value/state of the selected `Radio` within the `RadioGroup`.
*/
get value() {
return this._value;
}
/**
* Replaces the name associated with the `RadioGroup` with the provided parameter.
*/
@Input()
set name(name: string) {
this._name = name;
this.updateRadios();
}
/**
* Returns the associated name of the `RadioGroup`.
*/
get name() {
return this._name;
}
/**
* Set to true to disable the whole radio group
*/
@Input() disabled = false;
/**
* Returns the skeleton value in the `RadioGroup` if there is one.
*/
@Input()
get skeleton(): any {
return this._skeleton;
}
/**
* Sets the skeleton value for all `Radio` to the skeleton value of `RadioGroup`.
*/
set skeleton(value: any) {
this._skeleton = value;
this.updateChildren();
}
/**
* Binds 'bx--form-item' value to the class for `RadioGroup`.
*/
@HostBinding("class.bx--form-item") radioButtonGroupClass = true;
/**
* To track whether the `RadioGroup` has been initialized.
*/
protected isInitialized = false;
/**
* Reflects whether or not the input is disabled and cannot be selected.
*/
protected _disabled = false;
/**
* Reflects whether or not the dropdown is loading.
*/
protected _skeleton = false;
/**
* The value of the selected option within the `RadioGroup`.
*/
protected _value: any = null;
/**
* The `Radio` within the `RadioGroup` that is selected.
*/
protected _selected: Radio = null;
/**
* The name attribute associated with the `RadioGroup`.
*/
protected _name = `radio-group-${RadioGroup.radioGroupCount++}`;
/**
* Updates the selected `Radio` to be checked (selected).
*/
checkSelectedRadio() {
if (this.selected && !this._selected.checked) {
this.selected.checked = true;
}
}
/**
* Use the value of the `RadioGroup` to update the selected radio to the right state (selected state).
*/
updateSelectedRadioFromValue() {
let alreadySelected = this._selected != null && this._selected.value === this._value;
if (this.radios && !alreadySelected) {
this._selected = null;
this.radios.forEach(radio => {
if (radio.checked) {
this._selected = radio;
}
});
}
}
/**
* Creates a class of `RadioChange` to emit the change in the `RadioGroup`.
*/
emitChangeEvent(event: RadioChange) {
this.change.emit(event);
this.propagateChange(event.value);
this.onTouched();
}
updateRadioNames() {
console.warn("updateRadioNames had been deprecated. Use updateRadios instead");
this.updateRadios();
}
/**
* Synchronizes radio properties.
*/
updateRadios() {
if (this.radios) {
setTimeout(() => {
this.radios.forEach(radio => radio.name = this.name);
if (this.labelPlacement === "left") {
this.radios.forEach(radio => radio.labelPlacement = "left");
}
});
}
}
/**
* Updates the value of the `RadioGroup` using the provided parameter.
*/
writeValue(value: any) {
this.value = value;
}
ngAfterContentInit() {
this.radios.changes.subscribe(() => {
this.updateRadios();
this.updateRadioChangeHandler();
});
this.updateChildren();
this.updateRadioChangeHandler();
}
ngAfterViewInit() {
this.updateRadios();
}
/**
* Used to set method to propagate changes back to the form.
*/
public registerOnChange(fn: any) {
this.propagateChange = fn;
}
/**
* Registers a callback to be triggered when the control has been touched.
* @param fn Callback to be triggered when the checkbox is touched.
*/
public registerOnTouched(fn: any) {
this.onTouched = fn;
}
/**
* Needed to properly implement ControlValueAccessor.
*/
onTouched: () => any = () => {};
/**
* Method set in registerOnChange to propagate changes back to the form.
*/
propagateChange = (_: any) => {};
protected updateChildren() {
if (this.radios) {
this.radios.forEach(child => child.skeleton = this.skeleton);
}
}
protected updateRadioChangeHandler() {
this.radios.forEach(radio => {
radio.registerRadioChangeHandler((event: RadioChange) => {
// update selected and value from the event
this._selected = event.source;
this._value = event.value;
// bubble the event
this.emitChangeEvent(event);
});
});
}
}