@@ -45,7 +45,8 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
4545 'cds--select--light': theme === 'light',
4646 'cds--select--invalid': invalid,
4747 'cds--select--warning': warn,
48- 'cds--select--disabled': disabled
48+ 'cds--select--disabled': disabled,
49+ 'cds--select--readonly': readonly
4950 }">
5051 <label
5152 *ngIf="label"
@@ -80,12 +81,15 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
8081 [disabled]="disabled"
8182 (change)="onChange($event)"
8283 [attr.aria-invalid]="invalid ? 'true' : null"
84+ [attr.aria-readonly]="readonly ? 'true' : null"
8385 class="cds--select-input"
8486 [ngClass]="{
8587 'cds--select-input--sm': size === 'sm',
8688 'cds--select-input--md': size === 'md',
8789 'cds--select-input--lg': size === 'lg'
88- }">
90+ }"
91+ (mousedown)="onMouseDown($event)"
92+ (keydown)="onKeyDown($event)">
8993 <ng-content></ng-content>
9094 </select>
9195 <svg
@@ -182,6 +186,10 @@ export class Select implements ControlValueAccessor, AfterViewInit {
182186 * Set to `true` for an invalid select component.
183187 */
184188 @Input ( ) invalid = false ;
189+ /**
190+ * Set to `true` for readonly state.
191+ */
192+ @Input ( ) readonly = false ;
185193
186194 /**
187195 * @deprecated since v5 - Use `cdsLayer` directive instead
@@ -273,4 +281,24 @@ export class Select implements ControlValueAccessor, AfterViewInit {
273281 */
274282 protected onChangeHandler = ( _ : any ) => { } ;
275283 protected onTouchedHandler = ( ) => { } ;
284+
285+ onMouseDown ( event : MouseEvent ) {
286+ /**
287+ * This prevents the select from opening with mouse
288+ */
289+ if ( this . readonly ) {
290+ event . preventDefault ( ) ;
291+ ( < HTMLElement > event . target ) . focus ( ) ;
292+ }
293+ }
294+
295+ onKeyDown ( event : KeyboardEvent ) {
296+ const selectAccessKeys = [ "ArrowDown" , "ArrowUp" , "ArrowLeft" , "ArrowRight" , " " ] ;
297+ /**
298+ * This prevents the select from opening for the above keys
299+ */
300+ if ( this . readonly && selectAccessKeys . includes ( event . key ) ) {
301+ event . preventDefault ( ) ;
302+ }
303+ }
276304}
0 commit comments