Repository navigation
(fix) O3-6387: Report a cleared date segment in OpenmrsDatePicker - #2037
denniskigen merged 4 commits into
Conversation
🦋 Changeset detectedLatest commit: dfa5e6c The changes in this PR will be included in the next version bump. 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 |
[High impact] The change reaches single-date fields across multiple application workflows. The PR appears safe to merge, with a non-blocking request for controlled-value test coverage.
|
denniskigen
left a comment
There was a problem hiding this comment.
Thanks, @solomonfortune. This is very close, and comparing the shown segments with the held date is a nice way to stop a draft like 31/02 being reported early. I've left a couple of suggestions. Can we also add a changeset ('@openmrs/esm-styleguide': patch)?
For context, React Aria stopped reporting partly cleared dates in adobe/react-spectrum#9510, which reached us in #1975, and they've said they won't report null for them (adobe/react-spectrum#9624). So this is a workaround on our side. Can we link those in the comment above DatePickerInput so it's easy to drop if React Aria adds a supported way?
solomonfortune
left a comment
There was a problem hiding this comment.
Thanks, @denniskigen. Done: the hidden input now follows the picker's value (with your form test), the blur validation handler is in, the extra onChange props are gone, the React Aria issues are linked above DatePickerInput, a 31/02 test is added, and there's a changeset.
denniskigen
left a comment
There was a problem hiding this comment.
This is very close, @solomonfortune. A few small things left.
| function DatePickerInput(props, ref) { | ||
| const datePickerState = useContext(DatePickerStateContext)!; | ||
| const [dateFieldProps, fieldRef] = useContextProps({ slot: props.slot }, ref, DateFieldContext); | ||
| const [dateFieldProps, fieldRef] = useContextProps(props, ref, DateFieldContext); |
There was a problem hiding this comment.
| const [dateFieldProps, fieldRef] = useContextProps(props, ref, DateFieldContext); | |
| const [dateFieldProps, fieldRef] = useContextProps({ slot: props.slot }, ref, DateFieldContext); |
This one still passes all of props.
| * date is complete again it is reported, even if it is the same date as before. | ||
| * | ||
| * This is a workaround on our side. React Aria stopped reporting partly cleared dates in | ||
| * https://github.com/adobe/react-spectrum/issues/9510, which reached us in |
There was a problem hiding this comment.
| * https://github.com/adobe/react-spectrum/issues/9510, which reached us in | |
| * https://github.com/adobe/react-spectrum/pull/9510, which reached us in |
nit: #9510 is a PR.
| await user.keyboard('02'); | ||
|
|
||
| expect(onChange.mock.calls.every(([value]) => value === undefined)).toBe(true); | ||
| }); |
There was a problem hiding this comment.
| }); | |
| }); | |
| it('should show the required error on blur while part of the date is cleared, and clear it once the date is typed back', async () => { | |
| const user = userEvent.setup(); | |
| render( | |
| <> | |
| <OpenmrsDatePicker aria-label="datepicker" isRequired defaultValue={november1st} /> | |
| <button>Other</button> | |
| </>, | |
| ); | |
| const day = screen.getByRole('spinbutton', { name: /day/i }); | |
| await user.click(day); | |
| await user.keyboard('{Backspace}{Backspace}'); | |
| await user.click(screen.getByRole('button', { name: 'Other' })); | |
| expect(day).toHaveAttribute('aria-invalid', 'true'); | |
| await user.click(day); | |
| await user.keyboard('01'); | |
| await user.click(screen.getByRole('button', { name: 'Other' })); | |
| expect(day).not.toHaveAttribute('aria-invalid'); | |
| }); |
Mind adding a test for the blur validation? This one fails without the onBlur handler, and also if it commits only while isPartial.
There was a problem hiding this comment.
Thanks, @denniskigen. Fixed the useContextProps call to pass only the slot, corrected the link to the React Aria PR, and added the blur validation test.
|
denniskigen
left a comment
There was a problem hiding this comment.
LGTM. Thanks for working through each round of feedback, @solomonfortune. The tests for the form input, blur validation and 31/02 cases will make it easy to drop this workaround once React Aria supports it.



Requirements
Summary
When the user clears one segment of a complete date in
OpenmrsDatePicker, the picker doesn't callonChange. React Aria only changes its value once every segment is filled in or every segment is empty, so a partly cleared date stays as draft text in the field and the parent keeps the old date while the field looks cleared. Completing the date back to the date it already holds isn't reported either.DatePickerInputnow sets the picker's value tonullwhen a segment of a complete date is cleared, so parents getonChange(undefined)/onChangeRaw(null), as they do when the whole field is cleared. The field keeps the last complete date as its own value meanwhile, so the segments that are still filled in aren't wiped. Once the date is complete again, the picker reports it, even if it's the same date as before. A complete but invalid draft (for example 31/02) isn't reported early. The hidden form input follows the picker's value so forms don't submit the old date, and blur commits validation against the picker's value so required errors show and clear as expected.Parents now see
undefinedwhile a segment is empty. Forms that validate on every change will show their "required" error as soon as a segment is deleted, as they already do on a full clear.Screenshots
No visual change.
Related Issue
https://openmrs.atlassian.net/browse/O3-6387
Other
Added six tests to
datepicker.test.tsxcovering: clearing a segment reportsundefinedwhile keeping the remaining segments, clearing and retyping a segment reports the date again (both uncontrolled and controlled), form submission excludes the old date while partially cleared, a complete but invalid draft (31/02) isn't reported early, and a required field shows and clears its error on blur. The tests stubSelection.collapsebecause happy-dom dispatchesselectionchangesynchronously, which makes React Aria's segment focus handling recurse until the stack overflows (real browsers fire it asynchronously).