-
Notifications
You must be signed in to change notification settings - Fork 169
Date picker input field bug/kt #3522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
16f56fd
feat(date-picker): add MM/DD/YYYY parse/format utilities that reject …
KATIETOLER 8699dac
Merge main fix conflicts
KATIETOLER 0442ce9
refactor(date-picker): parse and format input with date-fns and accep…
KATIETOLER af8f806
fix(date-picker): validate typed input on blur instead of on every ke…
KATIETOLER e627c14
Add onInput back, add clear, and add onDateChange handler.
KATIETOLER 15074eb
Fix confusing wording on test.
KATIETOLER 95fac32
fix(date-picker): sync internal selected when a date is typed or picked.
KATIETOLER fc19680
Add helper function so that states dont diverge.
KATIETOLER File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
|
|
||
| import { | ||
| evaluateDatePickerInput, | ||
| formatDatePickerInput, | ||
| parseDatePickerInput, | ||
| } from './date-picker-input'; | ||
|
|
||
| describe('formatDatePickerInput', () => { | ||
| test('formats a date as zero-padded MM/DD/YYYY', () => { | ||
| expect(formatDatePickerInput(new Date(2026, 5, 5))).toBe('06/05/2026'); | ||
| }); | ||
|
|
||
| test('keeps two-digit months and days without padding changes', () => { | ||
| expect(formatDatePickerInput(new Date(2026, 10, 25))).toBe('11/25/2026'); | ||
| }); | ||
|
|
||
| test('round-trips with parseDatePickerInput', () => { | ||
| const date = new Date(2027, 0, 1); | ||
| const parsed = parseDatePickerInput(formatDatePickerInput(date)); | ||
| expect(parsed?.getFullYear()).toBe(2027); | ||
| expect(parsed?.getMonth()).toBe(0); | ||
| expect(parsed?.getDate()).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('parseDatePickerInput', () => { | ||
| test('parses a complete, valid MM/DD/YYYY date', () => { | ||
| const date = parseDatePickerInput('06/05/2026'); | ||
| expect(date?.getFullYear()).toBe(2026); | ||
| expect(date?.getMonth()).toBe(5); | ||
| expect(date?.getDate()).toBe(5); | ||
| }); | ||
|
|
||
| test('parses single-digit month and day without padding', () => { | ||
| const date = parseDatePickerInput('6/5/2026'); | ||
| expect(date?.getFullYear()).toBe(2026); | ||
| expect(date?.getMonth()).toBe(5); | ||
| expect(date?.getDate()).toBe(5); | ||
| }); | ||
|
|
||
| test('returns null for incomplete input (mid-typing)', () => { | ||
| expect(parseDatePickerInput('06/05')).toBeNull(); | ||
| expect(parseDatePickerInput('06')).toBeNull(); | ||
| }); | ||
|
|
||
| test('leniently parses a short year as entered', () => { | ||
| expect(parseDatePickerInput('06/05/26')?.getFullYear()).toBe(26); | ||
| }); | ||
|
|
||
| test('returns null for an out-of-range month', () => { | ||
| expect(parseDatePickerInput('13/01/2026')).toBeNull(); | ||
| expect(parseDatePickerInput('00/01/2026')).toBeNull(); | ||
| }); | ||
|
|
||
| test('returns null for an invalid day that does not round-trip', () => { | ||
| expect(parseDatePickerInput('02/30/2026')).toBeNull(); | ||
| expect(parseDatePickerInput('04/31/2026')).toBeNull(); | ||
| }); | ||
|
|
||
| test('returns null for non-date garbage', () => { | ||
| expect(parseDatePickerInput('not a date')).toBeNull(); | ||
| expect(parseDatePickerInput('Fri Jun 05 2026')).toBeNull(); | ||
| expect(parseDatePickerInput('')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('evaluateDatePickerInput', () => { | ||
| test('returns the date with no error for valid full-date entry', () => { | ||
| const { date, error } = evaluateDatePickerInput('06/05/2026'); | ||
| expect(error).toBe(false); | ||
| expect(date?.getFullYear()).toBe(2026); | ||
| }); | ||
|
|
||
| test('returns no date and no error for empty input', () => { | ||
| expect(evaluateDatePickerInput('')).toEqual({ date: null, error: false }); | ||
| expect(evaluateDatePickerInput(' ')).toEqual({ | ||
| date: null, | ||
| error: false, | ||
| }); | ||
| }); | ||
|
|
||
| test('flags incomplete entry as an error without a date', () => { | ||
| expect(evaluateDatePickerInput('06/05')).toEqual({ | ||
| date: null, | ||
| error: true, | ||
| }); | ||
| }); | ||
|
|
||
| test('flags an invalid date like 02/30/2026 as an error', () => { | ||
| expect(evaluateDatePickerInput('02/30/2026')).toEqual({ | ||
| date: null, | ||
| error: true, | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects an out-of-range date per isAllowed', () => { | ||
| const noPastDates = (d: Date) => d >= new Date(2026, 0, 1); | ||
| const { date, error } = evaluateDatePickerInput('06/05/2020', noPastDates); | ||
| expect(date).toBeNull(); | ||
| expect(error).toBe(true); | ||
| }); | ||
|
|
||
| test('accepts an in-range date per isAllowed', () => { | ||
| const noPastDates = (d: Date) => d >= new Date(2026, 0, 1); | ||
| const { date, error } = evaluateDatePickerInput('06/05/2027', noPastDates); | ||
| expect(date?.getFullYear()).toBe(2027); | ||
| expect(error).toBe(false); | ||
| }); | ||
|
|
||
| test('supports inline editing of the year', () => { | ||
| const prefilled = formatDatePickerInput(new Date(2026, 5, 5)); | ||
| expect(prefilled).toBe('06/05/2026'); | ||
|
|
||
| const edited = prefilled.replace('2026', '2027'); | ||
| const { date, error } = evaluateDatePickerInput(edited); | ||
| expect(error).toBe(false); | ||
| expect(date?.getFullYear()).toBe(2027); | ||
| expect(date?.getMonth()).toBe(5); | ||
| expect(date?.getDate()).toBe(5); | ||
| }); | ||
| }); |
|
KATIETOLER marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { format, isValid, parse } from 'date-fns'; | ||
|
|
||
| export const DATE_PICKER_INPUT_FORMAT = 'MM/DD/YYYY'; | ||
|
|
||
| const DATE_FNS_FORMAT = 'MM/dd/yyyy'; | ||
| const REFERENCE_DATE = new Date(0); | ||
|
|
||
| export const formatDatePickerInput = (date: Date): string => | ||
| format(date, DATE_FNS_FORMAT); | ||
|
|
||
| export const parseDatePickerInput = (value: string): Date | null => { | ||
| const date = parse(value.trim(), DATE_FNS_FORMAT, REFERENCE_DATE); | ||
| return isValid(date) ? date : null; | ||
| }; | ||
|
|
||
| export type DatePickerInputResult = { | ||
| date: Date | null; | ||
| error: boolean; | ||
| }; | ||
|
|
||
| export const evaluateDatePickerInput = ( | ||
| value: string, | ||
| isAllowed: (date: Date) => boolean = () => true, | ||
| ): DatePickerInputResult => { | ||
| if (!value.trim()) return { date: null, error: false }; | ||
|
|
||
| const date = parseDatePickerInput(value); | ||
| if (!date || !isAllowed(date)) return { date: null, error: true }; | ||
|
|
||
| return { date, error: false }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.