src/radio/radio.component.ts
class: Radio (extends Checkbox)
selector: n-radio
source: src/forms/radio.component.ts
<cds-radio [(ngModel)]="radioState">Radio</cds-radio>Also see: RadioGroup
| providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: Radio, multi: true
}
|
| selector | cds-radio, ibm-radio |
| template | |
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
Accessors |
| ariaLabel | |
Type : string
|
|
|
Defined in src/radio/radio.component.ts:89
|
|
|
Used to set the |
|
| ariaLabelledby | |
Type : string
|
|
|
Defined in src/radio/radio.component.ts:75
|
|
| checked | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/radio/radio.component.ts:67
|
|
| disabled | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/radio/radio.component.ts:71
|
|
| id | |
Type : string
|
|
Default value : `radio-${Radio.radioCount++}`
|
|
|
Defined in src/radio/radio.component.ts:106
|
|
|
The id for the |
|
| labelPlacement | |
Type : "right" | "left"
|
|
Default value : "right"
|
|
|
Defined in src/radio/radio.component.ts:73
|
|
| name | |
Type : string
|
|
Default value : ""
|
|
|
Defined in src/radio/radio.component.ts:69
|
|
| required | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/radio/radio.component.ts:94
|
|
|
Sets the HTML required attribute |
|
| skeleton | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/radio/radio.component.ts:102
|
|
|
Set to |
|
| value | |
Type : any
|
|
|
Defined in src/radio/radio.component.ts:98
|
|
|
The value of the |
|
| change | |
Type : EventEmitter
|
|
|
Defined in src/radio/radio.component.ts:110
|
|
|
emits when the state of the radio changes |
|
| class.cds--radio-button-wrapper |
Type : boolean
|
Default value : true
|
|
Defined in src/radio/radio.component.ts:112
|
| class.cds--radio-button-wrapper--label-left |
Type : boolean
|
|
Defined in src/radio/radio.component.ts:114
|
| onChange | ||||||
onChange(event: Event)
|
||||||
|
Defined in src/radio/radio.component.ts:134
|
||||||
|
Synchronizes with the
Parameters :
Returns :
void
|
| onClick | ||||||
onClick(event: Event)
|
||||||
|
Defined in src/radio/radio.component.ts:138
|
||||||
|
Parameters :
Returns :
void
|
| registerRadioChangeHandler | ||||||||
registerRadioChangeHandler(fn: (event: RadioChange) => void)
|
||||||||
|
Defined in src/radio/radio.component.ts:149
|
||||||||
|
Method called by
Parameters :
Returns :
void
|
| setDisabledFromGroup | ||||||
setDisabledFromGroup(disabled: boolean)
|
||||||
|
Defined in src/radio/radio.component.ts:153
|
||||||
|
Parameters :
Returns :
void
|
| Protected _labelledby |
Type : string
|
Default value : ""
|
|
Defined in src/radio/radio.component.ts:123
|
| disabledFromGroup |
Default value : false
|
|
Defined in src/radio/radio.component.ts:121
|
|
Reflects whether or not the input is disabled at |
| hostClass |
Default value : true
|
Decorators :
@HostBinding('class.cds--radio-button-wrapper')
|
|
Defined in src/radio/radio.component.ts:112
|
| radioChangeHandler |
Default value : () => {...}
|
|
Defined in src/radio/radio.component.ts:128
|
|
Handler provided by the |
| Static radioCount |
Type : number
|
Default value : 0
|
|
Defined in src/radio/radio.component.ts:65
|
|
Used to dynamically create unique ids for the |
| ariaLabelledby | ||||||
getariaLabelledby()
|
||||||
|
Defined in src/radio/radio.component.ts:79
|
||||||
setariaLabelledby(value: string)
|
||||||
|
Defined in src/radio/radio.component.ts:75
|
||||||
|
Parameters :
Returns :
void
|
| labelLeft |
getlabelLeft()
|
|
Defined in src/radio/radio.component.ts:114
|
import {
Component,
Input,
HostBinding,
Output,
EventEmitter
} from "@angular/core";
import { NG_VALUE_ACCESSOR } from "@angular/forms";
import { RadioChange } from "./radio-change.class";
/**
* class: Radio (extends Checkbox)
*
* selector: `n-radio`
*
* source: `src/forms/radio.component.ts`
*
* ```html
* <cds-radio [(ngModel)]="radioState">Radio</cds-radio>
* ```
*
* Also see: [`RadioGroup`](#cds-radio-group)
*/
@Component({
selector: "cds-radio, ibm-radio",
template: `
<input
*ngIf="!skeleton"
class="cds--radio-button"
type="radio"
[checked]="checked"
[disabled]="disabled || disabledFromGroup"
[name]="name"
[id]="id"
[required]="required"
[attr.value]="value"
[attr.aria-labelledby]="ariaLabelledby"
(change)="onChange($event)"
(click)="onClick($event)">
<div *ngIf="skeleton" class="cds--radio-button cds--skeleton"></div>
<label
class="cds--radio-button__label"
[attr.aria-label]="ariaLabel"
[ngClass]="{
'cds--skeleton': skeleton
}"
[for]="id"
id="label-{{id}}">
<span class="cds--radio-button__appearance"></span>
<ng-content></ng-content>
</label>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: Radio,
multi: true
}
]
})
export class Radio {
/**
* Used to dynamically create unique ids for the `Radio`.
*/
static radioCount = 0;
@Input() checked = false;
@Input() name = "";
@Input() disabled = false;
@Input() labelPlacement: "right" | "left" = "right";
@Input() set ariaLabelledby(value: string) {
this._labelledby = value;
}
get ariaLabelledby() {
if (this._labelledby) {
return this._labelledby;
}
return `label-${this.id}`;
}
/**
* Used to set the `aria-label` attribute on the input label.
*/
@Input() ariaLabel: string;
/**
* Sets the HTML required attribute
*/
@Input() required = false;
/**
* The value of the `Radio`.
*/
@Input() value;
/**
* Set to `true` for a loading table.
*/
@Input() skeleton = false;
/**
* The id for the `Radio`.
*/
@Input() id = `radio-${Radio.radioCount++}`;
/**
* emits when the state of the radio changes
*/
@Output() change = new EventEmitter<RadioChange>();
@HostBinding("class.cds--radio-button-wrapper") hostClass = true;
@HostBinding("class.cds--radio-button-wrapper--label-left") get labelLeft() {
return this.labelPlacement === "left";
}
/**
* Reflects whether or not the input is disabled at `RadioGroup` level.
*/
disabledFromGroup = false;
protected _labelledby = "";
/**
* Handler provided by the `RadioGroup` to bubble events up
*/
radioChangeHandler = (event: RadioChange) => {};
/**
* Synchronizes with the `RadioGroup` in the event of a changed `Radio`.
* Emits the changes of both the `RadioGroup` and `Radio`.
*/
onChange(event: Event) {
event.stopPropagation();
}
onClick(event: Event) {
this.checked = (event.target as HTMLInputElement).checked;
const radioEvent = new RadioChange(this, this.value);
this.change.emit(radioEvent);
this.radioChangeHandler(radioEvent);
}
/**
* Method called by `RadioGroup` with a callback function to bubble `RadioChange` events
* @param fn callback that expects a `RadioChange` as an argument
*/
registerRadioChangeHandler(fn: (event: RadioChange) => void) {
this.radioChangeHandler = fn;
}
setDisabledFromGroup(disabled: boolean) {
this.disabledFromGroup = disabled;
}
}