3 ms·
You can dispense with the whole intermediate state variable and just do everything directly function calcState() { // Return field disabled for one-way t
by edflsafoiewq 5y ago
You can dispense with the whole intermediate state variable and just do everything directly
function calcState() {
// Return field disabled for one-way trips
elems.return.disabled = isOneWay();
let allOk = true;
// All enabled fields are valid?
for (const f of [elems.departure, elems.return]) {
const isInvalid = (
!f.disabled &&
isBadFormat(f.value)
);
setInvalid(f, isInvalid);
if (isInvalid) allOk = false;
}
// Return trip after departure trip?
if (allOk && !isOneWay() && isEarlyReturn()) {
allOk = false;
}
// Enable submit if everything is OK
elems.submit.disabled = !allOk;
}
function setInvalid(elem, invalid) {
elem.setAttribute(ARIA_INVALID, invalid);
}
This follows the problem description quite closely, eg. "The return textfield is enabled if the combobox’s value is return flight" is implemented by one line, and not spread across seven different enable/disable calls.