1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Text Input Group

use crate::{prelude::use_on_text_change, utils::HtmlElementSupport};
use yew::prelude::*;

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TextInputGroupProperties {
    #[prop_or_default]
    pub id: Option<AttrValue>,

    #[prop_or_default]
    pub class: Classes,

    #[prop_or_default]
    pub style: Option<AttrValue>,

    #[prop_or_default]
    pub children: Html,

    #[prop_or_default]
    pub disabled: bool,
}

/// Text input group component
///
/// > A **text input** group is a more flexible composable version of a text input. It enables consumers of PatternFly to build custom inputs for filtering and similar use cases by placing elements like icons, chips groups and buttons within a text input.
///
/// See: <https://www.patternfly.org/components/text-input-group>
///
/// ## Properties
///
/// Defined by [`TextInputGroupProperties`].
///
/// ## Children
///
/// This component is mainly a container, it requires one [`TextInputGroupMain`] to work properly.
#[function_component(TextInputGroup)]
pub fn text_input_group(props: &TextInputGroupProperties) -> Html {
    let mut class = classes!("pf-v5-c-text-input-group");

    class.extend(props.class.clone());

    if props.disabled {
        class.extend(classes!("pf-m-disabled"));
    }

    html!(
        <div {class} id={&props.id} style={&props.style}>
            { props.children.clone() }
        </div>
    )
}

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TextInputGroupMainProperties {
    #[prop_or_default]
    pub id: Option<AttrValue>,

    #[prop_or_default]
    pub class: Classes,

    #[prop_or_default]
    pub style: Option<AttrValue>,

    /// The value of the input component
    #[prop_or_default]
    pub value: String,

    #[prop_or_default]
    pub placeholder: Option<AttrValue>,

    #[prop_or_default]
    pub icon: Option<Html>,

    /// Disables the component
    #[prop_or_default]
    pub disabled: bool,

    #[prop_or_default]
    pub aria_label: Option<AttrValue>,

    #[prop_or_default]
    pub onchange: Callback<String>,

    #[prop_or_default]
    pub oninput: Callback<InputEvent>,

    #[prop_or_default]
    pub r#type: Option<AttrValue>,

    #[prop_or_default]
    pub inputmode: Option<AttrValue>,

    #[prop_or_default]
    pub onkeydown: Callback<KeyboardEvent>,

    #[prop_or_default]
    pub autofocus: bool,

    #[prop_or_default]
    pub inner_ref: Option<NodeRef>,

    #[prop_or_default]
    pub hint: Option<AttrValue>,
}

#[function_component(TextInputGroupMain)]
pub fn text_input_group_main(props: &TextInputGroupMainProperties) -> Html {
    let mut class = classes!("pf-v5-c-text-input-group__main");
    class.extend(props.class.clone());

    if props.icon.is_some() {
        class.push(classes!("pf-m-icon"));
    }

    let node_ref = use_node_ref();
    let node_ref = props.inner_ref.as_ref().unwrap_or(&node_ref);
    let oninput = use_on_text_change(
        node_ref.clone(),
        props.oninput.clone(),
        props.onchange.clone(),
    );

    // autofocus

    {
        let node_ref = node_ref.clone();
        use_effect_with(props.autofocus, move |autofocus| {
            if *autofocus {
                node_ref.focus();
            }
        });
    }

    // render

    html!(
        <div
            {class}
            id={&props.id}
            style={&props.style}
        >
            <span class="pf-v5-c-text-input-group__text">
                if let Some(hint) = &props.hint {
                    <input
                        class="pf-v5-c-text-input-group__text-input pf-m-hint"
                        type="text"
                        disabled=true
                        aria-hidden="true"
                        value={hint}
                    />
                }
                if let Some(icon) = &props.icon {
                    <span class="pf-v5-c-text-input-group__icon">
                        { icon.clone() }
                    </span>
                }
                <input
                    class="pf-v5-c-text-input-group__text-input"
                    ref={node_ref}
                    type={&props.r#type}
                    inputmode={&props.inputmode}
                    {oninput}
                    disabled={props.disabled}
                    placeholder={&props.placeholder}
                    value={props.value.clone()}
                    aria-label={&props.aria_label}
                    onkeydown={&props.onkeydown}
                />
            </span>
        </div>
    )
}

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct TextInputGroupUtilitiesProperties {
    #[prop_or_default]
    pub children: Html,
}

#[function_component(TextInputGroupUtilities)]
pub fn text_input_group_utilities(props: &TextInputGroupUtilitiesProperties) -> Html {
    html! (
        <div class="pf-v5-c-text-input-group__utilities">
            { props.children.clone() }
        </div>
    )
}