kenan238 / reblessed

A high-level terminal interface library for node.js.
MIT License
87 stars 10 forks source link

Dynamically added form input elements are not included in focus cycle #24

Closed markabrahams closed 6 months ago

markabrahams commented 9 months ago

After dynamically adding an input element (e.g. textbox) to a form, the new element is not present anywhere in the focus cycle.

This occurs because the dynamically built _children array (which is used for focus cycling) on the form is not refreshed, even though the children array is updated correctly. There's even an existing comment saying that the bypass of rebuilding the _children array if one already exists on a refresh should possibly be removed.

Form.prototype._refresh = function () {
    // XXX Possibly remove this if statement and refresh on every focus.
    // Also potentially only include *visible* focusable elements.
    // This would remove the need to check for _selected.visible in previous()
    // and next().
    if (!this._children) {
        var out = [];
        this.children.forEach(function fn(el) {
            if (el.keyable)
                out.push(el);
            el.children.forEach(fn);
        });
        this._children = out;
    }
};

Turns out the comment was spot on!

The fix is to simply remove the "if" condition around this, which rebuilds a correct _children array of descendant inputs every time it is required. Will raise a PR for the fix shortly.