Skip to content

Commit 0532bd9

Browse files
feat: fluid state for select (#3009)
Signed-off-by: Akshat Patel <[email protected]> Co-authored-by: Akshat Patel <[email protected]> Co-authored-by: Akshat Patel <[email protected]>
1 parent c42963f commit 0532bd9

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/select/select.component.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,23 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
3232
@Component({
3333
selector: "cds-select, ibm-select",
3434
template: `
35-
<div class="cds--form-item">
36-
<ng-template [ngIf]="skeleton">
35+
<div
36+
[ngClass]="{
37+
'cds--form-item': !skeleton,
38+
'cds--select--fluid': fluid && !skeleton
39+
}">
40+
<ng-container *ngIf="skeleton && !fluid">
3741
<div *ngIf="label" class="cds--label cds--skeleton"></div>
3842
<div class="cds--select cds--skeleton"></div>
39-
</ng-template>
43+
</ng-container>
44+
<ng-container *ngIf="skeleton && fluid">
45+
<div class="cds--list-box__wrapper--fluid">
46+
<div class="cds--list-box cds--skeleton">
47+
<div class="cds--list-box__label"></div>
48+
<div class="cds--list-box__field"></div>
49+
</div>
50+
</div>
51+
</ng-container>
4052
<div
4153
*ngIf="!skeleton"
4254
class="cds--select"
@@ -46,7 +58,9 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
4658
'cds--select--invalid': invalid,
4759
'cds--select--warning': warn,
4860
'cds--select--disabled': disabled,
49-
'cds--select--readonly': readonly
61+
'cds--select--readonly': readonly,
62+
'cds--select--fluid--invalid': fluid && invalid,
63+
'cds--select--fluid--focus': fluid && focused
5064
}">
5165
<label
5266
*ngIf="label"
@@ -60,7 +74,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
6074
<ng-container *ngTemplateOutlet="noInline"></ng-container>
6175
</div>
6276
<div
63-
*ngIf="helperText"
77+
*ngIf="helperText && !invalid && !warn && !skeleton && !fluid"
6478
class="cds--form__helper-text"
6579
[ngClass]="{
6680
'cds--form__helper-text--disabled': disabled
@@ -89,7 +103,9 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
89103
'cds--select-input--lg': size === 'lg'
90104
}"
91105
(mousedown)="onMouseDown($event)"
92-
(keydown)="onKeyDown($event)">
106+
(keydown)="onKeyDown($event)"
107+
(focus)="fluid ? handleFocus($event) : null"
108+
(blur)="fluid ? handleFocus($event) : null">
93109
<ng-content></ng-content>
94110
</select>
95111
<svg
@@ -116,16 +132,30 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
116132
size="16"
117133
class="cds--select__invalid-icon cds--select__invalid-icon--warning">
118134
</svg>
135+
<ng-container *ngIf="fluid">
136+
<hr class="cds--select__divider" />
137+
<div
138+
*ngIf="invalid && invalidText" role="alert" class="cds--form-requirement" aria-live="polite">
139+
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
140+
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
141+
</div>
142+
<div *ngIf="!invalid && warn" class="cds--form-requirement">
143+
<ng-container *ngIf="!isTemplate(warnText)">{{warnText}}</ng-container>
144+
<ng-template *ngIf="isTemplate(warnText)" [ngTemplateOutlet]="warnText"></ng-template>
145+
</div>
146+
</ng-container>
119147
</div>
120-
<div
121-
*ngIf="invalid && invalidText" role="alert" class="cds--form-requirement" aria-live="polite">
122-
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
123-
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
124-
</div>
125-
<div *ngIf="!invalid && warn" class="cds--form-requirement">
126-
<ng-container *ngIf="!isTemplate(warnText)">{{warnText}}</ng-container>
127-
<ng-template *ngIf="isTemplate(warnText)" [ngTemplateOutlet]="warnText"></ng-template>
128-
</div>
148+
<ng-container *ngIf="!fluid">
149+
<div
150+
*ngIf="invalid && invalidText" role="alert" class="cds--form-requirement" aria-live="polite">
151+
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
152+
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
153+
</div>
154+
<div *ngIf="!invalid && warn" class="cds--form-requirement">
155+
<ng-container *ngIf="!isTemplate(warnText)">{{warnText}}</ng-container>
156+
<ng-template *ngIf="isTemplate(warnText)" [ngTemplateOutlet]="warnText"></ng-template>
157+
</div>
158+
</ng-container>
129159
</ng-template>
130160
`,
131161
providers: [
@@ -209,10 +239,17 @@ export class Select implements ControlValueAccessor, AfterViewInit {
209239
@Input() theme: "light" | "dark" = "dark";
210240
@Input() ariaLabel: string;
211241

242+
/**
243+
* Experimental: enable fluid state
244+
*/
245+
@Input() fluid = false;
246+
212247
@Output() valueChange = new EventEmitter();
213248

214249
@ViewChild("select") select: ElementRef;
215250

251+
focused = false;
252+
216253
protected _value;
217254

218255
ngAfterViewInit() {
@@ -296,9 +333,14 @@ export class Select implements ControlValueAccessor, AfterViewInit {
296333
}
297334
}
298335

336+
handleFocus(event: FocusEvent) {
337+
this.focused = event.type === "focus";
338+
}
339+
299340
/**
300341
* placeholder declarations. Replaced by the functions provided to `registerOnChange` and `registerOnTouched`
301342
*/
302343
protected onChangeHandler = (_: any) => { };
303344
protected onTouchedHandler = () => { };
345+
304346
}

src/select/select.stories.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default {
3535
helperText: "Optional helper text",
3636
size: "md",
3737
theme: "dark",
38-
display: "default"
38+
display: "default",
39+
fluid: false
3940
},
4041
argTypes: {
4142
size: {
@@ -74,6 +75,7 @@ const Template = (args) => ({
7475
[helperText]="helperText"
7576
[theme]="theme"
7677
[(ngModel)]="model"
78+
[fluid]="fluid"
7779
[display]="display">
7880
<option value="default" disabled selected hidden>Choose an option</option>
7981
<option value="solong">A much longer option that is worth having around to check how text flows</option>
@@ -90,6 +92,11 @@ const Template = (args) => ({
9092
});
9193
export const Basic = Template.bind({});
9294

95+
export const Fluid = Template.bind({});
96+
Fluid.args = {
97+
fluid: true
98+
};
99+
93100
const NgModelTemplate = (args) => ({
94101
props: args,
95102
template: `
@@ -107,6 +114,7 @@ const NgModelTemplate = (args) => ({
107114
[helperText]="helperText"
108115
[theme]="theme"
109116
[(ngModel)]="model"
117+
[fluid]="fluid"
110118
[display]="display"
111119
ariaLabel='ngModel select'>
112120
<option value="default" disabled selected hidden>Choose an option</option>

0 commit comments

Comments
 (0)