Repository navigation
[field] Fix data-dirty tracking for null-valued controls - #5290
Conversation
commit: |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Bundle size
PerformanceTotal duration: 1,394.62 ms +112.84 ms(+8.8%) | Renders: 78 (+0) | Paint: 2,158.96 ms +140.58 ms(+7.0%)
12 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
There was a problem hiding this comment.
Pull request overview
This PR fixes how data-dirty is computed for fields whose “empty” value is null (notably NumberField), ensuring the field is no longer considered dirty after the user returns to the original empty value.
Changes:
- Prevent
useFieldControlRegistrationfrom re-syncing the field’s “initial value” after the first registration, avoiding accidental capture of a later user-edited value as the initial value. - Add a regression test verifying
data-dirtyis cleared when aNumberFieldgoes from empty → non-empty → empty.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/react/src/internals/field-register-control/useFieldControlRegistration.ts | Sync initial field value only once to avoid mis-tracking dirty state for null-empty controls. |
| packages/react/src/field/root/FieldRoot.test.tsx | Adds coverage for data-dirty clearing when a null-empty NumberField returns to its empty initial value. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| @@ -82,6 +83,12 @@ export function useFieldControlRegistration(params: UseFieldControlRegistrationP | |||
| } | |||
|
|
|||
| function syncInitialValue() { | |||
There was a problem hiding this comment.
Root cause
syncInitialValue is intended to run only once when a field mounts. However, it currently runs on every value change. As a result, the field's initial value is overwritten with the latest value, which causes the data-dirty state to be calculated incorrectly.
Running syncInitialValue only once on mount fixes the issue.
Reproduction
- Open https://base-ui.com/react/handbook/forms.
- Enter
5in both the Server name and Number of instances fields. - Verify that
data-dirtyis applied to both fields. ✅ - Clear both fields.
- Notice that
data-dirtyis removed from Server name, but remains on Number of instances. ❌
Why this happens
For components such as NumberField and Select, the initial value is null rather than "".
Because syncInitialValue runs on every value change, validityData.initialValue is overwritten with the first value entered by the user instead of preserving the original null value. Consequently, when the field is cleared back to null, the current value no longer matches the stored initial value, so data-dirty incorrectly remains applied.
Making syncInitialValue run only once during mount preserves the correct initial value and fixes the issue.
PR reviewRoot cause analysis is correct and the fix works — I reproduced the bug on master with Select (stuck Bugs (2)1. 🟡
|
632ea1a to
90730e0
Compare
data-dirty tracking for null-valued controls
Fixed this bug but in different way than suggested. Suggested way reintroduced initial bug which this PR intended to fix
I think new behaviour is correct, dirty means the value differs from the initial value captured at mount, so a value that arrives later (async fetch / setValue) is a change → dirty. If a fetched value should be the baseline, set it as the field's initial value (render it from the start or reset the form).
Fixed
Fixed
Fixed |
PR review (follow-up)No merge-blocking issues found, but control swapping still has one narrow dirty-state bug. Existing FieldRoot suites pass in jsdom and Chromium; an added non-null baseline regression test fails on the current head. Bugs (1)1. 🟠 Recapture non-null baselines when swapping controlsLocation: if (validityData.initialValue === null && initialValue !== null) {
setValidityData((prev) => ({ ...prev, initialValue }));
}The new source guard permits synchronization for each swapped-in control, but this condition only replaces a Failure scenario: Swap a control with Fix: Update the baseline whenever the control source changes, while returning the previous state object when the value is unchanged to avoid an unnecessary render: diff --git a/packages/react/src/internals/field-register-control/useFieldControlRegistration.ts b/packages/react/src/internals/field-register-control/useFieldControlRegistration.ts
@@
const initialValue = getRegistrationValue(registration);
- if (validityData.initialValue === null && initialValue !== null) {
- setValidityData((prev) => ({ ...prev, initialValue }));
- }
+ setValidityData((prev) => {
+ if (Object.is(prev.initialValue, initialValue)) {
+ return prev;
+ }
+
+ return { ...prev, initialValue };
+ });The source guard prevents subsequent value changes from overwriting the baseline. Please also add a non-null-to-non-null control-swap regression test. Tests (0)No findings. Simplifications (0)No findings. Docs (0)No findings. VerdictApprove after nits - the null-initial-value fix works, but swapped controls can retain a stale non-null baseline. 🤖 Review generated with Codex |
90730e0 to
426a5a3
Compare
|
done |
|
New agent review, keep finding fresh issues - this one is narrow, though PR reviewThe fix direction is sound: capturing the baseline once per control source (including Bugs (1)1. 🟠 Remounting the same control resets the dirty baseline to the current valueLocation:
Failure scenario: Controlled Select inside a persistent Fix: If recapture-on-swap is the intended semantic this may be acceptable, but it should be pinned with a test. A heuristic that fixes the remount case while keeping the swap tests green — skip recapture when the field is already dirty: function syncInitialValue() {
const source = activeFieldControlSourceRef.current;
if (syncedInitialValueSourceRef.current === source) {
return;
}
syncedInitialValueSourceRef.current = source;
+ // A new control source normally recaptures the baseline (control swap), but if the
+ // user has already dirtied the field, treat the remounting control as the same
+ // logical field and keep the original baseline.
+ if (markedDirtyRef.current) {
+ return;
+ }
+
const registration = registrationRef.current;
if (!registration) {
return;
}The trade-off: a swap after interaction now keeps the old baseline instead of recapturing — ambiguous either way; this picks "preserve". Docs (1)2. ℹ️ New per-source gate deserves a one-line comment
VerdictApprove after nits — the confirmed remount baseline reset is narrow and possibly acceptable, but should be an explicit decision with a pinning test. 🤖 Review generated with Claude Code |
Fixed, hopefully everything is resolved now :) |
|
They kept finding new issues :-) A remount and a control swap look identical to the registration hook (both are unregister -> register with all-new identity), and each fix guessed with a different proxy, so each one fixed one case and broke another. The new guard (I pushed above) uses the only signal that actually separates them: a new instance registering with the previous instance's last known value is a remount (keep baseline), anything else is a swap (recapture). This is independent of dirty history, so Will re-review with both 5.6 + Fable now |
|
It turns out the first review's bug was something that should have been ignored. The original review assumed swapping controls inside the same The first reported bug was a non-issue because swapping a control does not create a new field: the baseline belongs to the Field root. The reported immediate |
RCA and reproduction steps: #5290 (comment)