orbit/kit/components/
button.rs1use crate::component::{Component, ComponentError, ComponentId, Context, Node};
4use std::any::Any;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum ButtonSize {
9 Small,
10 Medium,
11 Large,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ButtonVariant {
17 Primary,
18 Secondary,
19 Outline,
20 Ghost,
21 Link,
22}
23
24#[derive(Debug)]
46pub struct Button {
47 id: ComponentId,
49 pub text: String,
51 pub variant: ButtonVariant,
53 pub disabled: bool,
55 pub size: ButtonSize,
57 pub on_click: Option<fn()>,
59}
60
61#[derive(Debug, Clone)]
63pub struct ButtonProps {
64 pub text: String,
66 pub variant: Option<ButtonVariant>,
68 pub disabled: Option<bool>,
70 pub size: Option<ButtonSize>,
72 pub on_click: Option<fn()>,
74}
75
76impl Default for Button {
77 fn default() -> Self {
78 Self {
79 id: ComponentId::new(),
80 text: String::new(),
81 variant: ButtonVariant::Primary,
82 disabled: false,
83 size: ButtonSize::Medium,
84 on_click: None,
85 }
86 }
87}
88
89impl Component for Button {
90 type Props = ButtonProps;
91
92 fn component_id(&self) -> ComponentId {
93 self.id
94 }
95
96 fn create(props: Self::Props, _context: Context) -> Self {
97 Self {
98 id: ComponentId::new(),
99 text: props.text,
100 variant: props.variant.unwrap_or(ButtonVariant::Primary),
101 disabled: props.disabled.unwrap_or(false),
102 size: props.size.unwrap_or(ButtonSize::Medium),
103 on_click: props.on_click,
104 }
105 }
106
107 fn update(&mut self, props: Self::Props) -> Result<(), ComponentError> {
108 self.text = props.text;
109 self.variant = props.variant.unwrap_or(self.variant);
110 self.disabled = props.disabled.unwrap_or(self.disabled);
111 self.size = props.size.unwrap_or(self.size);
112 self.on_click = props.on_click;
113 Ok(())
114 }
115
116 fn as_any(&self) -> &dyn Any {
117 self
118 }
119
120 fn as_any_mut(&mut self) -> &mut dyn Any {
121 self
122 }
123
124 fn render(&self) -> Result<Vec<Node>, ComponentError> {
125 Ok(vec![])
128 }
129}