🧑💻 Simplify component class access functions
This commit is contained in:
parent
d69d4cda45
commit
9a5618ef4b
12 changed files with 82 additions and 94 deletions
|
|
@ -92,40 +92,55 @@ impl ThemeTrait for Bootsier {
|
|||
match component.type_id() {
|
||||
t if t == TypeId::of::<Icon>() => {
|
||||
if let Some(i) = component_as_mut::<Icon>(component) {
|
||||
i.replace_classes(i.font_size().to_string(), with_font(i.font_size()));
|
||||
i.alter_classes(
|
||||
ClassesOp::Replace(i.font_size().to_string()),
|
||||
with_font(i.font_size()),
|
||||
);
|
||||
}
|
||||
}
|
||||
t if t == TypeId::of::<Button>() => {
|
||||
if let Some(b) = component_as_mut::<Button>(component) {
|
||||
b.replace_classes("button__tap", "btn");
|
||||
b.replace_classes(b.style().to_string(), match b.style() {
|
||||
StyleBase::Default => "btn-primary",
|
||||
StyleBase::Success => "btn-success",
|
||||
StyleBase::Danger => "btn-danger",
|
||||
StyleBase::Warning => "btn-warning",
|
||||
StyleBase::Info => "btn-info",
|
||||
StyleBase::Light => "btn-light",
|
||||
StyleBase::Dark => "btn-dark",
|
||||
StyleBase::Link => "btn-link",
|
||||
});
|
||||
b.replace_classes(b.font_size().to_string(), with_font(b.font_size()));
|
||||
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "btn");
|
||||
b.alter_classes(
|
||||
ClassesOp::Replace(b.style().to_string()),
|
||||
match b.style() {
|
||||
StyleBase::Default => "btn-primary",
|
||||
StyleBase::Success => "btn-success",
|
||||
StyleBase::Danger => "btn-danger",
|
||||
StyleBase::Warning => "btn-warning",
|
||||
StyleBase::Info => "btn-info",
|
||||
StyleBase::Light => "btn-light",
|
||||
StyleBase::Dark => "btn-dark",
|
||||
StyleBase::Link => "btn-link",
|
||||
},
|
||||
);
|
||||
b.alter_classes(
|
||||
ClassesOp::Replace(b.font_size().to_string()),
|
||||
with_font(b.font_size()),
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
t if t == TypeId::of::<Heading>() => {
|
||||
if let Some(h) = component_as_mut::<Heading>(component) {
|
||||
h.replace_classes(h.size().to_string(), match h.size() {
|
||||
HeadingSize::ExtraLarge => "display-1",
|
||||
HeadingSize::XxLarge => "display-2",
|
||||
HeadingSize::XLarge => "display-3",
|
||||
HeadingSize::Large => "display-4",
|
||||
HeadingSize::Medium => "display-5",
|
||||
_ => "",
|
||||
});
|
||||
h.alter_classes(
|
||||
ClassesOp::Replace(h.size().to_string()),
|
||||
match h.size() {
|
||||
HeadingSize::ExtraLarge => "display-1",
|
||||
HeadingSize::XxLarge => "display-2",
|
||||
HeadingSize::XLarge => "display-3",
|
||||
HeadingSize::Large => "display-4",
|
||||
HeadingSize::Medium => "display-5",
|
||||
_ => "",
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
t if t == TypeId::of::<Paragraph>() => {
|
||||
if let Some(p) = component_as_mut::<Paragraph>(component) {
|
||||
p.replace_classes(p.font_size().to_string(), with_font(p.font_size()));
|
||||
p.alter_classes(
|
||||
ClassesOp::Replace(p.font_size().to_string()),
|
||||
with_font(p.font_size()),
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
|||
|
|
@ -44,39 +44,51 @@ impl ThemeTrait for Bulmix {
|
|||
match component.type_id() {
|
||||
t if t == TypeId::of::<Icon>() => {
|
||||
if let Some(i) = component_as_mut::<Icon>(component) {
|
||||
i.replace_classes(i.font_size().to_string(), with_font(i.font_size()));
|
||||
i.alter_classes(
|
||||
ClassesOp::Replace(i.font_size().to_string()),
|
||||
with_font(i.font_size()),
|
||||
);
|
||||
}
|
||||
}
|
||||
t if t == TypeId::of::<Button>() => {
|
||||
if let Some(b) = component_as_mut::<Button>(component) {
|
||||
b.replace_classes("button__tap", "button");
|
||||
b.replace_classes(b.style().to_string(), match b.style() {
|
||||
StyleBase::Default => "is-primary",
|
||||
StyleBase::Success => "is-success",
|
||||
StyleBase::Danger => "is-danger",
|
||||
StyleBase::Warning => "is-warning",
|
||||
StyleBase::Info => "is-info",
|
||||
StyleBase::Light => "is-light",
|
||||
StyleBase::Dark => "is-dark",
|
||||
StyleBase::Link => "is-text",
|
||||
});
|
||||
b.replace_classes(b.font_size().to_string(), with_font(b.font_size()));
|
||||
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "button");
|
||||
b.alter_classes(
|
||||
ClassesOp::Replace(b.style().to_string()),
|
||||
match b.style() {
|
||||
StyleBase::Default => "is-primary",
|
||||
StyleBase::Success => "is-success",
|
||||
StyleBase::Danger => "is-danger",
|
||||
StyleBase::Warning => "is-warning",
|
||||
StyleBase::Info => "is-info",
|
||||
StyleBase::Light => "is-light",
|
||||
StyleBase::Dark => "is-dark",
|
||||
StyleBase::Link => "is-text",
|
||||
},
|
||||
);
|
||||
b.alter_classes(
|
||||
ClassesOp::Replace(b.font_size().to_string()),
|
||||
with_font(b.font_size()),
|
||||
);
|
||||
}
|
||||
}
|
||||
t if t == TypeId::of::<Heading>() => {
|
||||
if let Some(h) = component_as_mut::<Heading>(component) {
|
||||
match h.size() {
|
||||
HeadingSize::Subtitle => {
|
||||
h.replace_classes(h.size().to_string(), "subtitle")
|
||||
h.alter_classes(ClassesOp::Replace(h.size().to_string()), "subtitle")
|
||||
}
|
||||
_ => h.add_classes("title"),
|
||||
_ => h.alter_classes(ClassesOp::Add, "title"),
|
||||
};
|
||||
}
|
||||
}
|
||||
t if t == TypeId::of::<Paragraph>() => {
|
||||
if let Some(p) = component_as_mut::<Paragraph>(component) {
|
||||
p.add_classes("block");
|
||||
p.replace_classes(p.font_size().to_string(), with_font(p.font_size()));
|
||||
p.alter_classes(ClassesOp::Add, "block");
|
||||
p.alter_classes(
|
||||
ClassesOp::Replace(p.font_size().to_string()),
|
||||
with_font(p.font_size()),
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ impl ComponentTrait for Block {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.prepend_classes("block__container");
|
||||
self.alter_classes(ClassesOp::Prepend, "block__container");
|
||||
}
|
||||
|
||||
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ impl ComponentTrait for Button {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.prepend_classes(
|
||||
self.alter_classes(
|
||||
ClassesOp::Prepend,
|
||||
[
|
||||
"button__tap".to_string(),
|
||||
self.style().to_string(),
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ impl ComponentTrait for Container {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, cx: &mut Context) {
|
||||
self.prepend_classes(
|
||||
self.alter_classes(
|
||||
ClassesOp::Prepend,
|
||||
[
|
||||
self.direction().to_string(),
|
||||
self.wrap_align().to_string(),
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ impl ComponentTrait for Item {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.prepend_classes(
|
||||
self.alter_classes(
|
||||
ClassesOp::Prepend,
|
||||
[
|
||||
"flex-item__container".to_owned(),
|
||||
self.grow().to_string(),
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@ impl ComponentTrait for ActionButton {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.prepend_classes(
|
||||
self.alter_classes(
|
||||
ClassesOp::Prepend,
|
||||
[
|
||||
"button__tap".to_string(),
|
||||
self.style().to_string(),
|
||||
|
|
|
|||
|
|
@ -162,9 +162,9 @@ impl Input {
|
|||
#[fn_builder]
|
||||
pub fn alter_name(&mut self, name: &str) -> &mut Self {
|
||||
if let Some(previous) = self.name.get() {
|
||||
self.remove_classes(concat_string!("form-item-", previous));
|
||||
self.alter_classes(ClassesOp::Remove, concat_string!("form-item-", previous));
|
||||
}
|
||||
self.add_classes(concat_string!("form-item-", name));
|
||||
self.alter_classes(ClassesOp::Add, concat_string!("form-item-", name));
|
||||
self.name.alter_value(name);
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ impl ComponentTrait for Heading {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.add_classes(self.size().to_string());
|
||||
self.alter_classes(ClassesOp::Add, self.size().to_string());
|
||||
}
|
||||
|
||||
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ impl ComponentTrait for Icon {
|
|||
#[rustfmt::skip]
|
||||
fn setup_before_prepare(&mut self, cx: &mut Context) {
|
||||
if let Some(icon_name) = self.icon_name().get() {
|
||||
self.prepend_classes(
|
||||
self.alter_classes(ClassesOp::Prepend,
|
||||
concat_string!("bi-", icon_name, " ", self.font_size().to_string()),
|
||||
);
|
||||
cx.set_param::<bool>(PARAM_BASE_INCLUDE_ICONS, true);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ impl ComponentTrait for Paragraph {
|
|||
}
|
||||
|
||||
fn setup_before_prepare(&mut self, _cx: &mut Context) {
|
||||
self.prepend_classes(self.font_size().to_string());
|
||||
self.alter_classes(ClassesOp::Prepend, self.font_size().to_string());
|
||||
}
|
||||
|
||||
fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup {
|
||||
|
|
|
|||
|
|
@ -3,18 +3,6 @@ use crate::html::{ClassesOp, OptionClasses};
|
|||
|
||||
pub trait ComponentClassesOp {
|
||||
fn with_classes(self, op: ClassesOp, classes: impl Into<String>) -> Self;
|
||||
|
||||
fn add_classes(&mut self, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn prepend_classes(&mut self, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn remove_classes(&mut self, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn replace_classes(&mut self, rep: impl Into<String>, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn toggle_classes(&mut self, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn set_classes(&mut self, classes: impl Into<String>) -> &mut Self;
|
||||
}
|
||||
|
||||
pub trait ComponentClasses: ComponentBase + ComponentClassesOp {
|
||||
|
|
@ -24,39 +12,8 @@ pub trait ComponentClasses: ComponentBase + ComponentClassesOp {
|
|||
}
|
||||
|
||||
impl<C: ComponentBase + ComponentClasses> ComponentClassesOp for C {
|
||||
#[doc(hidden)]
|
||||
fn with_classes(mut self, op: ClassesOp, classes: impl Into<String>) -> Self {
|
||||
self.alter_classes(op, classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn add_classes(&mut self, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Add, classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn prepend_classes(&mut self, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Prepend, classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn remove_classes(&mut self, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Remove, classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn replace_classes(&mut self, rep: impl Into<String>, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Replace(rep.into()), classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn toggle_classes(&mut self, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Toggle, classes);
|
||||
self
|
||||
}
|
||||
|
||||
fn set_classes(&mut self, classes: impl Into<String>) -> &mut Self {
|
||||
self.alter_classes(ClassesOp::Set, classes);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue