Standardize props

This commit is contained in:
Thomas Gideon 2020-06-03 15:33:25 -04:00
parent 96b6152d7b
commit c833bb1957
17 changed files with 358 additions and 252 deletions

View file

@ -1,20 +1,13 @@
use super::{Color, Edge};
#[derive(Clone, PartialEq)]
pub enum Border {
All,
Top,
Right,
Bottom,
Left,
}
pub struct Border(pub Edge, pub Color);
impl super::BootstrapClass for Border {
fn as_classname(&self) -> String {
match self {
Self::All => "border".into(),
Self::Top => "border-top".into(),
Self::Right => "border-right".into(),
Self::Bottom => "border-bottom".into(),
Self::Left => "border-left".into(),
}
let edge = match self.0 {
Edge::All => "border".to_owned(),
_ => self.0.with_prefix("border"),
};
format!("{} {}", edge, self.1.with_prefix("border"))
}
}

View file

@ -1,22 +0,0 @@
#[derive(Clone, PartialEq)]
pub enum BorderColor {
Primary,
Secondary,
Unset,
}
impl Default for BorderColor {
fn default() -> Self {
BorderColor::Unset
}
}
impl super::BootstrapClass for BorderColor {
fn as_classname(&self) -> String {
match self {
Self::Primary => "border-primary".into(),
Self::Secondary => "border-secondary".into(),
Self::Unset => "".into(),
}
}
}

36
src/prelude/color.rs Normal file
View file

@ -0,0 +1,36 @@
#[derive(Clone, PartialEq)]
pub enum Color {
Primary,
Secondary,
Success,
Danger,
Warning,
Info,
Light,
Dark,
White,
Unset,
}
impl Default for Color {
fn default() -> Self {
Self::Unset
}
}
impl Color {
pub fn with_prefix<S: AsRef<str>>(&self, prefix: S) -> String {
match self {
Self::Primary => format!("{}-primary", prefix.as_ref()),
Self::Secondary => format!("{}-secondary", prefix.as_ref()),
Self::Success => format!("{}-success", prefix.as_ref()),
Self::Danger => format!("{}-danger", prefix.as_ref()),
Self::Warning => format!("{}-warning", prefix.as_ref()),
Self::Info => format!("{}-info", prefix.as_ref()),
Self::Light => format!("{}-light", prefix.as_ref()),
Self::Dark => format!("{}-dark", prefix.as_ref()),
Self::White => format!("{}-white", prefix.as_ref()),
Self::Unset => "".into(),
}
}
}

View file

@ -1,26 +1,102 @@
mod border;
mod border_color;
mod color;
mod margin;
mod padding;
pub use self::{border::Border, border_color::BorderColor, margin::Margin};
pub use self::{border::Border, color::Color, margin::Margin, padding::Padding};
use std::fmt::{Display, Formatter, Result};
use yew::prelude::*;
#[derive(Properties, Clone, PartialEq, Default)]
pub struct Props {
#[prop_or_default]
pub aria_label: String,
#[prop_or_default]
pub role: String,
#[prop_or_default]
pub border: Option<Border>,
#[prop_or_default]
pub borders: Vec<Border>,
#[prop_or_default]
pub margin: Option<Margin>,
#[prop_or_default]
pub margins: Vec<Margin>,
#[prop_or_default]
pub padding: Option<Padding>,
#[prop_or_default]
pub paddings: Vec<Padding>,
#[prop_or_default]
pub class: String,
#[prop_or_default]
pub style: String,
#[prop_or_default]
pub children: Children,
}
impl<'a> From<&'a Props> for BootstrapProps<'a> {
fn from(props: &Props) -> BootstrapProps {
let class = &props.class;
let borders = collect_bs(&props.border, &props.borders);
let margins = collect_bs(&props.margin, &props.margins);
let paddings = collect_bs(&props.padding, &props.paddings);
BootstrapProps {
class,
borders,
margins,
paddings,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Edge {
All,
Top,
Right,
Bottom,
Left,
LeftAndRight,
TopAndBottom,
}
impl Display for Edge {
fn fmt(&self, fmt: &mut Formatter) -> Result {
match self {
Self::All => write!(fmt, ""),
Self::Top => write!(fmt, "t"),
Self::Bottom => write!(fmt, "b"),
Self::Right => write!(fmt, "r"),
Self::Left => write!(fmt, "l"),
Self::LeftAndRight => write!(fmt, "x"),
Self::TopAndBottom => write!(fmt, "y"),
}
}
}
impl Edge {
fn suffix(&self) -> &str {
match self {
Self::Top => "-top",
_ => "",
}
}
fn with_prefix<S: AsRef<str>>(&self, prefix: S) -> String {
match self {
Self::All => prefix.as_ref().to_owned(),
Self::LeftAndRight => format!(
"{0}{1} {0}{2}",
prefix.as_ref(),
Self::Left.suffix(),
Self::Right.suffix()
),
Self::TopAndBottom => format!(
"{0}{1} {0}{2}",
prefix.as_ref(),
Self::Top.suffix(),
Self::Bottom.suffix()
),
_ => format!("{}{}", prefix.as_ref(), self.suffix()),
}
}
}
@ -30,9 +106,10 @@ trait BootstrapClass {
}
pub struct BootstrapProps<'a> {
pub class: &'a str,
pub borders: Vec<&'a Border>,
pub border_colors: Vec<&'a BorderColor>,
pub margins: Vec<&'a Margin>,
pub paddings: Vec<&'a Padding>,
}
pub fn render_on_change<P: Properties + PartialEq>(
@ -57,16 +134,23 @@ pub fn collect_bs<'a, T>(t: &'a Option<T>, ts: &'a [T]) -> Vec<&'a T> {
}
}
pub fn calculate_classes(props: BootstrapProps) -> String {
pub fn calculate_classes<S: AsRef<str>>(prefix: S, props: BootstrapProps) -> String {
let BootstrapProps {
class,
borders,
border_colors,
margins,
paddings,
} = props;
let mut classes = Vec::new();
if !prefix.as_ref().is_empty() {
classes.push(prefix.as_ref().to_owned());
}
if !props.class.is_empty() {
classes.push(class.to_owned());
}
classes.append(&mut into_classnames(borders));
classes.append(&mut into_classnames(border_colors));
classes.append(&mut into_classnames(margins));
classes.append(&mut into_classnames(paddings));
classes.join(" ")
}

View file

@ -3,6 +3,6 @@ pub struct Padding(pub super::Edge, pub usize);
impl super::BootstrapClass for Padding {
fn as_classname(&self) -> String {
format!("m{}-{}", self.0, self.1)
format!("p{}-{}", self.0, self.1)
}
}