Skip to content

Commit 8a27ecc

Browse files
committed
fix: prevent date segment from having invalid part value
1 parent 46fe4f1 commit 8a27ecc

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.changeset/major-onions-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@formwerk/core': patch
3+
---
4+
5+
fix: prevent date segments from having invalid min value

packages/core/src/useDateTimeField/useDateTimeSegment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ export function useDateTimeSegment(_props: Reactivify<DateTimeSegmentProps>) {
111111
return;
112112
}
113113

114+
const { min, max, maxLength } = getMetadata();
114115
const nextValue = currentInput + evt.data;
115116
currentInput = nextValue;
116117

117118
const parsed = parser.parse(nextValue);
118-
const { min, max, maxLength } = getMetadata();
119119
if (isNullOrUndefined(min) || isNullOrUndefined(max) || isNullOrUndefined(maxLength)) {
120120
return;
121121
}
@@ -131,6 +131,13 @@ export function useDateTimeSegment(_props: Reactivify<DateTimeSegmentProps>) {
131131
// If the current input length is greater than or equal to the max length, or the parsed value is greater than the max value,
132132
// then we should signal the segment group that this segment is done and it can move to the next segment
133133
if (currentInput.length >= maxLength || parsed * 10 > max) {
134+
// When done, if the parsed value is less than the min value, we should set the value to the min value
135+
if (parsed < min) {
136+
if (segmentEl.value) {
137+
segmentEl.value.textContent = min.toString();
138+
}
139+
}
140+
134141
onDone();
135142
}
136143
},

packages/playground/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ const max = new Date('2025-01-20');
77
</script>
88

99
<template>
10-
<DateField name="birthdate" label="Birth Date" :value="value" :min="min" :max="max" />
10+
<DateField name="birthdate" label="Birth Date" />
1111
</template>

0 commit comments

Comments
 (0)