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
use crate::prelude::use_prop_id;
use std::mem::swap;
use yew::prelude::*;

/// Properties for [`Radio`].
#[derive(PartialEq, Properties)]
pub struct RadioProperties {
    #[prop_or_default]
    pub id: Option<String>,

    /// The input control name, used to group radio buttons together.
    #[prop_or_default]
    pub name: Option<AttrValue>,

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

    /// The radio button label content.
    #[prop_or_default]
    pub children: Children,

    #[prop_or_default]
    pub checked: bool,

    #[prop_or_default]
    pub reversed: bool,

    #[prop_or_default]
    pub disabled: bool,

    /// A longer description text.
    #[prop_or_default]
    pub description: Option<Html>,

    /// Additional content aligned with the label.
    #[prop_or_default]
    pub body: Option<Html>,

    /// Event fired when the radio button is checked (but not when unchecked).
    #[prop_or_default]
    pub onchange: Callback<()>,
}

/// Radio button component
///
/// > A **radio** button is used to present the user with mutually exclusive choices. Always present radio buttons in groups of 2 or more.
///
/// See: <https://www.patternfly.org/components/forms/radio>
///
/// ## Properties
///
/// Defined by [`RadioProperties`].
#[function_component(Radio)]
pub fn radio(props: &RadioProperties) -> Html {
    let class = classes!("pf-v5-c-radio");

    let id = use_prop_id(props.id.clone());

    let mut input_class = classes!("pf-v5-c-radio__input");

    if props.children.is_empty() {
        input_class.extend(classes!("pf-m-standalone"));
    }

    let onchange = use_callback(props.onchange.clone(), |_, onchange| {
        onchange.emit(());
    });

    let mut first = html!(
        <input
            class={input_class}
            type="radio"
            id={(*id).clone()}
            name={&props.name}
            checked={props.checked}
            disabled={props.disabled}
            value={&props.value}
            {onchange}
        />
    );

    let mut label_class = classes!("pf-v5-c-radio__label");
    if props.disabled {
        label_class.extend(classes!("pf-m-disabled"));
    }

    let mut second = html!(
        <>
            if !props.children.is_empty() {
                <label
                    class={label_class}
                    for={(*id).clone()}
                >
                    { props.children.clone() }
                </label>
            }
        </>
    );

    if props.reversed {
        swap(&mut first, &mut second);
    }

    html!(
        <div {class}>
            {first} {second}

            if let Some(description) = &props.description {
                <span class="pf-v5-c-radio__description">
                    { description.clone() }
                </span>
            }

            if  let Some(body) = &props.body {
                <span class="pf-v5-c-radio__body">
                    { body.clone() }
                </span>
            }
        </div>
    )
}