bootstrap-rs/src/input/textarea.rs

98 lines
2.5 KiB
Rust
Raw Normal View History

2020-05-28 18:09:25 +00:00
use crate::prelude::*;
use yew::prelude::*;
pub struct TextArea {
link: ComponentLink<Self>,
state: String,
props: Props,
}
#[derive(Debug)]
pub struct InputChange(ChangeData);
#[derive(Properties, Clone, PartialEq)]
pub struct Props {
#[prop_or_default]
pub name: String,
#[prop_or_default]
pub id: String,
2020-06-03 19:33:25 +00:00
pub on_change: Callback<String>,
#[prop_or_default]
pub border: Option<Border>,
#[prop_or_default]
pub borders: Vec<Border>,
#[prop_or_default]
pub margin: Option<Margin>,
#[prop_or_default]
pub margins: Vec<Margin>,
#[prop_or_default]
pub padding: Option<Padding>,
#[prop_or_default]
pub paddings: Vec<Padding>,
#[prop_or_default]
pub class: String,
#[prop_or_default]
pub style: String,
2020-06-09 20:10:45 +00:00
#[prop_or_default]
pub value: String,
2020-06-19 20:35:43 +00:00
#[prop_or_default]
pub valid: Option<bool>,
2020-05-28 18:09:25 +00:00
}
impl Component for TextArea {
type Message = InputChange;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
2020-06-09 20:10:45 +00:00
let state = props.value.clone();
2020-05-28 18:09:25 +00:00
Self { props, state, link }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
if let InputChange(ChangeData::Value(value)) = msg {
self.state = value.clone();
2020-06-03 19:33:25 +00:00
self.props.on_change.emit(value);
2020-05-28 18:09:25 +00:00
true
} else {
false
}
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
2020-06-09 20:10:45 +00:00
let should_render = render_on_change(&mut self.props, props);
if should_render {
self.state = self.props.value.clone();
}
should_render
2020-05-28 18:09:25 +00:00
}
fn view(&self) -> Html {
2020-06-19 20:35:43 +00:00
let prefix = format!("form-control{}", valid_as_class(&self.props.valid));
2020-05-28 18:09:25 +00:00
html! {
<textarea
name=&self.props.name
id=&self.props.id
2020-06-19 20:35:43 +00:00
class=calculate_classes(prefix, (&self.props).into())
2020-05-28 18:09:25 +00:00
onchange=self.link.callback(|evt| InputChange(evt))
>
{ &self.state }
</textarea>
}
}
}
2020-06-03 19:33:25 +00:00
impl<'a> From<&'a Props> for BootstrapProps<'a> {
fn from(props: &Props) -> BootstrapProps {
let class = &props.class;
let borders = collect_bs(&props.border, &props.borders);
let margins = collect_bs(&props.margin, &props.margins);
let paddings = collect_bs(&props.padding, &props.paddings);
BootstrapProps {
class,
borders,
margins,
paddings,
}
}
}