perspective_viewer/components/form/
select_field.rs

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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use std::fmt::{Debug, Display};
use std::sync::Arc;

use itertools::Itertools;
use strum::IntoEnumIterator;
use yew::{function_component, html, Callback, Properties};

use crate::components::containers::select::{Select, SelectItem};
use crate::components::form::optional_field::OptionalField;

#[derive(Properties, Debug, PartialEq, Clone)]
pub struct SelectEnumFieldProps<T>
where
    T: IntoEnumIterator + Display + Default + PartialEq + Clone + 'static,
{
    pub label: String,
    pub current_value: Option<T>,
    pub on_change: Callback<Option<T>>,

    #[prop_or_default]
    pub default_value: Option<T>,

    #[prop_or_default]
    pub disabled: bool,
}

#[function_component(SelectEnumField)]
pub fn select_enum_field<T>(props: &SelectEnumFieldProps<T>) -> yew::Html
where
    T: IntoEnumIterator + Debug + Display + Default + PartialEq + Clone + 'static,
{
    let values = T::iter().map(SelectItem::Option).collect_vec();
    let selected = props.current_value.clone().unwrap_or_default();
    let checked = selected != props.default_value.clone().unwrap_or_default();
    let reset_value = props.default_value.clone();
    html! {
        <div class="row">
            <OptionalField
                label={props.label.clone()}
                on_check={props.on_change.reform(move |_| reset_value.clone())}
                {checked}
                disabled={props.disabled}
            >
                <Select<T> {values} {selected} on_select={props.on_change.reform(Option::Some)} />
            </OptionalField>
        </div>
    }
}

#[derive(Properties, Debug, PartialEq, Clone)]
pub struct SelectValueFieldProps<T>
where
    T: Display + PartialEq + Clone + 'static,
{
    pub label: String,
    pub current_value: Option<T>,
    pub default_value: T,
    pub values: Arc<Vec<T>>,
    pub on_change: Callback<Option<T>>,

    #[prop_or_default]
    pub disabled: bool,
}

#[function_component(SelectValueField)]
pub fn select_value_field<T>(props: &SelectValueFieldProps<T>) -> yew::Html
where
    T: Display + PartialEq + Clone + 'static,
{
    let values = props
        .values
        .iter()
        .cloned()
        .map(SelectItem::Option)
        .collect_vec();

    let selected = props
        .current_value
        .clone()
        .unwrap_or_else(|| props.default_value.clone());

    let checked = selected != props.default_value;
    html! {
        <div class="row">
            <OptionalField
                label={props.label.clone()}
                on_check={props.on_change.reform(|_| None)}
                {checked}
                disabled={props.disabled}
            >
                <Select<T> {values} {selected} on_select={props.on_change.reform(Option::Some)} />
            </OptionalField>
        </div>
    }
}