use std::rc::Rc;
use dioxus::prelude::*;
use super::input_form::{FormSignals, use_form_signals};
use crate::class::compose;
use crate::hooks::use_unique_id;
use crate::icons::{icon_svg, lucide};
use crate::overlay::focus_element;
use crate::{Button, ButtonSize, ButtonVariant, Field, FieldError, Label};
#[derive(Props, Clone, PartialEq)]
pub struct PasswordFieldProps {
#[props(default = "Password".to_string(), into)]
pub label: String,
#[props(into)]
pub hint: Option<String>,
#[props(into)]
pub error: Option<String>,
#[props(default = "Show password".to_string(), into)]
pub show_label: String,
#[props(default = "Hide password".to_string(), into)]
pub hide_label: String,
#[props(into)]
pub id: Option<String>,
#[props(into)]
pub name: Option<String>,
#[props(into)]
pub aria_describedby: Option<String>,
pub oninput: Option<EventHandler<FormEvent>>,
pub onchange: Option<EventHandler<FormEvent>>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = input)]
pub attributes: Vec<Attribute>,
}
fn password_field_described_by(
aria_describedby: Option<&str>,
hint_id: Option<&str>,
error_id: Option<&str>,
) -> Option<String> {
Some(compose([aria_describedby, hint_id, error_id])).filter(|ids| !ids.is_empty())
}
#[component]
pub fn PasswordField(props: PasswordFieldProps) -> Element {
let generated = use_unique_id();
let control_id = props
.id
.clone()
.unwrap_or_else(|| format!("ui-password-{generated}"));
let hint = props.hint.clone().filter(|hint| !hint.is_empty());
let error = props.error.clone().filter(|error| !error.is_empty());
let hint_id = hint.as_ref().map(|_| format!("{control_id}-hint"));
let error_id = format!("{control_id}-error");
let described_by = password_field_described_by(
props.aria_describedby.as_deref(),
hint_id.as_deref(),
error.as_ref().map(|_| error_id.as_str()),
);
let form = use_form_signals().unwrap_or_default();
let mut shown_at = use_signal(|| None::<FormSignals>);
let visible = *shown_at.read() == Some(form);
let mut input = use_signal(|| None::<Rc<MountedData>>);
let class = compose([Some("ui-password-field"), props.class.as_deref()]);
let invalid = error.is_some();
let aria_invalid = invalid.then_some("true");
let toggle_label = if visible {
props.hide_label
} else {
props.show_label
};
let (oninput, onchange) = (props.oninput, props.onchange);
rsx! {
Field {
class,
control_id: control_id.clone(),
described_by: described_by.clone(),
invalid,
Label { r#for: control_id.clone(), "{props.label}" }
div { class: "ui-password-field__control", "data-ui-password-field": "true",
input {
aria_describedby: described_by,
aria_invalid,
class: "ui-input",
id: control_id.clone(),
name: props.name,
r#type: if visible { "text" } else { "password" },
onmounted: move |event| input.set(Some(event.data())),
oninput: move |event| {
if let Some(handler) = oninput {
handler.call(event);
}
},
onchange: move |event| {
if let Some(handler) = onchange {
handler.call(event);
}
},
..props.attributes,
}
Button {
aria_controls: control_id.clone(),
aria_label: toggle_label,
aria_pressed: if visible { "true" } else { "false" },
"data-ui-password-toggle": "true",
size: ButtonSize::Icon,
variant: ButtonVariant::Ghost,
onclick: move |event: MouseEvent| {
event.prevent_default();
shown_at.set(if visible { None } else { Some(form) });
focus_element(input());
},
span {
aria_hidden: "true",
class: "ui-password-field__icon",
"data-ui-password-icon": "show",
{icon_svg(&lucide::EYE, "ui-icon__svg lucide-eye")}
}
span {
aria_hidden: "true",
class: "ui-password-field__icon",
"data-ui-password-icon": "hide",
{icon_svg(&lucide::EYE_OFF, "ui-icon__svg lucide-eye-off")}
}
}
}
if let Some(hint) = hint {
p { class: "ui-field__hint", "data-ui-field-hint": "true", id: hint_id, "{hint}" }
}
FieldError { message: error }
}
}
}
#[cfg(test)]
mod tests {
use super::password_field_described_by;
#[test]
fn described_by_lists_app_ids_then_hint_then_error() {
assert_eq!(password_field_described_by(None, None, None), None);
assert_eq!(
password_field_described_by(Some("extra"), Some("pw-hint"), Some("pw-error"))
.as_deref(),
Some("extra pw-hint pw-error")
);
assert_eq!(
password_field_described_by(None, None, Some("pw-error")).as_deref(),
Some("pw-error")
);
}
}