Skip to content

Commit 6d02c8e

Browse files
committed
fix(date-picker): Validators
1 parent 33138c7 commit 6d02c8e

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

src/components/common/mixins/forms/form-value.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { convertToDate, getDateFormValue } from '../../../calendar/helpers.js';
12
import { asNumber } from '../../util.js';
23
import type { FormValueType, IgcFormControl } from './types.js';
34

@@ -38,6 +39,14 @@ export const defaultNumberTransformers: FormValueTransformers<number> = {
3839
setFormValue: (value) => value.toString(),
3940
};
4041

42+
export const defaultDateTimeTransformers: Partial<
43+
FormValueTransformers<Date | null>
44+
> = {
45+
setValue: convertToDate,
46+
setDefaultValue: convertToDate,
47+
setFormValue: getDateFormValue,
48+
};
49+
4150
export class FormValue<T> {
4251
private _host: IgcFormControl;
4352
private _value: T;

src/components/common/validators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ export const minDateValidator: Validator<{
118118
key: 'rangeUnderflow',
119119
message: ({ min }) => formatString(validatorMessages.min, min),
120120
isValid: ({ value, min }) =>
121-
min
122-
? !DateTimeUtil.lessThanMinValue(value ?? new Date(), min, false, true)
121+
value && min
122+
? !DateTimeUtil.lessThanMinValue(value, min, false, true)
123123
: true,
124124
};
125125

@@ -130,7 +130,7 @@ export const maxDateValidator: Validator<{
130130
key: 'rangeOverflow',
131131
message: ({ max }) => formatString(validatorMessages.max, max),
132132
isValid: ({ value, max }) =>
133-
max
134-
? !DateTimeUtil.greaterThanMaxValue(value ?? new Date(), max, false, true)
133+
value && max
134+
? !DateTimeUtil.greaterThanMaxValue(value, max, false, true)
135135
: true,
136136
};

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -950,42 +950,57 @@ describe('Date picker', () => {
950950
});
951951

952952
it('should enforce min value constraint', () => {
953+
// No value - submit passes
953954
spec.setProperties({ min: new Date(2026, 0, 1) });
954-
spec.assertSubmitFails();
955+
spec.assertSubmitPasses();
955956

957+
// Invalid min constraint
956958
spec.setProperties({ value: new Date(2022, 0, 1) });
957959
spec.assertSubmitFails();
958960

961+
// Valid value
959962
spec.setProperties({ value: new Date(2026, 0, 2) });
960963
spec.assertSubmitPasses();
961964
});
962965

963966
it('should enforce max value constraint', () => {
964-
spec.setProperties({ max: new Date(2020, 0, 1), value: today.native });
967+
// No value - submit passes
968+
spec.setProperties({ max: new Date(2020, 0, 1) });
969+
spec.assertSubmitPasses();
970+
971+
// Invalid max constraint
972+
spec.setProperties({ value: today.native });
965973
spec.assertSubmitFails();
966974

975+
// Valid value
967976
spec.setProperties({ value: new Date(2020, 0, 1) });
968977
spec.assertSubmitPasses();
969978
});
970979

971980
it('should enforce min value constraint with string property', () => {
981+
// No value - submit passes
972982
spec.setProperties({ min: new Date(2026, 0, 1).toISOString() });
973-
spec.assertSubmitFails();
983+
spec.assertSubmitPasses();
974984

985+
// Invalid min constraint
975986
spec.setProperties({ value: new Date(2022, 0, 1).toISOString() });
976987
spec.assertSubmitFails();
977988

989+
// Valid value
978990
spec.setProperties({ value: new Date(2026, 0, 2).toISOString() });
979991
spec.assertSubmitPasses();
980992
});
981993

982994
it('should enforce max value constraint with string property', () => {
983-
spec.setProperties({
984-
max: new Date(2020, 0, 1).toISOString(),
985-
value: today.native,
986-
});
995+
// No value - submit passes
996+
spec.setProperties({ max: new Date(2020, 0, 1).toISOString() });
997+
spec.assertSubmitPasses();
998+
999+
// Invalid max constraint
1000+
spec.setProperties({ value: today.native });
9871001
spec.assertSubmitFails();
9881002

1003+
// Valid value
9891004
spec.setProperties({ value: new Date(2020, 0, 1).toISOString() });
9901005
spec.assertSubmitPasses();
9911006
});
@@ -1066,7 +1081,6 @@ describe('Date picker', () => {
10661081
const spec = createFormAssociatedTestBed<IgcDatePickerComponent>(html`
10671082
<igc-date-picker
10681083
name="datePicker"
1069-
required
10701084
.defaultValue=${null}
10711085
></igc-date-picker>
10721086
`);
@@ -1076,12 +1090,13 @@ describe('Date picker', () => {
10761090
});
10771091

10781092
it('fails required validation', () => {
1093+
spec.setProperties({ required: true });
10791094
spec.assertIsPristine();
10801095
spec.assertSubmitFails();
10811096
});
10821097

10831098
it('passes required validation when updating defaultValue', () => {
1084-
spec.setProperties({ defaultValue: today.native });
1099+
spec.setProperties({ required: true, defaultValue: today.native });
10851100
spec.assertIsPristine();
10861101

10871102
spec.assertSubmitPasses();
@@ -1107,7 +1122,7 @@ describe('Date picker', () => {
11071122
it('fails max validation', () => {
11081123
spec.setProperties({
11091124
max: today.native,
1110-
defaultValue: today.native,
1125+
defaultValue: today.add('day', 1).native,
11111126
});
11121127

11131128
spec.assertIsPristine();
@@ -1117,7 +1132,7 @@ describe('Date picker', () => {
11171132
it('passes max validation', () => {
11181133
spec.setProperties({
11191134
max: today.native,
1120-
defaultValue: today.add('day', -1).native,
1135+
defaultValue: today.native,
11211136
});
11221137

11231138
spec.assertIsPristine();
@@ -1135,7 +1150,10 @@ describe('Date picker', () => {
11351150
},
11361151
];
11371152

1138-
spec.setProperties({ disabledDates });
1153+
spec.setProperties({
1154+
disabledDates,
1155+
defaultValue: new Date(2024, 1, 28),
1156+
});
11391157

11401158
spec.assertIsPristine();
11411159
spec.assertSubmitFails();
@@ -1154,7 +1172,7 @@ describe('Date picker', () => {
11541172

11551173
spec.setProperties({
11561174
disabledDates,
1157-
defaultValue: new Date(2024, 1, 26),
1175+
defaultValue: new Date(2024, 1, 29),
11581176
});
11591177

11601178
spec.assertIsPristine();
@@ -1187,7 +1205,7 @@ describe('Date picker', () => {
11871205
});
11881206

11891207
describe('Validation message slots', () => {
1190-
it('', async () => {
1208+
it('', () => {
11911209
const now = CalendarDay.today;
11921210
const tomorrow = now.add('day', 1);
11931211
const yesterday = now.add('day', -1);

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { live } from 'lit/directives/live.js';
55

66
import { getThemeController, themes } from '../../theming/theming-decorator.js';
77
import IgcCalendarComponent, { focusActiveDate } from '../calendar/calendar.js';
8-
import { convertToDate, getDateFormValue } from '../calendar/helpers.js';
8+
import { convertToDate } from '../calendar/helpers.js';
99
import {
1010
type DateRangeDescriptor,
1111
DateRangeType,
@@ -32,6 +32,7 @@ import { FormAssociatedRequiredMixin } from '../common/mixins/forms/associated-r
3232
import {
3333
type FormValue,
3434
createFormValueState,
35+
defaultDateTimeTransformers,
3536
} from '../common/mixins/forms/form-value.js';
3637
import { createCounter, findElementFromEventPath } from '../common/util.js';
3738
import IgcDateTimeInputComponent from '../date-time-input/date-time-input.js';
@@ -434,11 +435,7 @@ export default class IgcDatePickerComponent extends FormAssociatedRequiredMixin(
434435

435436
this._formValue = createFormValueState<Date | null>(this, {
436437
initialValue: null,
437-
transformers: {
438-
setValue: convertToDate,
439-
setDefaultValue: convertToDate,
440-
setFormValue: getDateFormValue,
441-
},
438+
transformers: defaultDateTimeTransformers,
442439
});
443440

444441
this.addEventListener('focusin', this.handleFocusIn);

src/components/date-picker/validators.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export const datePickerValidators: Validator<IgcDatePickerComponent>[] = [
1515
maxDateValidator,
1616
{
1717
key: 'badInput',
18-
message: (host) => formatString(messages.disabledDate, host.value),
19-
isValid: (host) =>
20-
host.value ? !isDateInRanges(host.value, host.disabledDates ?? []) : true,
18+
message: ({ value }) => formatString(messages.disabledDate, value),
19+
isValid: ({ value, disabledDates }) =>
20+
value && disabledDates ? !isDateInRanges(value, disabledDates) : true,
2121
},
2222
];

0 commit comments

Comments
 (0)