Current section
Files
Jump to
Current section
Files
src/m3e/form_field.gleam
//// FormField is a container for form controls that applies Material Design styling and behavior.
////
//// This file was generated by m3e/generator
////
//// DO NOT EDIT
////
import gleam/list
import lustre/attribute.{type Attribute}
import lustre/element.{type Element}
import m3e/attr
import m3e/float_label_type.{type FloatLabelType}
import m3e/form_field_variant.{type FormFieldVariant}
import m3e/hide_subscript_type.{type HideSubscriptType}
// --- Types ---
/// FormField is a View Model for this component
///
/// ## Fields:
///
/// - float_label: Specifies whether the label should float always or only when necessary.
/// - hide_required_marker: Whether the required marker should be hidden.
/// - hide_subscript: Whether subscript content is hidden.
/// - variant: The appearance variant of the field.
///
pub opaque type FormField {
FormField(
float_label: FloatLabelType,
hide_required_marker: HideRequiredMarker,
hide_subscript: HideSubscriptType,
variant: FormFieldVariant,
)
}
/// HideRequiredMarker is whether the required marker should be hidden.
///
pub type HideRequiredMarker {
IsHideRequiredMarker
IsNotHideRequiredMarker
}
// --- Defaults ---
pub const default_float_label: FloatLabelType = float_label_type.Auto
pub const default_hide_required_marker: HideRequiredMarker = IsNotHideRequiredMarker
pub const default_hide_subscript: HideSubscriptType = hide_subscript_type.Auto
pub const default_variant: FormFieldVariant = form_field_variant.Outlined
/// Slots are used in child elements to insert content into this component
///
pub type Slot {
Prefix
// Renders content before the fields's control.
PrefixText
// Renders text before the fields's control.
Suffix
// Renders content after the fields's control.
SuffixText
// Renders text after the fields's control.
Hint
// Renders hint text in the fields's subscript, when the control is valid.
Error
// Renders error text in the fields's subscript, when the control is invalid.
}
// --- Configuration ---
/// Config is a public record for configuring this component.
///
pub type Config {
Config(
float_label: FloatLabelType,
hide_required_marker: HideRequiredMarker,
hide_subscript: HideSubscriptType,
variant: FormFieldVariant,
)
}
/// default_config is the default configuration for this component.
///
pub fn default_config() -> Config {
Config(
float_label: float_label_type.Auto,
hide_required_marker: IsNotHideRequiredMarker,
hide_subscript: hide_subscript_type.Auto,
variant: form_field_variant.Outlined,
)
}
// --- Constructors ---
/// from_config creates a new FormField from the given configuration.
///
pub fn from_config(config: Config) -> FormField {
FormField(
float_label: config.float_label,
hide_required_marker: config.hide_required_marker,
hide_subscript: config.hide_subscript,
variant: config.variant,
)
}
/// new creates a new FormField with the default configuration.
///
pub fn new() -> FormField {
from_config(default_config())
}
// --- Setters ---
/// float_label sets the value of float_label for this FormField.
///
pub fn float_label(record: FormField, float_label: FloatLabelType) -> FormField {
FormField(..record, float_label: float_label)
}
/// hide_required_marker sets the value of hide_required_marker for this FormField.
///
pub fn hide_required_marker(
record: FormField,
hide_required_marker: HideRequiredMarker,
) -> FormField {
FormField(..record, hide_required_marker: hide_required_marker)
}
/// hide_subscript sets the value of hide_subscript for this FormField.
///
pub fn hide_subscript(
record: FormField,
hide_subscript: HideSubscriptType,
) -> FormField {
FormField(..record, hide_subscript: hide_subscript)
}
/// variant sets the value of variant for this FormField.
///
pub fn variant(record: FormField, variant: FormFieldVariant) -> FormField {
FormField(..record, variant: variant)
}
// --- Renderers ---
/// render creates a Lustre Element for a FormField
///
pub fn render(
model: FormField,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
element.element(
"m3e-form-field",
list.flatten([
[
attr.with_default(
"float-label",
float_label_type.to_string(model.float_label),
float_label_type.to_string(default_float_label),
),
attr.boolean(
"hide-required-marker",
model.hide_required_marker == IsHideRequiredMarker,
),
attr.with_default(
"hide-subscript",
hide_subscript_type.to_string(model.hide_subscript),
hide_subscript_type.to_string(default_hide_subscript),
),
attr.with_default(
"variant",
form_field_variant.to_string(model.variant),
form_field_variant.to_string(default_variant),
),
],
attributes,
])
|> list.filter(fn(a) { a != attribute.none() }),
children,
)
}
/// render_config creates a Lustre Element from a FormField Config
///
pub fn render_config(
c: Config,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
render(from_config(c), attributes, children)
}
/// slot returns a Lustre Attribute(msg) for the given slot name
///
pub fn slot(s: Slot) -> Attribute(msg) {
case s {
Prefix -> attribute.attribute("slot", "prefix")
PrefixText -> attribute.attribute("slot", "prefix-text")
Suffix -> attribute.attribute("slot", "suffix")
SuffixText -> attribute.attribute("slot", "suffix-text")
Hint -> attribute.attribute("slot", "hint")
Error -> attribute.attribute("slot", "error")
}
}