use std::rc::Rc;
use yew::prelude::*;
use super::{PartChoice, ShapeEdit, part_choices};
use crate::config::*;
use crate::ui::SelectValueField;
#[derive(Properties, PartialEq)]
pub struct DatetimeStyleSimpleProps {
pub enable_time_config: bool,
pub shape: DatetimeFormat,
pub default_shape: DatetimeFormat,
pub on_change: Callback<ShapeEdit>,
}
#[function_component(DatetimeStyleSimple)]
pub fn datetime_style_simple(props: &DatetimeStyleSimpleProps) -> Html {
type PresetField = SelectValueField<PartChoice<SimpleDatetimeFormat>>;
let date_values = part_choices::<SimpleDatetimeFormat>(
props.enable_time_config && props.shape.time_style.is_some(),
);
let time_values = part_choices::<SimpleDatetimeFormat>(props.shape.date_style.is_some());
let on_date = props.on_change.reform(|choice| -> ShapeEdit {
Rc::new(
move |shape: &DatetimeFormat, reset: &DatetimeFormat| DatetimeFormat {
date_style: PartChoice::resolve(choice, reset.date_style),
..shape.clone()
},
)
});
let on_time = props.on_change.reform(|choice| -> ShapeEdit {
Rc::new(
move |shape: &DatetimeFormat, reset: &DatetimeFormat| DatetimeFormat {
time_style: PartChoice::resolve(choice, reset.time_style),
..shape.clone()
},
)
});
html! {
<>
<PresetField
label="date-style"
values={date_values}
default_value={PartChoice::from(props.default_shape.date_style)}
current_value={PartChoice::from(props.shape.date_style)}
on_change={on_date}
/>
if props.enable_time_config {
<PresetField
label="time-style"
values={time_values}
default_value={PartChoice::from(props.default_shape.time_style)}
current_value={PartChoice::from(props.shape.time_style)}
on_change={on_time}
/>
}
</>
}
}