Browse Source

Add validation styling

main
Thomas Gideon 2 years ago
parent
commit
bfa2f0a64a
  1. 2
      Cargo.toml
  2. 12
      src/input/mod.rs
  3. 5
      src/input/textarea.rs
  4. 8
      src/prelude/mod.rs

2
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "bootstrap-rs"
version = "0.2.3"
version = "0.2.4"
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
edition = "2018"

12
src/input/mod.rs

@ -55,6 +55,8 @@ pub struct Props {
pub class: String,
#[prop_or_default]
pub style: String,
#[prop_or_default]
pub valid: Option<bool>,
}
impl Component for Input {
@ -84,11 +86,15 @@ impl Component for Input {
}
fn view(&self) -> Html {
let class = if self.props.readonly {
calculate_classes("form-control-plaintext", (&self.props).into())
let prefix = if self.props.readonly {
format!(
"form-control-plaintext{}",
valid_as_class(&self.props.valid)
)
} else {
calculate_classes("form-control", (&self.props).into())
format!("form-control{}", valid_as_class(&self.props.valid))
};
let class = calculate_classes(prefix, (&self.props).into());
html! {
<input
name=&self.props.name

5
src/input/textarea.rs

@ -35,6 +35,8 @@ pub struct Props {
pub style: String,
#[prop_or_default]
pub value: String,
#[prop_or_default]
pub valid: Option<bool>,
}
impl Component for TextArea {
@ -65,11 +67,12 @@ impl Component for TextArea {
}
fn view(&self) -> Html {
let prefix = format!("form-control{}", valid_as_class(&self.props.valid));
html! {
<textarea
name=&self.props.name
id=&self.props.id
class=calculate_classes("form-control", (&self.props).into())
class=calculate_classes(prefix, (&self.props).into())
onchange=self.link.callback(|evt| InputChange(evt))
>
{ &self.state }

8
src/prelude/mod.rs

@ -124,6 +124,14 @@ pub fn render_on_change<P: Properties + PartialEq>(
}
}
pub fn valid_as_class(v: &Option<bool>) -> &'static str {
match v {
None => "",
Some(true) => " is-valid",
Some(false) => " is-invalid",
}
}
pub fn collect_bs<'a, T>(t: &'a Option<T>, ts: &'a [T]) -> Vec<&'a T> {
if let Some(t) = t.as_ref() {
let mut r = vec![t];

Loading…
Cancel
Save