use dioxus::prelude::*;
use super::model::DataTableViewState;
use crate::hooks::use_unique_id;
use crate::{Button, ButtonVariant, Field, Input, Label};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DataTableSavedView {
pub id: String,
pub label: String,
pub state: DataTableViewState,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DataTableSavedViewsLabels {
pub region: String,
pub view: String,
pub custom: String,
pub name: String,
pub save: String,
pub update: String,
pub remove: String,
pub reset: String,
}
impl Default for DataTableSavedViewsLabels {
fn default() -> Self {
Self {
region: "Saved record views".into(),
view: "Saved view".into(),
custom: "Custom view".into(),
name: "New view name".into(),
save: "Save new view".into(),
update: "Update view".into(),
remove: "Remove view".into(),
reset: "Reset view".into(),
}
}
}
pub const DATA_TABLE_SAVED_VIEW_NAME_LIMIT: usize = 120;
pub fn data_table_saved_state(state: &DataTableViewState) -> DataTableViewState {
state.clone().first_page()
}
#[derive(Props, Clone, PartialEq)]
pub struct DataTableSavedViewsProps {
pub views: Vec<DataTableSavedView>,
pub state: DataTableViewState,
#[props(into)]
pub active_id: Option<String>,
pub reset_state: DataTableViewState,
pub on_apply: EventHandler<(DataTableViewState, Option<String>)>,
pub on_save: EventHandler<(String, DataTableViewState)>,
pub on_update: Option<EventHandler<(String, DataTableViewState)>>,
pub on_remove: Option<EventHandler<String>>,
#[props(default)]
pub disabled: bool,
#[props(default)]
pub labels: DataTableSavedViewsLabels,
}
#[component]
#[allow(clippy::too_many_lines)]
pub fn DataTableSavedViews(props: DataTableSavedViewsProps) -> Element {
let id = use_unique_id();
let mut name = use_signal(String::new);
let mut seen = std::collections::HashSet::new();
let views: Vec<DataTableSavedView> = props
.views
.iter()
.filter(|view| !view.id.trim().is_empty() && seen.insert(view.id.clone()))
.cloned()
.collect();
let active = props
.active_id
.as_ref()
.and_then(|active| views.iter().find(|view| view.id == *active))
.cloned();
let labels = props.labels.clone();
let disabled = props.disabled;
let on_apply = props.on_apply;
let on_save = props.on_save;
let state = props.state.clone();
let update_state = state.clone();
let reset_state = props.reset_state.clone();
let apply_views = views.clone();
let (update_active, remove_active) = (active.clone(), active.clone());
rsx! {
section {
aria_busy: if disabled { "true" } else { "false" },
aria_label: "{labels.region}",
class: "ui-data-table-view__saved",
Field { control_id: "{id}-view",
Label { r#for: "{id}-view", "{labels.view}" }
select {
class: "ui-select",
disabled,
id: "{id}-view",
oninput: move |event| {
if let Some(view) = apply_views.iter().find(|view| view.id == event.value()) {
on_apply.call((data_table_saved_state(&view.state), Some(view.id.clone())));
}
},
option { disabled: true, selected: active.is_none(), value: "", "{labels.custom}" }
for view in views.iter() {
option {
key: "{view.id}",
selected: active.as_ref().is_some_and(|active| active.id == view.id),
value: "{view.id}",
"{view.label}"
}
}
}
}
Field { control_id: "{id}-name",
Label { r#for: "{id}-name", "{labels.name}" }
Input {
disabled,
id: "{id}-name",
maxlength: "{DATA_TABLE_SAVED_VIEW_NAME_LIMIT}",
value: "{name}",
oninput: move |event: FormEvent| name.set(event.value().chars().take(DATA_TABLE_SAVED_VIEW_NAME_LIMIT).collect()),
}
}
Button {
disabled: disabled || name.read().trim().is_empty(),
onclick: move |_| {
let trimmed = name.peek().trim().to_string();
if !disabled && !trimmed.is_empty() {
on_save.call((trimmed, data_table_saved_state(&state)));
}
},
"{labels.save}"
}
if let Some(on_update) = props.on_update {
Button {
disabled: disabled || active.is_none(),
variant: ButtonVariant::Outline,
onclick: move |_| {
if let (false, Some(active)) = (disabled, update_active.as_ref()) {
on_update.call((active.id.clone(), data_table_saved_state(&update_state)));
}
},
"{labels.update}"
}
}
if let Some(on_remove) = props.on_remove {
Button {
disabled: disabled || active.is_none(),
variant: ButtonVariant::Outline,
onclick: move |_| {
if let (false, Some(active)) = (disabled, remove_active.as_ref()) {
on_remove.call(active.id.clone());
}
},
"{labels.remove}"
}
}
Button {
disabled,
variant: ButtonVariant::Outline,
onclick: move |_| {
if !disabled {
on_apply.call((data_table_saved_state(&reset_state), None));
}
},
"{labels.reset}"
}
}
}
}