Skip to content

Commit 0f019df

Browse files
authored
Merge pull request #2411 from Akshat55/datepicker-helpertext
feat: Add helper text attribute to datepicker
2 parents 988d8ce + 8a508f5 commit 0f019df

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/datepicker-input/datepicker-input.component.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ import { NG_VALUE_ACCESSOR } from "@angular/forms";
6767
class="bx--date-picker__icon bx--date-picker__icon--warn">
6868
</svg>
6969
</div>
70+
<div
71+
*ngIf="helperText && !invalid && !warn"
72+
class="bx--form__helper-text"
73+
[ngClass]="{'bx--form__helper-text--disabled': disabled}">
74+
<ng-container *ngIf="!isTemplate(helperText)">{{helperText}}</ng-container>
75+
<ng-template *ngIf="isTemplate(helperText)" [ngTemplateOutlet]="helperText"></ng-template>
76+
</div>
7077
<div *ngIf="!warn && invalid" class="bx--form-requirement">
71-
<ng-container *ngIf="!isTemplate(invalidText)">{{ invalidText }}</ng-container>
78+
<ng-container *ngIf="!isTemplate(invalidText)">{{invalidText}}</ng-container>
7279
<ng-template *ngIf="isTemplate(invalidText)" [ngTemplateOutlet]="invalidText"></ng-template>
7380
</div>
7481
<div *ngIf="!invalid && warn" class="bx--form-requirement">
@@ -127,6 +134,11 @@ export class DatePickerInput {
127134
*/
128135
@Input() warnText: string | TemplateRef<any>;
129136

137+
/**
138+
* Sets the optional helper text.
139+
*/
140+
@Input() helperText: string | TemplateRef<any>;
141+
130142
@Input() skeleton = false;
131143

132144
@Input() value = "";
@@ -136,7 +148,7 @@ export class DatePickerInput {
136148
// @ts-ignore
137149
@ViewChild("input", { static: false }) input: ElementRef;
138150

139-
constructor(protected elementRef: ElementRef) {}
151+
constructor(protected elementRef: ElementRef) { }
140152

141153
onChange(event) {
142154
this.value = event.target.value;
@@ -157,9 +169,9 @@ export class DatePickerInput {
157169
this.onTouched = fn;
158170
}
159171

160-
onTouched: () => any = () => {};
172+
onTouched: () => any = () => { };
161173

162-
propagateChange = (_: any) => {};
174+
propagateChange = (_: any) => { };
163175

164176
public isTemplate(value) {
165177
return value instanceof TemplateRef;

src/datepicker/datepicker.component.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { I18n } from "carbon-components-angular/i18n";
5959
[warn]="warn"
6060
[warnText]="warnText"
6161
[skeleton]="skeleton"
62+
[helperText]="helperText"
6263
(valueChange)="onValueChange($event)"
6364
(click)="openCalendar(input)">
6465
</ibm-date-picker-input>
@@ -80,6 +81,7 @@ import { I18n } from "carbon-components-angular/i18n";
8081
[warn]="rangeWarn"
8182
[warnText]="rangeWarnText"
8283
[skeleton]="skeleton"
84+
[helperText]="rangeHelperText"
8385
(valueChange)="onRangeValueChange($event)"
8486
(click)="openCalendar(rangeInput)">
8587
</ibm-date-picker-input>
@@ -130,8 +132,9 @@ export class DatePicker implements
130132
*/
131133
@Input() language = "en";
132134

133-
@Input() label: string | TemplateRef<any>;
134-
135+
@Input() label: string | TemplateRef<any>;
136+
@Input() helperText: string | TemplateRef<any>;
137+
@Input() rangeHelperText: string | TemplateRef<any>;
135138
@Input() rangeLabel: string;
136139

137140
@Input() placeholder = "mm/dd/yyyy";
@@ -220,7 +223,7 @@ export class DatePicker implements
220223
get flatpickrOptions() {
221224
const plugins = [...this.plugins, carbonFlatpickrMonthSelectPlugin];
222225
if (this.range) {
223-
plugins.push(rangePlugin({ input: `#${this.id}-rangeInput`, position: "left"}));
226+
plugins.push(rangePlugin({ input: `#${this.id}-rangeInput`, position: "left" }));
224227
}
225228
return Object.assign({}, this._flatpickrOptions, this.flatpickrBaseOptions, {
226229
mode: this.range ? "range" : "single",
@@ -407,9 +410,9 @@ export class DatePicker implements
407410
this.onTouched = fn;
408411
}
409412

410-
onTouched: () => any = () => {};
413+
onTouched: () => any = () => { };
411414

412-
propagateChange = (_: any) => {};
415+
propagateChange = (_: any) => { };
413416

414417
/**
415418
* Cleans up our flatpickr instance

src/datepicker/datepicker.stories.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ storiesOf("Components|Date Picker", module)
194194
[warn]="warn"
195195
[warnText]="warnText"
196196
[dateFormat]="dateFormat"
197-
(valueChange)="valueChange($event)">
197+
(valueChange)="valueChange($event)"
198+
[helperText]="helperText">
198199
</ibm-date-picker>
199200
<p style="margin-top: 20px;">Without initial value</p>
200201
<ibm-date-picker
@@ -209,7 +210,8 @@ storiesOf("Components|Date Picker", module)
209210
[warn]="warn"
210211
[warnText]="warnText"
211212
[dateFormat]="dateFormat"
212-
(valueChange)="valueChange($event)">
213+
(valueChange)="valueChange($event)"
214+
[helperText]="helperText">
213215
</ibm-date-picker>
214216
`,
215217
props: {
@@ -222,6 +224,7 @@ storiesOf("Components|Date Picker", module)
222224
invalidText: text("Form validation content", "Invalid date format"),
223225
warn: boolean("Show the warning state", false),
224226
warnText: text("Text for the warning", "This is a warning"),
227+
helperText: text("Helper text", ""),
225228
invalid: boolean("Show form validation", false),
226229
disabled: boolean("Disabled", false),
227230
dateFormat: text("Date format", "m/d/Y"),
@@ -249,7 +252,8 @@ storiesOf("Components|Date Picker", module)
249252
[rangeInvalidText]="invalidText"
250253
[dateFormat]="dateFormat"
251254
[value]="value"
252-
(valueChange)="valueChange($event)">
255+
(valueChange)="valueChange($event)"
256+
[helperText]="helperText">
253257
</ibm-date-picker>
254258
<p style="margin-top: 20px;">Without initial value</p>
255259
<ibm-date-picker
@@ -268,7 +272,8 @@ storiesOf("Components|Date Picker", module)
268272
[rangeWarn]="warn"
269273
[rangeWarnText]="warnText"
270274
[dateFormat]="dateFormat"
271-
(valueChange)="valueChange($event)">
275+
(valueChange)="valueChange($event)"
276+
[helperText]="helperText">
272277
</ibm-date-picker>
273278
`,
274279
props: {
@@ -281,6 +286,7 @@ storiesOf("Components|Date Picker", module)
281286
invalidText: text("Form validation content", "Invalid date format"),
282287
warn: boolean("Show the warning state", false),
283288
warnText: text("Text for the warning", "This is a warning"),
289+
helperText: text("Helper text", ""),
284290
invalid: boolean("Show form validation", false),
285291
disabled: boolean("Disabled", false),
286292
dateFormat: text("Date format", "m/d/Y"),

0 commit comments

Comments
 (0)