Current section
Files
Jump to
Current section
Files
src/m3e/input_chip.gleam
//// InputChip is a chip which represents a discrete piece of information entered by a user.
////
//// 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/chip_variant.{type ChipVariant}
// --- Types ---
/// InputChip is a View Model for this component
///
/// ## Fields:
///
/// - disabled: Whether the element is disabled.
/// - disabled_interactive: Whether the element is disabled and interactive.
/// - removable: Whether the chip is removable.
/// - remove_label: The accessible label given to the button used to remove the chip.
/// - value: A string representing the value of the chip.
/// - variant: The appearance variant of the chip.
///
pub opaque type InputChip {
InputChip(
disabled: Disabled,
disabled_interactive: DisabledInteractive,
removable: Removable,
remove_label: String,
value: String,
variant: ChipVariant,
)
}
/// Disabled is whether the element is disabled.
///
pub type Disabled {
IsDisabled
IsNotDisabled
}
/// DisabledInteractive is whether the element is disabled and interactive.
///
pub type DisabledInteractive {
IsDisabledInteractive
IsNotDisabledInteractive
}
/// Removable is whether the chip is removable.
///
pub type Removable {
IsRemovable
IsNotRemovable
}
// --- Defaults ---
pub const default_disabled: Disabled = IsNotDisabled
pub const default_disabled_interactive: DisabledInteractive = IsNotDisabledInteractive
pub const default_removable: Removable = IsNotRemovable
pub const default_remove_label: String = "Remove"
pub const default_value: String = ""
pub const default_variant: ChipVariant = chip_variant.Outlined
/// Slots are used in child elements to insert content into this component
///
pub type Slot {
Avatar
// Renders an avatar before the chip's label.
Icon
// Renders an icon before the chip's label.
RemoveIcon
// Renders the icon for the button used to remove the chip.
TrailingIcon
// Renders an icon after the chip's label.
}
// --- Configuration ---
/// Config is a public record for configuring this component.
///
pub type Config {
Config(
disabled: Disabled,
disabled_interactive: DisabledInteractive,
removable: Removable,
remove_label: String,
value: String,
variant: ChipVariant,
)
}
/// default_config is the default configuration for this component.
///
pub fn default_config() -> Config {
Config(
disabled: IsNotDisabled,
disabled_interactive: IsNotDisabledInteractive,
removable: IsNotRemovable,
remove_label: "Remove",
value: "",
variant: chip_variant.Outlined,
)
}
// --- Constructors ---
/// from_config creates a new InputChip from the given configuration.
///
pub fn from_config(config: Config) -> InputChip {
InputChip(
disabled: config.disabled,
disabled_interactive: config.disabled_interactive,
removable: config.removable,
remove_label: config.remove_label,
value: config.value,
variant: config.variant,
)
}
/// new creates a new InputChip with the default configuration.
///
pub fn new() -> InputChip {
from_config(default_config())
}
// --- Setters ---
/// disabled sets the value of disabled for this InputChip.
///
pub fn disabled(record: InputChip, disabled: Disabled) -> InputChip {
InputChip(..record, disabled: disabled)
}
/// disabled_interactive sets the value of disabled_interactive for this InputChip.
///
pub fn disabled_interactive(
record: InputChip,
disabled_interactive: DisabledInteractive,
) -> InputChip {
InputChip(..record, disabled_interactive: disabled_interactive)
}
/// removable sets the value of removable for this InputChip.
///
pub fn removable(record: InputChip, removable: Removable) -> InputChip {
InputChip(..record, removable: removable)
}
/// remove_label sets the value of remove_label for this InputChip.
///
pub fn remove_label(record: InputChip, remove_label: String) -> InputChip {
InputChip(..record, remove_label: remove_label)
}
/// value sets the value of value for this InputChip.
///
pub fn value(record: InputChip, value: String) -> InputChip {
InputChip(..record, value: value)
}
/// variant sets the value of variant for this InputChip.
///
pub fn variant(record: InputChip, variant: ChipVariant) -> InputChip {
InputChip(..record, variant: variant)
}
// --- Renderers ---
/// render creates a Lustre Element for a InputChip
///
pub fn render(
model: InputChip,
attributes: List(Attribute(msg)),
children: List(Element(msg)),
) -> Element(msg) {
element.element(
"m3e-input-chip",
list.flatten([
[
attr.boolean("disabled", model.disabled == IsDisabled),
attr.boolean(
"disabled-interactive",
model.disabled_interactive == IsDisabledInteractive,
),
attr.boolean("removable", model.removable == IsRemovable),
attr.with_default(
"remove-label",
model.remove_label,
default_remove_label,
),
attr.with_default("value", model.value, default_value),
attr.with_default(
"variant",
chip_variant.to_string(model.variant),
chip_variant.to_string(default_variant),
),
],
attributes,
])
|> list.filter(fn(a) { a != attribute.none() }),
children,
)
}
/// render_config creates a Lustre Element from a InputChip 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 {
Avatar -> attribute.attribute("slot", "avatar")
Icon -> attribute.attribute("slot", "icon")
RemoveIcon -> attribute.attribute("slot", "remove-icon")
TrailingIcon -> attribute.attribute("slot", "trailing-icon")
}
}