windjammer-ui 0.3.6

Cross-platform UI framework for Windjammer (Web, Desktop, Mobile)
Documentation
use super::traits::Renderable

pub struct Tab {
    id: string,
    label: string,
    content: string,
    disabled: bool,
}

impl Tab {
    pub fn new(id: string, label: string, content: string) -> Tab {
        Tab {
            id,
            label,
            content,
            disabled: false,
        }
    }

    pub fn disabled(self, disabled: bool) -> Tab {
        self.disabled = disabled
        self
    }
}

pub struct Tabs {
    tabs: Vec<Tab>,
    active: string,
}

impl Tabs {
    pub fn new() -> Tabs {
        Tabs {
            tabs: Vec::new(),
            active: "".to_string(),
        }
    }

    pub fn tab(self, tab: Tab) -> Tabs {
        self.tabs.push(tab)
        self
    }

    pub fn active(self, id: string) -> Tabs {
        self.active = id
        self
    }

}

impl Renderable for Tabs {
fn render(self) -> string {
        let mut tabs_html = "<div class='wj-tabs-header'>".to_string()

        let mut i = 0
        while i < self.tabs.len() {
            let tab = &self.tabs[i]
            let active_class = if tab.id == self.active { " wj-tab-active" } else { "" }
            let disabled_class = if tab.disabled { " wj-tab-disabled" } else { "" }

            tabs_html = format!("{}<button class='wj-tab{}{}' data-tab-id='{}'>{}</button>",
                tabs_html, active_class, disabled_class, tab.id, tab.label)

            i = i + 1
        }

        tabs_html = format!("{}</div>", tabs_html)

        let mut content_html = "<div class='wj-tabs-content'>".to_string()

        let mut j = 0
        while j < self.tabs.len() {
            let tab = &self.tabs[j]
            let display_style = if tab.id == self.active {
                "display: block;"
            } else {
                "display: none;"
            }

            content_html = format!("{}<div class='wj-tab-panel' data-tab-id='{}' style='{}'>{}</div>",
                content_html, tab.id, display_style, tab.content)

            j = j + 1
        }

        content_html = format!("{}</div>", content_html)

        format!("<div class='wj-tabs'>{}{}</div>", tabs_html, content_html)
    }
}

fn main() {
    let tabs = Tabs::new()
        .tab(Tab::new("home".to_string(), "Home".to_string(), "<p>Home content</p>".to_string()))
        .tab(Tab::new("profile".to_string(), "Profile".to_string(), "<p>Profile content</p>".to_string()))
        .tab(Tab::new("settings".to_string(), "Settings".to_string(), "<p>Settings content</p>".to_string()))
        .active("home".to_string())

    println!("{}", tabs.render())
}