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