patternfly_yew/hooks/
text_change.rs

1use crate::utils::value;
2use yew::prelude::*;
3
4/// A hook to handle the discrepancy between `oninput`/`onchange` for text input fields.
5///
6/// The hook will return a callback which is suitable for the `oninput` event of a text input. It
7/// will emit the `oninput` event unchanged, and emit an `onchange` event with the full value
8/// of the input control.
9///
10/// The `node` parameter must point to an HTML input element, suitable for the [`value`] function.
11/// Otherwise no `onchange` event fill be fired.
12#[hook]
13pub fn use_on_text_change(
14    node: NodeRef,
15    oninput: Callback<InputEvent>,
16    onchange: Callback<String>,
17) -> Callback<InputEvent> {
18    use_callback(
19        (node, oninput, onchange),
20        |evt, (node, oninput, onchange)| {
21            oninput.emit(evt);
22            if let Some(value) = value(node) {
23                onchange.emit(value);
24            }
25        },
26    )
27}