Refine textarea value, input props.
This commit is contained in:
parent
88e53935b4
commit
404aff275a
3 changed files with 30 additions and 17 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bootstrap-rs"
|
||||
version = "0.2.10"
|
||||
version = "0.2.11"
|
||||
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -51,6 +42,7 @@ impl Component for TextArea {
|
|||
let prefix = vec!["form-control", &valid_as_class(&self.props.valid)];
|
||||
let html = html! {
|
||||
<textarea
|
||||
readonly=self.props.readonly
|
||||
onchange=self.link.callback(|evt| InputChange(evt))
|
||||
>
|
||||
{ &self.state }
|
||||
|
@ -59,3 +51,23 @@ impl Component for 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…
Reference in a new issue