use yew::{
classes, function_component, html, AttrValue, Callback, Children, Classes, Html, Properties,
};
use crate::filled_input::FilledInput;
use crate::form_control::FormControl;
use crate::form_helper_text::FormHelperText;
use crate::input::Input;
use crate::input_label::InputLabel;
use crate::outlined_input::OutlinedInput;
use crate::select::Select;
use crate::styles::color::Color;
use crate::styles::input_type::InputType;
use crate::styles::label_variant::LabelVariant;
use crate::styles::margin::Margin;
use crate::styles::size::Size;
use crate::utils::global_id;
#[derive(Debug, Clone, PartialEq, Properties)]
pub struct Props {
#[prop_or_default]
pub auto_complete: AttrValue,
#[prop_or(false)]
pub auto_focus: bool,
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub classes: Classes,
#[prop_or_default]
pub color: Color,
#[prop_or_default]
pub default_value: AttrValue,
#[prop_or(false)]
pub disabled: bool,
#[prop_or(false)]
pub error: bool,
#[prop_or(false)]
pub full_width: bool,
#[prop_or_default]
pub helper_text: Option<Html>,
#[prop_or_default]
pub id: AttrValue,
#[prop_or_default]
pub label: Option<Html>,
#[prop_or_default]
pub margin: Margin,
#[prop_or_default]
pub max_rows: Option<i32>,
#[prop_or_default]
pub min_rows: Option<i32>,
#[prop_or(false)]
pub multiline: bool,
#[prop_or_default]
pub name: AttrValue,
#[prop_or_default]
pub on_blur: Option<Callback<()>>,
#[prop_or_default]
pub on_change: Option<Callback<AttrValue>>,
#[prop_or_default]
pub on_focus: Option<Callback<()>>,
#[prop_or_default]
pub placeholder: AttrValue,
#[prop_or(false)]
pub required: bool,
#[prop_or_default]
pub rows: Option<i32>,
#[prop_or(false)]
pub select: bool,
#[prop_or(Size::Medium)]
pub size: Size,
#[prop_or_default]
pub style: AttrValue,
#[prop_or_default]
pub input_type: InputType,
#[prop_or(false)]
pub input_read_only: bool,
#[prop_or(false)]
pub input_label_shrink: bool,
#[prop_or_default]
pub value: AttrValue,
#[prop_or_default]
pub variant: LabelVariant,
}
#[function_component(TextField)]
pub fn text_field(props: &Props) -> Html {
let id = AttrValue::from(global_id());
let input_label_id = AttrValue::from(format!("{id}-label"));
let helper_text_id = AttrValue::from(format!("{id}-helper-text"));
let root_cls = classes!("ZuTextField-root", props.classes.clone());
let input_variant = get_input_variant(props, &id, &helper_text_id);
let input_content = if props.select {
html! {
<Select
aria_described_by={&helper_text_id}
id={&id}
label_id={&input_label_id}
input={input_variant}
value={&props.value}
>
{for props.children.iter()}
</Select>
}
} else {
input_variant
};
html! {
<FormControl
classes={root_cls}
color={props.color}
disabled={props.disabled}
error={props.error}
full_width={props.full_width}
required={props.required}
variant={props.variant}
>
if let Some(label) = &props.label {
<InputLabel
id={input_label_id}
html_for={id}
shrink={props.input_label_shrink}>
{label.clone()}
</InputLabel>
}
{input_content}
if let Some(helper_text) = &props.helper_text {
<FormHelperText id={helper_text_id}>
{helper_text.clone()}
</FormHelperText>
}
</FormControl>
}
}
fn get_input_variant(props: &Props, id: &AttrValue, helper_text_id: &AttrValue) -> Html {
match props.variant {
LabelVariant::Filled => html! {
<Input
aria_described_by={helper_text_id}
auto_complete={&props.auto_complete}
auto_focus={props.auto_focus}
default_value={&props.default_value}
full_width={props.full_width}
multiline={props.multiline}
name={&props.name}
rows={props.rows}
max_rows={props.max_rows}
min_rows={props.min_rows}
input_type={props.input_type}
value={&props.value}
id={id}
on_blur={&props.on_blur}
on_change={&props.on_change}
on_focus={&props.on_focus}
placeholder={&props.placeholder}
/>
},
LabelVariant::Outlined => html! {
<FilledInput
aria_described_by={helper_text_id}
auto_complete={&props.auto_complete}
auto_focus={props.auto_focus}
default_value={&props.default_value}
full_width={props.full_width}
multiline={props.multiline}
name={&props.name}
rows={props.rows}
max_rows={props.max_rows}
min_rows={props.min_rows}
input_type={props.input_type}
value={&props.value}
id={id}
on_blur={&props.on_blur}
on_change={&props.on_change}
on_focus={&props.on_focus}
placeholder={&props.placeholder}
/>
},
LabelVariant::Standard => html! {
<OutlinedInput
aria_described_by={helper_text_id}
auto_complete={&props.auto_complete}
auto_focus={props.auto_focus}
default_value={&props.default_value}
full_width={props.full_width}
multiline={props.multiline}
name={&props.name}
rows={props.rows}
max_rows={props.max_rows}
min_rows={props.min_rows}
input_type={props.input_type}
value={&props.value}
id={id}
on_blur={&props.on_blur}
on_change={&props.on_change}
on_focus={&props.on_focus}
placeholder={&props.placeholder}
/>
},
}
}