Skip to content

Commit 02a8cd3

Browse files
committed
fix(date-inputs): Remove alwaysLeadingZero prop. Update tests.
1 parent 878bb10 commit 02a8cd3

File tree

7 files changed

+35
-71
lines changed

7 files changed

+35
-71
lines changed

src/components/date-picker/date-picker.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('Date picker', () => {
5555

5656
beforeEach(async () => {
5757
picker = await fixture<IgcDatePickerComponent>(
58-
html`<igc-date-picker always-leading-zero></igc-date-picker>`
58+
html`<igc-date-picker></igc-date-picker>`
5959
);
6060
dateTimeInput = picker.renderRoot.querySelector(
6161
IgcDateTimeInputComponent.tagName
@@ -581,20 +581,26 @@ describe('Date picker', () => {
581581
it('should use the value of locale format for displayFormat, if it is not defined', async () => {
582582
expect(picker.locale).to.equal('en-US');
583583
expect(picker.getAttribute('display-format')).to.be.null;
584-
expect(picker.displayFormat).to.equal(picker.inputFormat);
584+
expect(picker.displayFormat).to.equal('M/d/yyyy');
585585

586586
// updates inputFormat according to changed locale
587587
picker.locale = 'fr';
588588
await elementUpdated(picker);
589589
expect(picker.inputFormat).to.equal('dd/MM/yyyy');
590-
expect(picker.displayFormat).to.equal(picker.inputFormat);
590+
expect(picker.displayFormat).to.equal('dd/MM/yyyy');
591591

592592
// sets inputFormat as attribute
593593
picker.setAttribute('input-format', 'dd-MM-yyyy');
594594
await elementUpdated(picker);
595595

596596
expect(picker.inputFormat).to.equal('dd-MM-yyyy');
597597
expect(picker.displayFormat).to.equal(picker.inputFormat);
598+
599+
// changing locale after setting input format shouldn't affect it
600+
picker.locale = 'de';
601+
await elementUpdated(picker);
602+
expect(picker.inputFormat).to.equal('dd-MM-yyyy');
603+
expect(picker.displayFormat).to.equal(picker.inputFormat);
598604
});
599605
});
600606

src/components/date-picker/date-picker.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,6 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
420420
@property({ type: Boolean, reflect: true, attribute: 'show-week-numbers' })
421421
public showWeekNumbers = false;
422422

423-
/**
424-
* Sets to always show leading zero regardless of the displayFormat applied or one based on locale.
425-
* Leading zero is applied during edit for the inputFormat always, regardless of this option.
426-
* @attr
427-
*/
428-
@property({ type: Boolean, attribute: 'always-leading-zero' })
429-
public alwaysLeadingZero = false;
430-
431423
/**
432424
* Format to display the value in when not editing.
433425
* Defaults to the locale format if not set.
@@ -862,7 +854,6 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
862854
label=${bindIf(this._isMaterial, this.label)}
863855
input-format=${ifDefined(this._inputFormat)}
864856
display-format=${ifDefined(format)}
865-
?always-leading-zero=${this.alwaysLeadingZero}
866857
?disabled=${this.disabled}
867858
?readonly=${readOnly}
868859
?required=${this.required}

src/components/date-range-picker/date-range-picker-single.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('Date range picker - single input', () => {
4242

4343
beforeEach(async () => {
4444
picker = await fixture<IgcDateRangePickerComponent>(
45-
html`<igc-date-range-picker always-leading-zero></igc-date-range-picker>`
45+
html`<igc-date-range-picker></igc-date-range-picker>`
4646
);
4747
input = picker.renderRoot.querySelector(IgcInputComponent.tagName)!;
4848

@@ -205,21 +205,21 @@ describe('Date range picker - single input', () => {
205205
});
206206

207207
it('should update the masked value with the locale', async () => {
208-
expect(picker.displayFormat).to.equal(picker.inputFormat);
208+
expect(picker.displayFormat).to.equal('M/d/yyyy');
209209

210210
picker.value = {
211211
start: CalendarDay.from(new Date(2025, 3, 9)).native,
212212
end: CalendarDay.from(new Date(2025, 3, 10)).native,
213213
};
214214
await elementUpdated(picker);
215215
const input = picker.renderRoot.querySelector(IgcInputComponent.tagName)!;
216-
expect(input.value).to.equal('04/09/2025 - 04/10/2025');
216+
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
217217

218218
picker.locale = 'bg';
219219
await elementUpdated(picker);
220220

221221
expect(input.value.normalize('NFKC')).to.equal(
222-
'09.04.2025 г. - 10.04.2025 г.'
222+
'9.04.2025 г. - 10.04.2025 г.'
223223
);
224224
});
225225
it('should set the default placeholder of the single input to the input format (like dd/MM/yyyy - dd/MM/yyyy)', async () => {
@@ -238,7 +238,7 @@ describe('Date range picker - single input', () => {
238238
it('should set the mask of the single input per the display format (like dd/MM/yyyy - dd/MM/yyyy)', async () => {
239239
picker.useTwoInputs = false;
240240
await elementUpdated(picker);
241-
expect(picker.displayFormat).to.equal(picker.inputFormat);
241+
expect(picker.displayFormat).to.equal('M/d/yyyy');
242242

243243
picker.value = {
244244
start: CalendarDay.from(new Date(2025, 3, 9)).native,
@@ -247,7 +247,7 @@ describe('Date range picker - single input', () => {
247247
await elementUpdated(picker);
248248

249249
const input = picker.renderRoot.querySelector(IgcInputComponent.tagName)!;
250-
expect(input.value).to.equal('04/09/2025 - 04/10/2025');
250+
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
251251

252252
picker.displayFormat = 'yyyy-MM-dd';
253253
await elementUpdated(picker);
@@ -264,7 +264,7 @@ describe('Date range picker - single input', () => {
264264
};
265265
await elementUpdated(picker);
266266

267-
expect(input.value).to.equal('04/09/2025 - 04/10/2025');
267+
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
268268

269269
picker.clear();
270270
await elementUpdated(picker);
@@ -288,7 +288,7 @@ describe('Date range picker - single input', () => {
288288
start,
289289
end,
290290
});
291-
expect(input.value).to.equal('04/09/2025 - 04/10/2025');
291+
expect(input.value).to.equal('4/9/2025 - 4/10/2025');
292292
expect(eventSpy).not.called;
293293
});
294294
});

src/components/date-range-picker/date-range-picker.utils.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ export const checkSelectedRange = (
5555
? DateTimeUtil.formatDisplayDate(
5656
expectedValue.start,
5757
picker.locale,
58-
picker.displayFormat,
59-
picker.alwaysLeadingZero
58+
picker.displayFormat
6059
)
6160
: '';
6261
const end = expectedValue?.end
6362
? DateTimeUtil.formatDisplayDate(
6463
expectedValue.end,
6564
picker.locale,
66-
picker.displayFormat,
67-
picker.alwaysLeadingZero
65+
picker.displayFormat
6866
)
6967
: '';
7068
expect(input.value).to.equal(`${start} - ${end}`);

src/components/date-time-input/date-time-input.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Date Time Input component', () => {
4141
describe('', async () => {
4242
beforeEach(async () => {
4343
el = await fixture<IgcDateTimeInputComponent>(
44-
html`<igc-date-time-input always-leading-zero></igc-date-time-input>`
44+
html`<igc-date-time-input></igc-date-time-input>`
4545
);
4646
input = el.renderRoot.querySelector('input')!;
4747
parser.prompt = defaultPrompt;
@@ -60,18 +60,20 @@ describe('Date Time Input component', () => {
6060
await elementUpdated(el);
6161
expect(el.placeholder).to.equal('dd.MM.yyyy');
6262
expect(el.inputFormat).to.equal('dd.MM.yyyy');
63+
expect(el.displayFormat).to.equal('d.M.yyyy');
6364
});
6465

6566
it('should update inputFormat with value according to locale', async () => {
6667
el.value = new Date(2020, 2, 3);
6768
await elementUpdated(el);
68-
expect(input.value).to.equal('03/03/2020');
69+
expect(input.value).to.equal('3/3/2020');
6970

7071
el.locale = 'no';
7172
await elementUpdated(el);
7273
expect(el.placeholder).to.equal('dd.MM.yyyy');
7374
expect(el.inputFormat).to.equal('dd.MM.yyyy');
74-
expect(input.value).to.equal('03.03.2020');
75+
expect(el.displayFormat).to.equal('d.M.yyyy');
76+
expect(input.value).to.equal('3.3.2020');
7577
});
7678

7779
it('should use inputFormat if no displayFormat is defined - issue 1114', async () => {
@@ -108,13 +110,12 @@ describe('Date Time Input component', () => {
108110
el.value = new Date(2020, 2, 3);
109111
await elementUpdated(el);
110112

111-
expect(input.value).to.equal('03/03/2020');
113+
expect(input.value).to.equal('3/3/2020');
112114

113115
el.displayFormat = 'dd.MM/yyyy';
114116
await elementUpdated(el);
115117
expect(input.value).to.equal('03.03/2020');
116118

117-
el.alwaysLeadingZero = false;
118119
el.displayFormat = 'd.M';
119120
await elementUpdated(el);
120121
expect(input.value).to.equal('3.3');
@@ -201,7 +202,7 @@ describe('Date Time Input component', () => {
201202
await elementUpdated(el);
202203

203204
expect(eventSpy).calledWith('igcChange');
204-
expect(input.value).to.deep.equal('01/01/2000');
205+
expect(input.value).to.deep.equal('1/1/2000');
205206
});
206207

207208
it('should correctly switch between different pre-defined date formats', async () => {
@@ -275,7 +276,7 @@ describe('Date Time Input component', () => {
275276
it('should clear input date on clear', async () => {
276277
el.value = new Date(2020, 2, 3);
277278
await elementUpdated(el);
278-
expect(input.value).to.equal('03/03/2020');
279+
expect(input.value).to.equal('3/3/2020');
279280

280281
el.clear();
281282
await elementUpdated(el);
@@ -831,7 +832,7 @@ describe('Date Time Input component', () => {
831832
it('Drop behavior', async () => {
832833
el.value = new Date(2020, 2, 3);
833834
await elementUpdated(el);
834-
expect(input.value).to.equal('03/03/2020');
835+
expect(input.value).to.equal('3/3/2020');
835836

836837
input.value = '1010';
837838
input.setSelectionRange(0, 4);

src/components/date-time-input/date-time-input.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,6 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
175175
return this._max;
176176
}
177177

178-
/**
179-
* Sets to always show leading zero regardless of the displayFormat applied or one based on locale.
180-
* Leading zero is applied during edit for the inputFormat always, regardless of this option.
181-
* @attr
182-
*/
183-
@property({ type: Boolean, attribute: 'always-leading-zero' })
184-
public alwaysLeadingZero = false;
185-
186178
/**
187179
* Format to display the value in when not editing.
188180
* Defaults to the locale format if not set.
@@ -234,11 +226,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
234226
this.updateDefaultMask();
235227
this.setMask(this._defaultMask);
236228
}
237-
}
238229

239-
@watch('alwaysLeadingZero', { waitUntilFirstUpdate: true })
240-
protected setAlwaysLeadingZero(): void {
241-
this.updateDefaultDisplayFormat();
242230
if (this.value) {
243231
this.updateMask();
244232
}
@@ -255,11 +243,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
255243
protected get hasDateParts(): boolean {
256244
const parts =
257245
this._inputDateParts ||
258-
DateTimeUtil.parseDateTimeFormat(
259-
this.inputFormat,
260-
this.locale,
261-
this.alwaysLeadingZero
262-
);
246+
DateTimeUtil.parseDateTimeFormat(this.inputFormat, this.locale);
263247

264248
return parts.some(
265249
(p) =>
@@ -272,11 +256,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
272256
protected get hasTimeParts(): boolean {
273257
const parts =
274258
this._inputDateParts ||
275-
DateTimeUtil.parseDateTimeFormat(
276-
this.inputFormat,
277-
this.locale,
278-
this.alwaysLeadingZero
279-
);
259+
DateTimeUtil.parseDateTimeFormat(this.inputFormat, this.locale);
280260
return parts.some(
281261
(p) =>
282262
p.type === DateParts.Hours ||
@@ -389,8 +369,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
389369
this._maskedValue = DateTimeUtil.formatDisplayDate(
390370
this.value,
391371
this.locale,
392-
this.displayFormat,
393-
this.alwaysLeadingZero
372+
this.displayFormat
394373
);
395374
}
396375
}
@@ -512,8 +491,7 @@ export default class IgcDateTimeInputComponent extends EventEmitterMixin<
512491

513492
private updateDefaultDisplayFormat(): void {
514493
this._defaultDisplayFormat = getDateFormatter().getLocaleDateTimeFormat(
515-
this.locale,
516-
this.alwaysLeadingZero
494+
this.locale
517495
);
518496
}
519497

src/components/date-time-input/date-util.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,14 @@ export abstract class DateTimeUtil {
198198
* @param locale Locale of the component
199199
* @param displayFormat Display format specified by the user. Can be undefined.
200200
* @param inputFormat Input format, so it is not calculated again and used for leading zero format.
201-
* @param leadingZero Should leading zero be applied?
202201
* @returns
203202
*/
204203
public static formatDisplayDate(
205204
value: Date,
206205
locale: string,
207-
displayFormat: string | undefined,
208-
leadingZero = false
206+
displayFormat: string | undefined
209207
): string {
210-
let options: Intl.DateTimeFormatOptions = {};
208+
const options: Intl.DateTimeFormatOptions = {};
211209
switch (displayFormat) {
212210
case 'short':
213211
case 'long':
@@ -233,19 +231,11 @@ export abstract class DateTimeUtil {
233231
return getDateFormatter().formatDateCustomFormat(
234232
value,
235233
displayFormat,
236-
{ locale, forceLeadingZero: leadingZero }
234+
{ locale }
237235
);
238236
}
239237
}
240238

241-
if (leadingZero && !displayFormat) {
242-
options = {
243-
day: '2-digit',
244-
month: '2-digit',
245-
year: 'numeric',
246-
} as Intl.DateTimeFormatOptions;
247-
}
248-
249239
return getDateFormatter().formatDateTime(value, locale, options);
250240
}
251241

0 commit comments

Comments
 (0)