Closed
Description
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
]