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
use plaster::prelude::*;

#[derive(Clone, PartialEq, Default)]
pub struct CheckboxOption {
    pub key: String,
    pub label: String,
    pub icon: Option<String>,
    pub description: Option<String>,
    pub disabled: bool,
}

/// A big enum checkbox field
pub struct BigCheckbox {
    label: String,
    value: Vec<String>,
    options: Vec<CheckboxOption>,
    radio: bool,
    on_change: Option<Callback<Vec<String>>>,
}

pub enum Msg {
    Click(String, bool),
}

#[derive(Default, Clone, PartialEq)]
pub struct Props {
    /// The input label
    pub label: String,
    /// The controlled value of the input
    pub value: Vec<String>,
    /// The options for the checkboxes
    pub options: Vec<CheckboxOption>,
    /// Whether this should be a radio button
    pub radio: bool,
    /// A callback that is fired when the user changes the input value
    pub on_change: Option<Callback<Vec<String>>>,
}

impl Component for BigCheckbox {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, _context: ComponentLink<Self>) -> Self {
        BigCheckbox {
            label: props.label,
            value: props.value,
            options: props.options,
            radio: props.radio,
            on_change: props.on_change,
        }
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.label = props.label;
        self.value = props.value;
        self.options = props.options;
        self.radio = props.radio;
        self.on_change = props.on_change;

        true
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Click(key, disabled) => {
                if !disabled {
                    if self.radio {
                        self.value = vec![key];
                    } else {
                        if self.value.contains(&key) {
                            self.value.retain(|x| x != &key);
                        } else {
                            self.value.push(key);
                        }
                    }

                    if let Some(ref callback) = self.on_change {
                        callback.emit(self.value.clone());
                    }
                }
            }
        };

        true
    }
}

impl Renderable<BigCheckbox> for BigCheckbox {
    fn view(&self) -> Html<Self> {
        let options = self.options.iter().map(|option| {
            let icon = if let Some(ref icon) = option.icon {
                html! {
                    <img class="big-enum-icon", src=icon, />
                }
            } else {
                html!(<span />)
            };

            let checked = if self.value.contains(&option.key) {
                html!(<img class="big-enum-select-indicator", src="/small-check-true.svg", />)
            } else {
                html!(<img class="big-enum-select-indicator", src="/small-check-false.svg", />)
            };
            let class = if option.disabled {
                "big-enum-select-item disabled"
            } else {
                "big-enum-select-item"
            };
            let key = option.key.clone();
            let disabled = option.disabled;

            html! {
                <div class=class, onclick=|_| Msg::Click(key.clone(), disabled),>
                    {checked}
                    {icon}
                    <div class="big-enum-title",>{&option.label}</div>
                    <div class="big-enum-content",>{option.description.as_ref().map(|x| x.as_str()).unwrap_or("")}</div>
                </div>
            }
        });

        html! {
            <div class="big-enum-select",>
                {for options}
            </div>
        }
    }
}