File

src/radio/radio.component.ts

Description

class: Radio (extends Checkbox)

selector: n-radio

source: src/forms/radio.component.ts

Example :
    <cds-radio [(ngModel)]="radioState">Radio</cds-radio>

Also see: RadioGroup

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Inputs

ariaLabel
Type : string

Used to set the aria-label attribute on the input label.

ariaLabelledby
Type : string
checked
Type : boolean
Default value : false
disabled
Type : boolean
Default value : false
id
Type : string
Default value : `radio-${Radio.radioCount++}`

The id for the Radio.

labelPlacement
Type : "right" | "left"
Default value : "right"
name
Type : string
Default value : ""
required
Type : boolean
Default value : false

Sets the HTML required attribute

skeleton
Type : boolean
Default value : false

Set to true for a loading table.

value
Type : any

The value of the Radio.

Outputs

change
Type : EventEmitter

emits when the state of the radio changes

HostBindings

class.cds--radio-button-wrapper
Type : boolean
Default value : true
class.cds--radio-button-wrapper--label-left
Type : boolean

Methods

onChange
onChange(event: Event)

Synchronizes with the RadioGroup in the event of a changed Radio. Emits the changes of both the RadioGroup and Radio.

Parameters :
Name Type Optional
event Event No
Returns : void
onClick
onClick(event: Event)
Parameters :
Name Type Optional
event Event No
Returns : void
registerRadioChangeHandler
registerRadioChangeHandler(fn: (event: RadioChange) => void)

Method called by RadioGroup with a callback function to bubble RadioChange events

Parameters :
Name Type Optional Description
fn function No

callback that expects a RadioChange as an argument

Returns : void
setDisabledFromGroup
setDisabledFromGroup(disabled: boolean)
Parameters :
Name Type Optional
disabled boolean No
Returns : void

Properties

Protected _labelledby
Type : string
Default value : ""
disabledFromGroup
Default value : false

Reflects whether or not the input is disabled at RadioGroup level.

hostClass
Default value : true
Decorators :
@HostBinding('class.cds--radio-button-wrapper')
radioChangeHandler
Default value : () => {...}

Handler provided by the RadioGroup to bubble events up

Static radioCount
Type : number
Default value : 0

Used to dynamically create unique ids for the Radio.

Accessors

ariaLabelledby
getariaLabelledby()
setariaLabelledby(value: string)
Parameters :
Name Type Optional
value string No
Returns : void
labelLeft
getlabelLeft()
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;
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""