feat(core, prompts): add DatePrompt for date input with customizable formats#448
feat(core, prompts): add DatePrompt for date input with customizable formats#448
Conversation
🦋 Changeset detectedLatest commit: fc7301f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
natemoo-re
left a comment
There was a problem hiding this comment.
Looking great so far! Left a few comments—happy to clarify if anything doesn't make sense.
|
I'm really liking this so far, but I have a few thoughts
|
|
it'd be good to render so it goes like this:
etc |
|
Thanks everyone for your feedback so far 😄 |
|
i've tested this locally and i don't get any errors, which is weird. I think the CI is failling due to the internal value i set with so:
also maybe snapshots were created on my local env (14), while CI (UTC) produced 15, so the snapshot comparison failed. i might need to think about fixing this in the upcoming days. |
| } | ||
|
|
||
| get cursor() { | ||
| // Convert segment cursor to absolute position for backward compatibility |
There was a problem hiding this comment.
Is backward compatibility needed?
| return start + this.#cursor.positionInSegment; | ||
| } | ||
|
|
||
| /** Get current segment cursor position */ |
There was a problem hiding this comment.
| /** Get current segment cursor position */ |
| #segmentValues: DateParts; | ||
| #minDate: Date | undefined; | ||
| #maxDate: Date | undefined; | ||
| /** Segment-based cursor: { segmentIndex, positionInSegment } */ |
There was a problem hiding this comment.
| /** Segment-based cursor: { segmentIndex, positionInSegment } */ |
| #minDate: Date | undefined; | ||
| #maxDate: Date | undefined; | ||
| /** Segment-based cursor: { segmentIndex, positionInSegment } */ | ||
| #cursor: { segmentIndex: number; positionInSegment: number } = { |
There was a problem hiding this comment.
Type can be trivially inferred here?
| // Initialize cursor to first segment, position 0 | ||
| this.#cursor = { segmentIndex: 0, positionInSegment: 0 }; |
There was a problem hiding this comment.
This is already the default value
| 0, | ||
| Math.min(seg.len - 1, this.#cursor.positionInSegment) | ||
| ); | ||
| return { segment: seg, index }; |
There was a problem hiding this comment.
Definite nit, but we can name the variable segment to avoid assignment here
| return { segment: seg, index }; | ||
| } | ||
|
|
||
| #onCursor(key?: string) { |
There was a problem hiding this comment.
This is a lot of direct manipulation in an event handler—would prefer if we could refactor to call out to methods that adjust state like #incrementCurrentSegment, #decrementCurrentSegment, #moveCursorNext, #moveCursorPrevious
| newNum = key === 'up' ? bounds.min : bounds.max; | ||
| } else { | ||
| const delta = key === 'up' ? 1 : -1; | ||
| newNum = Math.max(bounds.min, Math.min(bounds.max, num + delta)); |
There was a problem hiding this comment.
Also a nit, but we're doing Math.max(min, Math.min(max, num)) enough that we should have a generic util for clamp(min, num, max)
| #refreshFromSegmentValues() { | ||
| const display = this.#config.format(this.#segmentValues); | ||
| this._setUserInput(display); | ||
| this._setValue(segmentValuesToDate(this.#segmentValues) ?? undefined); | ||
| } |
There was a problem hiding this comment.
Would it be possible to have userInput and value be fully derived so that we don't need to imperatively call #refereshFromSegmentValues() manually?
| } | ||
|
|
||
| function daysInMonth(year: number, month: number): number { | ||
| return new Date(year || 2000, month || 1, 0).getDate(); |
There was a problem hiding this comment.
2000 was a leap year, so maybe not the best default?
Implements a date prompt that enforces a structured date format and supports the most common variants:
YYYY/MM/DD,MM/DD/YYYY, andDD/MM/YYYY.related: #447