|
1 | | -import { Component, Input } from "@angular/core"; |
| 1 | +import { |
| 2 | + Component, |
| 3 | + Input, |
| 4 | + Output, |
| 5 | + ViewChild, |
| 6 | + ElementRef, |
| 7 | + HostListener, |
| 8 | + EventEmitter |
| 9 | +} from "@angular/core"; |
| 10 | +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms"; |
2 | 11 |
|
| 12 | +/** |
| 13 | + * `ibm-select` provides a styled `select` component. |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * |
| 17 | + * ``` |
| 18 | + * <ibm-select [(ngModel)]="model"> |
| 19 | + * <option value="default" disabled selected hidden>Choose an option</option> |
| 20 | + * <option value="option1">Option 1</option> |
| 21 | + * <option value="option2">Option 2</option> |
| 22 | + * <option value="option3">Option 3</option> |
| 23 | + * </ibm-select> |
| 24 | + * ``` |
| 25 | +
|
| 26 | + */ |
3 | 27 | @Component({ |
4 | 28 | selector: "ibm-select", |
5 | 29 | template: ` |
6 | 30 | <div class="bx--form-item"> |
7 | 31 | <div class="bx--select"> |
8 | 32 | <label [attr.for]="id" class="bx--label">{{label}}</label> |
9 | | - <select [attr.id]="id" class="bx--select-input"> |
| 33 | + <select |
| 34 | + #select |
| 35 | + [attr.id]="id" |
| 36 | + [disabled]="disabled" |
| 37 | + (change)="onChange($event)" |
| 38 | + class="bx--select-input"> |
10 | 39 | <ng-content></ng-content> |
11 | 40 | </select> |
12 | 41 | <svg class="bx--select__arrow" width="10" height="5" viewBox="0 0 10 5"> |
13 | 42 | <path d="M0 0l5 4.998L10 0z" fill-rule="evenodd" /> |
14 | 43 | </svg> |
15 | 44 | </div> |
16 | 45 | </div> |
17 | | - ` |
| 46 | + `, |
| 47 | + providers: [ |
| 48 | + { |
| 49 | + provide: NG_VALUE_ACCESSOR, |
| 50 | + useExisting: Select, |
| 51 | + multi: true |
| 52 | + } |
| 53 | + ] |
18 | 54 | }) |
19 | | -export class Select { |
| 55 | +export class Select implements ControlValueAccessor { |
| 56 | + /** |
| 57 | + * Tracks the total number of selects instantiated. Used to generate unique IDs |
| 58 | + */ |
20 | 59 | static selectCount = 0; |
| 60 | + /** |
| 61 | + * Label for the select. Appears above the input. |
| 62 | + */ |
21 | 63 | @Input() label = "Select label"; |
| 64 | + /** |
| 65 | + * Sets the unique ID. Defaults to `select-${total count of selects instantiated}` |
| 66 | + */ |
22 | 67 | @Input() id = `select-${Select.selectCount++}`; |
| 68 | + /** |
| 69 | + * Set to true to disable component. |
| 70 | + */ |
| 71 | + @Input() disabled = false; |
| 72 | + /** |
| 73 | + * emits the selected options `value` |
| 74 | + */ |
| 75 | + @Output() selected = new EventEmitter(); |
| 76 | + |
| 77 | + @ViewChild("select") select: ElementRef; |
| 78 | + |
| 79 | + get value() { |
| 80 | + return this.select.nativeElement.value; |
| 81 | + } |
| 82 | + |
| 83 | + set value(v) { |
| 84 | + this.select.nativeElement.value = v; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Receives a value from the model. |
| 89 | + */ |
| 90 | + writeValue(obj: any) { |
| 91 | + this.value = obj; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Registers a listener that notifies the model when the control updates |
| 96 | + */ |
| 97 | + registerOnChange(fn: any) { |
| 98 | + this.onChangeHandler = fn; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Registers a listener that notifies the model when the control is blurred |
| 103 | + */ |
| 104 | + registerOnTouched(fn: any) { |
| 105 | + this.onTouchedHandler = fn; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the disabled state through the model |
| 110 | + */ |
| 111 | + setDisabledState(isDisabled: boolean) { |
| 112 | + this.disabled = isDisabled; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Handles the change event from the `select`. |
| 117 | + * Sends events to the change handler and emits a `selected` event. |
| 118 | + */ |
| 119 | + onChange(event) { |
| 120 | + this.onChangeHandler(event.target.value); |
| 121 | + this.selected.emit(event.target.value); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * placeholder declarations. Replaced by the functions provided to `registerOnChange` and `registerOnTouched` |
| 126 | + */ |
| 127 | + private onChangeHandler = (_: any) => { }; |
| 128 | + private onTouchedHandler = () => { }; |
| 129 | + |
| 130 | + /** |
| 131 | + * Listens for the host blurring, and notifies the model |
| 132 | + */ |
| 133 | + @HostListener("blur") |
| 134 | + private blur() { |
| 135 | + this.onTouchedHandler(); |
| 136 | + } |
23 | 137 | } |
0 commit comments