Skip to content

Create a use_linked_inputs widget instead of Input #475

Closed

Description

@rmorshea

Old Behavior

The current Input widget is clunky to use and was created primarily for the purpose of making it easier to construct a set of linked inputs. The current Input util also doesn't work when the IDOM_FEATURE_INDEX_AS_DEFAULT_KEY=1 feature flag is set.

New Behavior

To resolve this we should create a use_linked_inputs widget utility that can achieve this use case with a better interface and also work with the IDOM_FEATURE_INDEX_AS_DEFAULT_KEY=1 feature flag set.

Implementation Details

_CastInput = TypeVar("_CastInput")


def use_linked_inputs(
    attributes: Iterable[Dict[str, Any]],
    on_change: Callable[[_CastInput], None] = lambda value: None,
    cast: Callable[[str], _CastInput] = lambda value: value,
    initial_value: str = "",
    ignore_empty: bool = True,
) -> List[VdomDict]:
    """Return a list of linked inputs equal to the number of given attributes"""
    value, set_value = idom.hooks.use_state(initial_value)

    def sync_inputs(event: Dict[str, Any]) -> None:
        new_value = event["value"]
        set_value(new_value)
        if not new_value and ignore_empty:
            return None
        on_change(cast(new_value))

    return [
        idom.html.input({**attrs, "onChange": sync_inputs, "value": value})
        for attrs in attributes
    ]

Activity

  1. added
    flag-triage
    and removed
    flag-triage
    on Aug 22, 2021
  2. added this to the 1.0 milestone on Jan 11, 2022
  3. added
    priority-2-moderate
    type-revision
    priority-1-high
    and removed
    priority-2-moderate
    on Jan 13, 2022
  4. rmorshea commented on Jan 14, 2022

    @rmorshea
    CollaboratorAuthor

    Since index is now the default this needs to be fixed before the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-1-hightype-revision

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions