Current section

Files

Jump to
m3e src m3e step.gleam
Raw

src/m3e/step.gleam

//// Step is a step in a wizard-like workflow.
////
//// This file was generated by m3e/generator
////
//// DO NOT EDIT
////
import gleam/function
import gleam/list
import gleam/option.{type Option, None}
import lustre/attribute.{type Attribute}
import lustre/element.{type Element}
import m3e/attr
// --- Types ---
/// Step is a View Model for this component
///
/// ## Fields:
///
/// - completed: Whether the step has been completed.
/// - disabled: Whether the element is disabled.
/// - editable: Whether the step is editable and users can return to it after completion.
/// - for: The identifier of the interactive control to which this element is attached.
/// - optional: Whether the step is optional.
/// - selected: Whether the element is selected.
/// - invalid: Whether the step has an error.
///
pub opaque type Step {
Step(
completed: Completed,
disabled: Disabled,
editable: Editable,
for: Option(String),
optional: Optional,
selected: Selected,
invalid: Invalid,
)
}
/// Completed is whether the step has been completed.
///
pub type Completed {
IsCompleted
IsNotCompleted
}
/// Disabled is whether the element is disabled.
///
pub type Disabled {
IsDisabled
IsNotDisabled
}
/// Editable is whether the step is editable and users can return to it after completion.
///
pub type Editable {
IsEditable
IsNotEditable
}
/// Optional is whether the step is optional.
///
pub type Optional {
IsOptional
IsNotOptional
}
/// Selected is whether the element is selected.
///
pub type Selected {
IsSelected
IsNotSelected
}
/// Invalid is whether the step has an error.
///
pub type Invalid {
IsInvalid
IsNotInvalid
}
// --- Defaults ---
pub const default_completed: Completed = IsNotCompleted
pub const default_disabled: Disabled = IsNotDisabled
pub const default_editable: Editable = IsNotEditable
pub const default_for: Option(String) = None
pub const default_optional: Optional = IsNotOptional
pub const default_selected: Selected = IsNotSelected
pub const default_invalid: Invalid = IsNotInvalid
/// Slots are used in child elements to insert content into this component
///
pub type Slot {
Icon
// Renders the icon of the step.
DoneIcon
// Renders the icon of a completed step.
EditIcon
// Renders the icon of a completed editable step.
ErrorIcon
// Renders icon of an invalid step.
Hint
// Renders the hint text of the step.
Error
// Renders the error message for an invalid step.
}
// --- Configuration ---
/// Config is a public record for configuring this component.
///
pub type Config {
Config(
completed: Completed,
disabled: Disabled,
editable: Editable,
for: Option(String),
optional: Optional,
selected: Selected,
invalid: Invalid,
)
}
/// default_config is the default configuration for this component.
///
pub fn default_config() -> Config {
Config(
completed: IsNotCompleted,
disabled: IsNotDisabled,
editable: IsNotEditable,
for: None,
optional: IsNotOptional,
selected: IsNotSelected,
invalid: IsNotInvalid,
)
}
// --- Constructors ---
/// from_config creates a new Step from the given configuration.
///
pub fn from_config(config: Config) -> Step {
Step(
completed: config.completed,
disabled: config.disabled,
editable: config.editable,
for: config.for,
optional: config.optional,
selected: config.selected,
invalid: config.invalid,
)
}
/// new creates a new Step with the default configuration.
///
pub fn new() -> Step {
from_config(default_config())
}
// --- Setters ---
/// completed sets the value of completed for this Step.
///
pub fn completed(record: Step, completed: Completed) -> Step {
Step(..record, completed: completed)
}
/// disabled sets the value of disabled for this Step.
///
pub fn disabled(record: Step, disabled: Disabled) -> Step {
Step(..record, disabled: disabled)
}
/// editable sets the value of editable for this Step.
///
pub fn editable(record: Step, editable: Editable) -> Step {
Step(..record, editable: editable)
}
/// for sets the value of for for this Step.
///
pub fn for(record: Step, for: Option(String)) -> Step {
Step(..record, for: for)
}
/// optional sets the value of optional for this Step.
///
pub fn optional(record: Step, optional: Optional) -> Step {
Step(..record, optional: optional)
}
/// selected sets the value of selected for this Step.
///
pub fn selected(record: Step, selected: Selected) -> Step {
Step(..record, selected: selected)
}
/// invalid sets the value of invalid for this Step.
///
pub fn invalid(record: Step, invalid: Invalid) -> Step {
Step(..record, invalid: invalid)
}
// --- Renderers ---
/// render creates a Lustre Element for a Step
///
pub fn render(
model: Step,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
element.element(
"m3e-step",
list.flatten([
[
attr.boolean("completed", model.completed == IsCompleted),
attr.boolean("disabled", model.disabled == IsDisabled),
attr.boolean("editable", model.editable == IsEditable),
attr.option(model.for, fn(_) { "for" }, function.identity, default_for),
attr.boolean("optional", model.optional == IsOptional),
attr.boolean("selected", model.selected == IsSelected),
attr.boolean("invalid", model.invalid == IsInvalid),
],
attributes,
])
|> list.filter(fn(a) { a != attribute.none() }),
children,
)
}
/// render_config creates a Lustre Element from a Step 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 {
Icon -> attribute.attribute("slot", "icon")
DoneIcon -> attribute.attribute("slot", "done-icon")
EditIcon -> attribute.attribute("slot", "edit-icon")
ErrorIcon -> attribute.attribute("slot", "error-icon")
Hint -> attribute.attribute("slot", "hint")
Error -> attribute.attribute("slot", "error")
}
}