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]
name = "bootstrap-rs"
version = "0.2.10"
version = "0.2.11"
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
edition = "2018"

View file

@ -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,

View file

@ -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()
}
}
}