use serde_json::{json, Value};
use super::{Component, Role};
#[derive(Clone, Debug)]
pub struct Text {
id: String,
label: String,
value: String,
placeholder: Option<String>,
lines: usize,
interactive: bool,
}
impl Text {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
placeholder: None,
lines: 1,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn placeholder(mut self, p: impl Into<String>) -> Self {
self.placeholder = Some(p.into());
self
}
pub fn lines(mut self, n: usize) -> Self {
self.lines = n.max(1);
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Text {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"text"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"placeholder": self.placeholder,
"lines": self.lines,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct Slider {
id: String,
label: String,
min: f64,
max: f64,
step: f64,
value: f64,
interactive: bool,
}
impl Slider {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
min: 0.0,
max: 100.0,
step: 1.0,
value: 0.0,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn min(mut self, v: f64) -> Self {
self.min = v;
self
}
pub fn max(mut self, v: f64) -> Self {
self.max = v;
self
}
pub fn step(mut self, v: f64) -> Self {
self.step = v;
self
}
pub fn value(mut self, v: f64) -> Self {
self.value = v;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Slider {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"slider"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "min": self.min, "max": self.max, "step": self.step, "value": self.value, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct Button {
id: String,
label: String,
variant: String,
primary: bool,
}
impl Button {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
variant: "primary".to_string(),
primary: false,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn secondary(mut self) -> Self {
self.variant = "secondary".to_string();
self
}
pub fn variant(mut self, v: impl Into<String>) -> Self {
self.variant = v.into();
self
}
pub fn primary(mut self) -> Self {
self.primary = true;
self
}
}
impl Component for Button {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"button"
}
fn props(&self) -> Value {
json!({ "label": self.label, "variant": self.variant, "primary": self.primary })
}
}
#[derive(Clone, Debug)]
pub struct Checkbox {
id: String,
label: String,
value: bool,
interactive: bool,
}
impl Checkbox {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: false,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: bool) -> Self {
self.value = v;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Checkbox {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"checkbox"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct Dropdown {
id: String,
label: String,
choices: Vec<(String, String)>,
value: Value,
multiple: bool,
allow_custom: bool,
interactive: bool,
}
impl Dropdown {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
choices: Vec::new(),
value: Value::Null,
multiple: false,
allow_custom: false,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn choices(mut self, items: &[(&str, &str)]) -> Self {
self.choices = items
.iter()
.map(|(l, v)| (l.to_string(), v.to_string()))
.collect();
self
}
pub fn choices_str(mut self, items: &[&str]) -> Self {
self.choices = items
.iter()
.map(|s| (s.to_string(), s.to_string()))
.collect();
self
}
pub fn options(self, items: &[&str]) -> Self {
self.choices_str(items)
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = json!(v.into());
self
}
pub fn value_list(mut self, v: &[&str]) -> Self {
self.value = json!(v);
self
}
pub fn multiple(mut self, on: bool) -> Self {
self.multiple = on;
self
}
pub fn allow_custom(mut self, on: bool) -> Self {
self.allow_custom = on;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Dropdown {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"dropdown"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
let choices: Vec<Value> = self
.choices
.iter()
.map(|(l, v)| json!({ "label": l, "value": v }))
.collect();
json!({
"label": self.label, "choices": choices, "value": self.value,
"multiple": self.multiple, "allow_custom": self.allow_custom, "interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct DatePicker {
id: String,
label: String,
value: String,
min: Option<String>,
max: Option<String>,
interactive: bool,
}
impl DatePicker {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
min: None,
max: None,
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn min(mut self, m: impl Into<String>) -> Self {
self.min = Some(m.into());
self
}
pub fn max(mut self, m: impl Into<String>) -> Self {
self.max = Some(m.into());
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for DatePicker {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"date"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "min": self.min, "max": self.max, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct TimePicker {
id: String,
label: String,
value: String,
interactive: bool,
}
impl TimePicker {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for TimePicker {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"time"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({ "label": self.label, "value": self.value, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct SortableList {
id: String,
label: String,
items: Vec<(String, String)>,
value: Vec<String>,
interactive: bool,
}
impl SortableList {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
items: Vec::new(),
value: Vec::new(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn items(mut self, list: &[(&str, &str)]) -> Self {
self.items = list
.iter()
.map(|(v, l)| (v.to_string(), l.to_string()))
.collect();
self.value = list.iter().map(|(v, _)| v.to_string()).collect();
self
}
pub fn items_str(mut self, list: &[&str]) -> Self {
self.items = list
.iter()
.map(|s| (s.to_string(), s.to_string()))
.collect();
self.value = list.iter().map(|s| s.to_string()).collect();
self
}
pub fn add(mut self, value: impl Into<String>, label: impl Into<String>) -> Self {
self.items.push((value.into(), label.into()));
self.value = self.items.iter().map(|(v, _)| v.clone()).collect();
self
}
pub fn value(mut self, order: &[&str]) -> Self {
self.value = order.iter().map(|s| s.to_string()).collect();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for SortableList {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"list"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
let items: Vec<Value> = self
.items
.iter()
.map(|(v, l)| json!({ "value": v, "label": l }))
.collect();
json!({ "label": self.label, "items": items, "value": self.value, "interactive": self.interactive })
}
}
#[derive(Clone, Debug)]
pub struct Number {
id: String,
label: String,
value: f64,
min: f64,
max: f64,
step: f64,
unit: String,
interactive: bool,
}
impl Number {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: 0.0,
min: 0.0,
max: 1.0e6,
step: 1.0,
unit: String::new(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: f64) -> Self {
self.value = v;
self
}
pub fn min(mut self, v: f64) -> Self {
self.min = v;
self
}
pub fn max(mut self, v: f64) -> Self {
self.max = v;
self
}
pub fn step(mut self, v: f64) -> Self {
self.step = v;
self
}
pub fn unit(mut self, u: impl Into<String>) -> Self {
self.unit = u.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Number {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"number"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label, "value": self.value, "min": self.min,
"max": self.max, "step": self.step, "unit": self.unit,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct Radio {
id: String,
label: String,
choices: Vec<String>,
value: String,
direction: String,
style: String,
interactive: bool,
}
impl Radio {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
choices: Vec::new(),
value: String::new(),
direction: "horizontal".into(),
style: "pills".into(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn choices(mut self, c: &[&str]) -> Self {
self.choices = c.iter().map(|s| s.to_string()).collect();
if self.value.is_empty() && !self.choices.is_empty() {
self.value = self.choices[0].clone();
}
self
}
pub fn choices_str(mut self, c: Vec<String>) -> Self {
if self.value.is_empty() && !c.is_empty() {
self.value = c[0].clone();
}
self.choices = c;
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn direction(mut self, d: impl Into<String>) -> Self {
self.direction = d.into();
self
}
pub fn style(mut self, s: impl Into<String>) -> Self {
self.style = s.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for Radio {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"radio"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"choices": self.choices,
"value": self.value,
"direction": self.direction,
"style": self.style,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct SliderRange {
id: String,
label: String,
min: f64,
max: f64,
step: f64,
value: (f64, f64),
unit: String,
interactive: bool,
}
impl SliderRange {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
min: 0.0,
max: 1.0,
step: 0.05,
value: (0.2, 0.8),
unit: String::new(),
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn min(mut self, m: f64) -> Self {
self.min = m;
self
}
pub fn max(mut self, m: f64) -> Self {
self.max = m;
self
}
pub fn step(mut self, s: f64) -> Self {
self.step = s;
self
}
pub fn value(mut self, low: f64, high: f64) -> Self {
self.value = (low, high);
self
}
pub fn unit(mut self, u: impl Into<String>) -> Self {
self.unit = u.into();
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for SliderRange {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"sliderrange"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"min": self.min,
"max": self.max,
"step": self.step,
"value": [self.value.0, self.value.1],
"unit": self.unit,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct ColorPicker {
id: String,
label: String,
value: String,
presets: Vec<String>,
interactive: bool,
}
impl ColorPicker {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: "#6366f1".into(),
presets: vec![
"#6366f1".into(),
"#8b5cf6".into(),
"#ec4899".into(),
"#ef4444".into(),
"#f59e0b".into(),
"#10b981".into(),
"#06b6d4".into(),
"#1e293b".into(),
"#ffffff".into(),
],
interactive: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, hex: impl Into<String>) -> Self {
self.value = hex.into();
self
}
pub fn presets(mut self, p: &[&str]) -> Self {
self.presets = p.iter().map(|s| s.to_string()).collect();
self
}
pub fn presets_str(mut self, p: Vec<String>) -> Self {
self.presets = p;
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
}
impl Component for ColorPicker {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"colorpicker"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"presets": self.presets,
"interactive": self.interactive
})
}
}
#[derive(Clone, Debug)]
pub struct RichText {
id: String,
label: String,
value: String,
placeholder: String,
lines: u32,
interactive: bool,
show_preview: bool,
}
impl RichText {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
value: String::new(),
placeholder: String::new(),
lines: 6,
interactive: true,
show_preview: true,
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
pub fn placeholder(mut self, ph: impl Into<String>) -> Self {
self.placeholder = ph.into();
self
}
pub fn lines(mut self, n: u32) -> Self {
self.lines = n.max(2);
self
}
pub fn interactive(mut self, on: bool) -> Self {
self.interactive = on;
self
}
pub fn show_preview(mut self, on: bool) -> Self {
self.show_preview = on;
self
}
}
impl Component for RichText {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"richtext"
}
fn role(&self) -> Role {
Role::Input
}
fn props(&self) -> Value {
json!({
"label": self.label,
"value": self.value,
"placeholder": self.placeholder,
"lines": self.lines,
"interactive": self.interactive,
"show_preview": self.show_preview,
})
}
}