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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use yew::prelude::*;

use crate::cx;
use crate::sui;
use crate::helper::*;
use either::Either;

/// An icon is a glyph used to represent something else.
pub struct Icon {
    link: ComponentLink<Self>,
    props: IconProps,
    classes: Vec<String>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
/// Display an icon on top of another icon in corner.
/// Defaults to `BottomRight`.
pub enum IconCorner {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum IconRotate {
    Clockwise,
    Counterclockwise
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum IconSize {
    Mini,
    Tiny,
    Small,
    Large,
    Big,
    Huge,
    Massive,
}

#[derive(Debug, Clone, PartialEq, Properties)]
pub struct IconProps {
    /// An html element type to render as root element.
    #[prop_or_else(|| "i".to_string())]
    pub root: String,
    /// Formatted to appear bordered.
    #[prop_or(false)]
    pub bordered: bool,
    /// Icon can formatted to appear circular.
    #[prop_or(false)]
    pub circular: bool,
    /// Additional classes.
    #[prop_or_else(|| None)]
    pub class_name: Option<String>,
    /// Color of the icon.
    #[prop_or_else(|| None)]
    pub color: Option<sui::Colors>,
    /// Icons can display a smaller corner icon.
    #[prop_or_else(|| Either::Left(false))]
    pub corner: Either<bool, IconCorner>,
    /// Show that the icon is inactive.
    #[prop_or(false)]
    pub disabled: bool,
    /// Fitted, without space to left or right of Icon.
    #[prop_or(false)]
    pub fitted: bool,
    /// Icon can be flipped.
    #[prop_or_else(|| None)]
    pub flipped: Option<sui::Flip>,
    /// Formatted to have its colors inverted for contrast.
    #[prop_or(false)]
    pub inverted: bool,
    /// Icon can be formatted as a link.
    #[prop_or(false)]
    pub link: bool,
    /// Icon can be used as a simple loader.
    #[prop_or(false)]
    pub loading: bool,
    /// Name of the icon.
    pub name: String,
    /// Icon can rotated.
    #[prop_or_else(|| None)]
    pub rotated: Option<IconRotate>,
    /// Size of the icon.
    #[prop_or_else(|| None)]
    pub size: Option<IconSize>,
    /// Icon can have an aria label.
    #[prop_or_else(|| None)]
    pub aria_hidden: Option<String>,
    /// Icon can have an aria label.
    #[prop_or_else(|| None)]
    pub aria_label: Option<String>,
    /// Children element.
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub onclick: Callback<MouseEvent>,
}

impl Component for Icon {
    type Message = MouseEvent;
    type Properties = IconProps;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        let classes = props.derive_classes();
        Self {
            link,
            props,
            classes,
        }
    }

    fn update(&mut self, event: Self::Message) -> ShouldRender {
        if self.props.disabled {
            event.prevent_default();
        } else {
            self.props.onclick.emit(event);
        }
        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if props != self.props {
            self.props = props;
            self.classes = self.props.derive_classes();
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        html!{
            <@{ self.props.root.clone() }
              class=classes!(self.classes.as_slice())
              onclick=self.link.callback(|e| e)
              aria-hidden=self.props.get_aria_hidden()
              aria-label=self.props.aria_label.clone()
            >
              { self.props.children.clone() }
            </@>
        }
    }
}

impl IconProps {
    fn derive_classes(&self) -> Vec<String> {
        let Self {
            color,
            name,
            size,
            bordered,
            circular,
            disabled,
            fitted,
            inverted,
            link,
            loading,
            corner,
            flipped,
            rotated,
            class_name,
            ..
        } = self;

        cx!(
            use_option(color),
            use_str(name),
            use_option(size),
            use_key(*bordered, "bordered"),
            use_key(*circular, "circular"),
            use_key(*disabled, "disabled"),
            use_key(*fitted, "fitted"),
            use_key(*inverted, "inverted"),
            use_key(*link, "link"),
            use_key(*loading, "loading"),
            use_key_or_option_and_key(corner, "corner"),
            use_option_and_key(flipped, "flipped"),
            use_option_and_key(rotated, "rotated"),
            use_str("icon"),
            use_option(class_name)
        )
    }

    fn get_aria_hidden(&self) -> Option<String> {
        if self.aria_label.is_none() {
            return Some("true".to_string());
        } else {
            self.aria_hidden.clone()
        }
    }
}

impl From<IconCorner> for &'static str {
    fn from(i: IconCorner) -> Self {
        use IconCorner::*;

        match i {
            TopLeft => "top left",
            TopRight => "top right",
            BottomLeft => "bottom left",
            BottomRight => "bottom right",
        }
    }
}

impl AsRef<str> for IconCorner {
    fn as_ref(&self) -> &str {
        (*self).into()
    }
}

impl Default for IconCorner {
    fn default() -> Self {
        Self::BottomRight
    }
}

impl From<IconRotate> for &'static str {
    fn from(i: IconRotate) -> Self {
        use IconRotate::*;

        match i {
            Clockwise => "clockwise",
            Counterclockwise => "Counterclockwise",
        }
    }
}

impl AsRef<str> for IconRotate {
    fn as_ref(&self) -> &str {
        (*self).into()
    }
}

impl From<IconSize> for &'static str {
    fn from(i: IconSize) -> Self {
        use IconSize::*;

        match i {
            Mini => "mini",
            Tiny => "tiny",
            Small => "small",
            Large => "large",
            Big => "big",
            Huge => "huge",
            Massive => "massive",
        }
    }
}

impl AsRef<str> for IconSize {
    fn as_ref(&self) -> &str {
        (*self).into()
    }
}