Interdependent Inputs With Rollback

HardFormsStateValidationReact35 mins

by Pratik Rai

Read the full write-up

Four fields — a number, a select, a range and a number — that constrain each other: each must not exceed the one to its right.

An edit that would break the chain does not get clamped or flagged. It rolls back to the value of its parent, the field to its right.

Requirements

  1. Enforce floor ≤ target ≤ ceiling ≤ cap at all times.

  2. An edit that would break the chain rolls that field back to its parent’s value.

  3. Lowering a field also pulls the fields to its left down with it, so the chain never breaks from the other direction.

  4. Non-numeric or empty input is treated as corrupt and rolls back too.

  5. Show which field was rolled back — .field-invalid and .rolled are styled for it.

Notes

  • The four different input types are deliberate: a range fires continuously while dragged, which is where a naive implementation fights the user.

  • Rolling back is not the same as clamping. Both keep the chain valid; only one is what was asked for.

Hints

Loading editor…