Skip to content

(fix) O3-6387: Report a cleared date segment in OpenmrsDatePicker - #2037

Merged
denniskigen merged 4 commits into
openmrs:mainfrom
solomonfortune:fix/O3-6387-datepicker-partial-clear
Dec 2, 2026
Merged

denniskigen merged 4 commits into
openmrs:mainfrom
solomonfortune:fix/O3-6387-datepicker-partial-clear

Conversation

@solomonfortune

@solomonfortune solomonfortune commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work is based on designs, which are linked or shown either in the Jira ticket or the description below. (See also: Styleguide)
  • My work includes tests or is validated by existing tests.
  • I have updated the esm-framework and storybook mocks (mock.tsx, mock-jest.tsx, and storybook mocks) to reflect any API changes.

Summary

When the user clears one segment of a complete date in OpenmrsDatePicker, the picker doesn't call onChange. 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.

DatePickerInput now sets the picker's value to null when a segment of a complete date is cleared, so parents get onChange(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 undefined while 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.tsx covering: clearing a segment reports undefined while 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 stub Selection.collapse because happy-dom dispatches selectionchange synchronously, which makes React Aria's segment focus handling recurse until the stack overflows (real browsers fire it asynchronously).

@changeset-bot

changeset-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest 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

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5 · The PR appears safe to merge, with a non-blocking request for controlled-value test coverage.RetriggerView in GreptileConfidence Score: 4/5

[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.

FindingsFix All in Claude CodeFindings

  1. P2 Controlled edits lack a test ▶
Fix with agent prompt
### Issue 1
packages/framework/esm-styleguide/src/datepicker/datepicker.test.tsx:363
Both new tests use `defaultValue`, so neither checks the parent updating `value` after `onChange(undefined)`. The fix keeps a separate field value during partial edits, making this round trip important. Add a controlled test whose parent stores each change, then clear and restore a segment. Check that the other segments survive and the restored date reaches the parent.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Add a controlled-value regression test for clearing and restoring a date segment.

  • The single-date picker reports when a date segment is cleared and completed again.

Reviews (1) · Last reviewed commit: "(fix) O3-6387: Report a cleared date seg..." · Reviewed by Greptile

@denniskigen denniskigen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 solomonfortune left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 denniskigen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
});
});
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@denniskigen denniskigen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@denniskigen
denniskigen merged commit a391d05 into openmrs:main Dec 2, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants