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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use either::Either;
use yew::prelude::*;

use crate::{cx, sui};
use crate::helper::*;
use crate::collections::Icon;
use crate::collections::label::LabelDetail;

/// A label displays content classification.
pub struct Label {
    link: ComponentLink<Self>,
    props: LabelProps,
    classes: Vec<String>,
}

pub enum LabelEvent {
    Click(MouseEvent),
    Remove(MouseEvent)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LabelAttachedPosition {
    Top,
    Bottom,
    TopRight,
    TopLeft,
    BottomLeft,
    BottomRight,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LabelCorner {
    Left,
    Right,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LabelPointing {
    Above,
    Below,
    Left,
    Right,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum LabelRibbon {
    Right
}

#[derive(Debug, Clone, PartialEq, Properties)]
pub struct LabelProps {
    /// An html element type to render as root element.
    #[prop_or_else(|| "a".to_string())]
    pub root: String,
    /// A label can be active.
    #[prop_or(false)]
    pub active: bool,
    /// A label can attach to a content segment.
    #[prop_or_else(|| None)]
    pub attached: Option<LabelAttachedPosition>,
    /// A label can reduce its complexity.
    #[prop_or(false)]
    pub basic: bool,
    /// Primary content.
    #[prop_or_default]
    pub children: Children,
    /// A label can be circular.
    #[prop_or(false)]
    pub circular: bool,
    /// Additional classes.
    #[prop_or_else(|| None)]
    pub class_name: Option<String>,
    /// Color of the label.
    #[prop_or_else(|| None)]
    pub color: Option<sui::Colors>,
    /// Shorthand for primary content.
    #[prop_or_else(|| None)]
    pub content: Option<String>,
    /// A label can position itself in the corner of an element.
    #[prop_or_else(|| Either::Left(false))]
    pub corner: Either<bool, LabelCorner>,
    /// Shorthand for LabelDetail.
    #[prop_or_default]
    pub detail: ChildrenWithProps<LabelDetail>,
    /// Float above another element in the upper right corner.
    #[prop_or(false)]
    pub floating: bool,
    /// A horizontal label is formatted to label content along-side it horizontally.
    #[prop_or(false)]
    pub horizontal: bool,
    /// Shorthand for Icon.
    #[prop_or_default]
    pub icon: ChildrenWithProps<Icon>,

    // TODO: /// A label can be formatted to emphasize an image or prop can be used as shorthand for Image.
    // image: oneOfType([bool, customitemShorthand]),

    /// Called on click.
    #[prop_or_default]
    pub on_click: Callback<MouseEvent>,
    /// Adds an "x" icon, called when "x" is clicked.
    #[prop_or_else(|| None)]
    pub on_remove: Option<Callback<MouseEvent>>,
    /// Icon to appear as the last child and trigger on_remove.
    #[prop_or_default]
    pub remove_icon: ChildrenWithProps<Icon>,
    /// A label can point to content next to it.
    #[prop_or_else(|| Either::Left(false))]
    pub pointing: Either<bool, LabelPointing>,
    /// A label can prompt for an error in your forms.
    #[prop_or(false)]
    pub prompt: bool,
    /// A label can appear as a ribbon attaching itself to an element.
    #[prop_or_else(|| Either::Left(false))]
    pub ribbon: Either<bool, LabelRibbon>,
    /// A label can have different sizes.
    #[prop_or_else(|| None)]
    pub size: Option<sui::Sizes>,
    /// A label can appear as a tag.
    #[prop_or(false)]
    pub tag: bool,
}

impl Component for Label {
    type Message = LabelEvent;
    type Properties = LabelProps;

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

    fn update(&mut self, event: Self::Message) -> bool {
        use LabelEvent::*;

        match event {
            Click(e) => self.props.on_click.emit(e.clone()),
            Remove(e) => {
                self.props.on_click.emit(e.clone());
                if let Some(cb) = self.props.on_remove.as_ref() {
                    cb.emit(e);
                }
            }
        }

        false
    }

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

    fn view(&self) -> Html {
        if !self.props.children.is_empty() {
            return html!{
                <@{ self.props.root.clone() }
                  class=classes!(self.classes.as_slice())
                  onclick=self.link.callback(|e| LabelEvent::Click(e))
                >
                  { self.props.children.clone() }
                </@>
            }
        }

        let remove_icon = if self.props.on_remove.is_some() {
            let cb = self.link.callback(|e| LabelEvent::Remove(e));
            if self.props.remove_icon.is_empty() {
                html! { <Icon name="delete" onclick={cb}/> }
            } else {
                let mut icon = self.props.remove_icon.iter().next().unwrap();
                icon.props.onclick = cb;
                html! { icon }
            }
        } else {
            html! {}
        };

        return html!(
            <@{ self.props.root.clone() }
              class=classes!(self.classes.as_slice())
              onclick=self.link.callback(|e| LabelEvent::Click(e))
            >
              {self.props.icon.clone()}
              // TODO: {typeof image !== 'boolean' && Image.create(image, { autoGenerateKey: false })}
              { match self.props.content {
                    Some(ref content) => html! { content.clone() },
                    None => html! {}
                }
              }
              {self.props.detail.clone()}
              {remove_icon}
            </@>
        )
    }
}

impl LabelProps {
    fn derive_classes(&self) -> Vec<String> {
        let Self {
            active,
            attached,
            basic,
            circular,
            class_name,
            color,
            corner,
            floating,
            horizontal,
            // TODO: image,
            pointing,
            prompt,
            ribbon,
            size,
            tag,
            ..
        } = self;

        let mut pointing_class = match pointing {
            Either::Left(b) => if *b {
                vec!["pointing".to_string()]
            } else {
                vec![]
            }
            Either::Right(p) => match *p {
                LabelPointing::Left | LabelPointing::Right => vec![p.as_ref().to_owned(), "pointing".to_string()],
                LabelPointing::Above | LabelPointing::Below => vec!["pointing".to_string(), p.as_ref().to_owned()],
            }
        };

        cx!(
            use_str("ui"),
            use_option(color),
            pointing_class,
            use_option(size),
            use_key(*active, "active"),
            use_key(*basic, "basic"),
            use_key(*circular, "circular"),
            use_key(*floating, "floating"),
            use_key(*horizontal, "horizontal"),
            // TODO: use_key(image == true, "image",
            use_key(*prompt, "prompt"),
            use_key(*tag, "tag"),
            use_key_or_option_and_key(corner, "corner"),
            use_key_or_option_and_key(ribbon, "ribbon"),
            use_option_and_key(attached, "attached"),
            use_str("label"),
            use_option(class_name)
        )
    }
}

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

        match l {
            Top => "top",
            Bottom => "bottom",
            TopRight => "top right",
            TopLeft => "top left",
            BottomLeft => "bottom left",
            BottomRight => "bottom right",
        }
    }
}

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

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

        match l {
            Left => "left",
            Right => "right",
        }
    }
}

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

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

        match l {
            Left => "left",
            Right => "right",
            Above => "above",
            Below => "below",
        }
    }
}

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

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

        match l {
            Right => "right",
        }
    }
}

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