Skip to content

Commit 3cbed35

Browse files
feat: add readonly state for select (#2977)
Co-authored-by: Akshat Patel <[email protected]>
1 parent a498763 commit 3cbed35

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/select/select.component.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/select/select.stories.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
args: {
2727
skeleton: false,
2828
disabled: false,
29+
readonly: false,
2930
invalid: false,
3031
invalidText: "Please select an option.",
3132
warn: false,
@@ -63,6 +64,7 @@ const Template = (args) => ({
6364
<cds-select
6465
[skeleton]="skeleton"
6566
[disabled]="disabled"
67+
[readonly]="readonly"
6668
[size]="size"
6769
[invalid]="invalid"
6870
[invalidText]="invalidText"
@@ -95,6 +97,7 @@ const NgModelTemplate = (args) => ({
9597
[(ngModel)]="model"
9698
[skeleton]="skeleton"
9799
[disabled]="disabled"
100+
[readonly]="readonly"
98101
[size]="size"
99102
[invalid]="invalid"
100103
[invalidText]="invalidText"

0 commit comments

Comments
 (0)