src/select/select.component.ts
ibm-select provides a styled select component.
Example:
<ibm-select [(ngModel)]="model">
<option value="default" disabled selected hidden>Choose an option</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</ibm-select>
| providers |
{
: , : , : true
}
|
| selector | ibm-select |
Properties |
|
Methods |
Inputs |
Outputs |
HostListeners |
Accessors |
disabled
|
Set to true to disable component.
Default value: |
|
Defined in src/select/select.component.ts:78
|
|
display
|
Type:
Default value: |
|
Defined in src/select/select.component.ts:66
|
|
id
|
Sets the unique ID. Defaults to |
|
Defined in src/select/select.component.ts:74
|
|
label
|
Label for the select. Appears above the input.
Default value: |
|
Defined in src/select/select.component.ts:70
|
|
selected
|
emits the selected options $event type: EventEmitter
|
|
Defined in src/select/select.component.ts:82
|
|
| blur |
blur()
|
|
Defined in src/select/select.component.ts:141
|
| onChange | ||||||||
onChange(event: )
|
||||||||
|
Defined in src/select/select.component.ts:126
|
||||||||
|
Handles the change event from the
Parameters :
Returns :
void
|
| registerOnChange | ||||||||
registerOnChange(fn: any)
|
||||||||
|
Defined in src/select/select.component.ts:104
|
||||||||
|
Registers a listener that notifies the model when the control updates
Parameters :
Returns :
void
|
| registerOnTouched | ||||||||
registerOnTouched(fn: any)
|
||||||||
|
Defined in src/select/select.component.ts:111
|
||||||||
|
Registers a listener that notifies the model when the control is blurred
Parameters :
Returns :
void
|
| setDisabledState | ||||||||
setDisabledState(isDisabled: boolean)
|
||||||||
|
Defined in src/select/select.component.ts:118
|
||||||||
|
Sets the disabled state through the model
Parameters :
Returns :
void
|
| writeValue | ||||||||
writeValue(obj: any)
|
||||||||
|
Defined in src/select/select.component.ts:97
|
||||||||
|
Receives a value from the model.
Parameters :
Returns :
void
|
| Private onChangeHandler |
onChangeHandler:
|
|
Defined in src/select/select.component.ts:134
|
|
placeholder declarations. Replaced by the functions provided to |
| Private onTouchedHandler |
onTouchedHandler:
|
|
Defined in src/select/select.component.ts:135
|
| select |
select:
|
Type : ElementRef
|
Decorators : ViewChild
|
|
Defined in src/select/select.component.ts:84
|
| Static selectCount |
selectCount:
|
Default value : 0
|
|
Defined in src/select/select.component.ts:61
|
|
Tracks the total number of selects instantiated. Used to generate unique IDs |
| value | ||||||||
getvalue()
|
||||||||
|
Defined in src/select/select.component.ts:86
|
||||||||
setvalue(v: )
|
||||||||
|
Defined in src/select/select.component.ts:90
|
||||||||
|
Parameters :
Returns :
void
|
import {
Component,
Input,
Output,
ViewChild,
ElementRef,
HostListener,
EventEmitter
} from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
/**
* `ibm-select` provides a styled `select` component.
*
* Example:
*
* ```
* <ibm-select [(ngModel)]="model">
* <option value="default" disabled selected hidden>Choose an option</option>
* <option value="option1">Option 1</option>
* <option value="option2">Option 2</option>
* <option value="option3">Option 3</option>
* </ibm-select>
* ```
*/
@Component({
selector: "ibm-select",
template: `
<div class="bx--form-item">
<div
[ngClass]="{'bx--select--inline': display === 'inline'}"
class="bx--select">
<label [attr.for]="id" class="bx--label">{{label}}</label>
<select
#select
[attr.id]="id"
[disabled]="disabled"
(change)="onChange($event)"
class="bx--select-input">
<ng-content></ng-content>
</select>
<svg class="bx--select__arrow" width="10" height="5" viewBox="0 0 10 5">
<path d="M0 0l5 4.998L10 0z" fill-rule="evenodd" />
</svg>
</div>
</div>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: Select,
multi: true
}
]
})
export class Select implements ControlValueAccessor {
/**
* Tracks the total number of selects instantiated. Used to generate unique IDs
*/
static selectCount = 0;
/**
* `inline` or `default` select displays
*/
@Input() display: "inline" | "default" = "default";
/**
* Label for the select. Appears above the input.
*/
@Input() label = "Select label";
/**
* Sets the unique ID. Defaults to `select-${total count of selects instantiated}`
*/
@Input() id = `select-${Select.selectCount++}`;
/**
* Set to true to disable component.
*/
@Input() disabled = false;
/**
* emits the selected options `value`
*/
@Output() selected = new EventEmitter();
@ViewChild("select") select: ElementRef;
get value() {
return this.select.nativeElement.value;
}
set value(v) {
this.select.nativeElement.value = v;
}
/**
* Receives a value from the model.
*/
writeValue(obj: any) {
this.value = obj;
}
/**
* Registers a listener that notifies the model when the control updates
*/
registerOnChange(fn: any) {
this.onChangeHandler = fn;
}
/**
* Registers a listener that notifies the model when the control is blurred
*/
registerOnTouched(fn: any) {
this.onTouchedHandler = fn;
}
/**
* Sets the disabled state through the model
*/
setDisabledState(isDisabled: boolean) {
this.disabled = isDisabled;
}
/**
* Handles the change event from the `select`.
* Sends events to the change handler and emits a `selected` event.
*/
onChange(event) {
this.onChangeHandler(event.target.value);
this.selected.emit(event.target.value);
}
/**
* placeholder declarations. Replaced by the functions provided to `registerOnChange` and `registerOnTouched`
*/
private onChangeHandler = (_: any) => { };
private onTouchedHandler = () => { };
/**
* Listens for the host blurring, and notifies the model
*/
@HostListener("blur")
private blur() {
this.onTouchedHandler();
}
}