Browse Source

Refine textarea value, input props.

main
Thomas Gideon 2 years ago
parent
commit
404aff275a
  1. 2
      Cargo.toml
  2. 1
      src/input/props.rs
  3. 44
      src/input/textarea.rs

2
Cargo.toml

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

1
src/input/props.rs

@ -15,6 +15,7 @@ pub struct Props {
pub value: String,
#[prop_or_default]
pub valid: Option<bool>,
#[prop_or_default]
pub on_change: Callback<String>,
#[prop_or_default]
pub readonly: bool,

44
src/input/textarea.rs

@ -16,16 +16,7 @@ impl Component for TextArea {
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
let state = if props.children.is_empty() {
props.value.clone()
} else {
let node = props.children.render();
if let VNode::VText(text) = node {
text.text
} else {
props.value.clone()
}
};
let state = extract_state(&props);
Self { props, state, link }
}
@ -42,7 +33,7 @@ impl Component for TextArea {
fn change(&mut self, props: Self::Properties) -> ShouldRender {
let should_render = render_on_change(&mut self.props, props);
if should_render {
self.state = self.props.value.clone();
self.state = extract_state(&self.props);
}
should_render
}
@ -50,12 +41,33 @@ impl Component for TextArea {
fn view(&self) -> Html {
let prefix = vec!["form-control", &valid_as_class(&self.props.valid)];
let html = html! {
<textarea
onchange=self.link.callback(|evt| InputChange(evt))
>
{ &self.state }
</textarea>
<textarea
readonly=self.props.readonly
onchange=self.link.callback(|evt| InputChange(evt))
>
{ &self.state }
</textarea>
};
render::render_with_prefix(&self.props, prefix, html)
}
}
fn extract_state(props: &Props) -> String {
if props.children.is_empty() {
props.value.clone()
} else {
let node = props.children.render();
if let VNode::VText(text) = node {
text.text
} else if let VNode::VList(list) = node {
list.iter().fold(String::new(), |mut buf, node| {
if let VNode::VText(text) = node {
buf.push_str(&text.text)
}
buf
})
} else {
props.value.clone()
}
}
}

Loading…
Cancel
Save