Skip to main content

patternfly_yew/components/breadcrumb/
variant.rs

1use std::rc::Rc;
2use yew::{
3    prelude::*,
4    virtual_dom::{VChild, VComp},
5};
6
7use super::{BreadcrumbItem, BreadcrumbItemProperties};
8
9pub trait BreadcrumbItemCreator {
10    fn create(self: Rc<Self>, current: bool) -> Html;
11}
12
13impl BreadcrumbItemCreator for BreadcrumbItemProperties {
14    fn create(mut self: Rc<Self>, current: bool) -> Html {
15        let props = Rc::make_mut(&mut self);
16        props.current = current;
17        VComp::new::<BreadcrumbItem>(self, None).into()
18    }
19}
20
21#[derive(Clone)]
22pub struct BreadcrumbChild {
23    pub creator: Rc<dyn BreadcrumbItemCreator>,
24}
25
26impl PartialEq for BreadcrumbChild {
27    fn eq(&self, other: &Self) -> bool {
28        Rc::ptr_eq(&self.creator, &other.creator)
29    }
30}
31
32impl From<BreadcrumbItemProperties> for BreadcrumbChild {
33    fn from(props: BreadcrumbItemProperties) -> Self {
34        Self {
35            creator: Rc::new(props),
36        }
37    }
38}
39
40#[derive(Clone, PartialEq)]
41pub struct BreadcrumbItemVariant {
42    props: BreadcrumbChild,
43    current: bool,
44}
45
46impl BreadcrumbItemVariant {
47    pub fn set_current(&mut self, current: bool) {
48        self.current = current;
49    }
50}
51
52impl<CHILD> From<VChild<CHILD>> for BreadcrumbItemVariant
53where
54    CHILD: BaseComponent,
55    CHILD::Properties: Into<BreadcrumbChild> + Clone,
56{
57    fn from(vchild: VChild<CHILD>) -> Self {
58        Self {
59            props: (*vchild.props).clone().into(),
60            current: false,
61        }
62    }
63}
64
65impl From<BreadcrumbItemVariant> for Html {
66    fn from(value: BreadcrumbItemVariant) -> Self {
67        value.props.creator.create(value.current)
68    }
69}