Refine textarea value, input props.

This commit is contained in:
Thomas Gideon 2020-06-25 14:27:01 -04:00
parent 88e53935b4
commit 404aff275a
3 changed files with 30 additions and 17 deletions

View File

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

View File

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

View File

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