blinc_cn 0.5.0

Blinc Component Library - shadcn-style themed components built on blinc_layout primitives
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Textarea component - styled multi-line text input following shadcn/ui patterns
//!
//! A themed wrapper around `blinc_layout::TextArea` with consistent styling,
//! size variants, and optional label/error message support.
//!
//! # Example
//!
//! ```ignore
//! use blinc_cn::prelude::*;
//!
//! // Basic textarea
//! let bio = text_area_state();
//! cn::textarea(&bio)
//!     .placeholder("Enter your bio")
//!
//! // With rows/cols sizing (like HTML textarea)
//! cn::textarea(&content)
//!     .rows(5)
//!     .cols(40)
//!
//! // With label
//! cn::textarea(&description)
//!     .label("Description")
//!     .placeholder("Enter description...")
//!
//! // With error message
//! cn::textarea(&message)
//!     .label("Message")
//!     .error("Message is required")
//!
//! // Disabled state
//! cn::textarea(&notes)
//!     .disabled(true)
//! ```

use super::label::{label, LabelSize};
use blinc_layout::prelude::*;
use blinc_layout::widgets::text_area::{
    text_area, SharedTextAreaState, TextArea as LayoutTextArea,
};
use blinc_theme::{ColorToken, RadiusToken, SpacingToken, ThemeState, TypographyTokens};

/// Textarea size variants (affects default dimensions)
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextareaSize {
    /// Small textarea (3 rows, smaller font)
    Small,
    /// Medium/default textarea (4 rows)
    #[default]
    Medium,
    /// Large textarea (6 rows, larger font)
    Large,
}

impl TextareaSize {
    fn default_rows(&self) -> usize {
        match self {
            TextareaSize::Small => 3,
            TextareaSize::Medium => 4,
            TextareaSize::Large => 6,
        }
    }

    fn font_size(&self, typography: &TypographyTokens) -> f32 {
        match self {
            TextareaSize::Small => typography.text_xs,   // 12px
            TextareaSize::Medium => typography.text_sm,  // 14px
            TextareaSize::Large => typography.text_base, // 16px
        }
    }
}

/// Configuration for building a Textarea
#[derive(Clone)]
struct TextareaConfig {
    state: SharedTextAreaState,
    size: TextareaSize,
    rows: Option<usize>,
    cols: Option<usize>,
    label: Option<String>,
    description: Option<String>,
    error: Option<String>,
    disabled: bool,
    required: bool,
    placeholder: Option<String>,
    max_length: Option<usize>,
    wrap: bool,
    width: Option<f32>,
    height: Option<f32>,
    full_width: bool,
    border_width: Option<f32>,
    corner_radius: Option<f32>,
}

impl TextareaConfig {
    fn new(state: SharedTextAreaState) -> Self {
        Self {
            state,
            size: TextareaSize::default(),
            rows: None,
            cols: None,
            label: None,
            description: None,
            error: None,
            disabled: false,
            required: false,
            placeholder: None,
            max_length: None,
            wrap: true,
            width: None,
            height: None,
            full_width: true,
            border_width: None,
            corner_radius: None,
        }
    }
}

/// Styled Textarea component (final built element)
pub struct Textarea {
    /// Inner element containing the complete structure
    inner: Div,
}

impl Textarea {
    /// Build from config
    fn from_config(config: TextareaConfig) -> Self {
        let theme = ThemeState::get();
        let typography = theme.typography();

        // Build the layout TextArea
        let radius = config
            .corner_radius
            .unwrap_or_else(|| theme.radius(RadiusToken::Md));

        let mut ta = text_area(&config.state)
            .font_size(config.size.font_size(&typography))
            .rounded(radius)
            .disabled(config.disabled)
            .wrap(config.wrap);

        // Apply border width if specified
        if let Some(border) = config.border_width {
            ta = ta.border_width(border);
        }

        // Apply rows/cols or use size preset
        if let Some(rows) = config.rows {
            ta = ta.rows(rows);
        } else {
            ta = ta.rows(config.size.default_rows());
        }

        if let Some(cols) = config.cols {
            ta = ta.cols(cols);
        }

        // Apply explicit dimensions if provided
        if let Some(w) = config.width {
            ta = ta.w(w);
        } else if config.full_width {
            ta = ta.w_full();
        }

        if let Some(h) = config.height {
            ta = ta.h(h);
        }

        // Apply placeholder
        if let Some(ref placeholder) = config.placeholder {
            ta = ta.placeholder(placeholder.clone());
        }

        // Apply max length
        if let Some(max) = config.max_length {
            ta = ta.max_length(max);
        }

        // Add CSS class directly to the TextArea widget (it supports .class())
        let ta = ta.class("cn-textarea");

        // If no label, description, or error, wrap in a minimal container
        let inner =
            if config.label.is_none() && config.description.is_none() && config.error.is_none() {
                div().child(ta)
            } else {
                // Build a container with label, textarea, and description/error
                let spacing = theme.spacing_value(SpacingToken::Space2);
                let mut container = div().flex_col().gap_px(spacing).h_fit();

                // Apply width to container
                if config.full_width {
                    container = container.w_full();
                } else if let Some(w) = config.width {
                    container = container.w(w);
                }

                // Label
                if let Some(ref label_text) = config.label {
                    let mut lbl = label(label_text).size(LabelSize::Medium);
                    if config.required {
                        lbl = lbl.required();
                    }
                    if config.disabled {
                        lbl = lbl.disabled(true);
                    }
                    container = container.child(lbl);
                }

                // Textarea (added directly — TextArea has the cn-textarea class)
                container = container.child(ta);

                // Error or description
                if let Some(ref error_text) = config.error {
                    let error_color = theme.color(ColorToken::Error);
                    container = container
                        .child(text(error_text).size(typography.text_xs).color(error_color));
                } else if let Some(ref desc_text) = config.description {
                    let desc_color = theme.color(ColorToken::TextTertiary);
                    container =
                        container.child(text(desc_text).size(typography.text_xs).color(desc_color));
                }

                container
            };

        Self { inner }
    }

    /// Add a CSS class for selector matching
    pub fn class(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.class(name);
        self
    }

    /// Set the element ID for CSS selector matching
    pub fn id(mut self, id: &str) -> Self {
        self.inner = self.inner.id(id);
        self
    }
}

impl ElementBuilder for Textarea {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn element_type_id(&self) -> blinc_layout::div::ElementTypeId {
        self.inner.element_type_id()
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        self.inner.layout_style()
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }
}

/// Builder for creating Textarea components with fluent API
pub struct TextareaBuilder {
    config: TextareaConfig,
    /// Cached built Textarea - built lazily on first access
    built: std::cell::OnceCell<Textarea>,
}

impl TextareaBuilder {
    /// Create a new textarea builder with the given state
    pub fn new(state: &SharedTextAreaState) -> Self {
        Self {
            config: TextareaConfig::new(state.clone()),
            built: std::cell::OnceCell::new(),
        }
    }

    /// Get or build the inner Textarea
    fn get_or_build(&self) -> &Textarea {
        self.built
            .get_or_init(|| Textarea::from_config(self.config.clone()))
    }

    /// Set the textarea size preset
    pub fn size(mut self, size: TextareaSize) -> Self {
        self.config.size = size;
        self
    }

    /// Set number of visible rows (like HTML textarea)
    pub fn rows(mut self, rows: usize) -> Self {
        self.config.rows = Some(rows);
        self
    }

    /// Set number of visible columns (like HTML textarea)
    pub fn cols(mut self, cols: usize) -> Self {
        self.config.cols = Some(cols);
        self
    }

    /// Set a label above the textarea
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.config.label = Some(label.into());
        self
    }

    /// Set a description/helper text below the textarea
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.config.description = Some(description.into());
        self
    }

    /// Set an error message (shows in red, replaces description)
    pub fn error(mut self, error: impl Into<String>) -> Self {
        self.config.error = Some(error.into());
        self
    }

    /// Set placeholder text
    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.config.placeholder = Some(placeholder.into());
        self
    }

    /// Set maximum character length
    pub fn max_length(mut self, max: usize) -> Self {
        self.config.max_length = Some(max);
        self
    }

    /// Enable or disable text wrapping (default: true)
    pub fn wrap(mut self, wrap: bool) -> Self {
        self.config.wrap = wrap;
        self
    }

    /// Disable text wrapping
    pub fn no_wrap(mut self) -> Self {
        self.config.wrap = false;
        self
    }

    /// Disable the textarea
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.config.disabled = disabled;
        self
    }

    /// Mark the textarea as required (shows asterisk on label)
    pub fn required(mut self) -> Self {
        self.config.required = true;
        self
    }

    /// Set a fixed width
    pub fn w(mut self, width: f32) -> Self {
        self.config.width = Some(width);
        self.config.full_width = false;
        self
    }

    /// Set a fixed height
    pub fn h(mut self, height: f32) -> Self {
        self.config.height = Some(height);
        self
    }

    /// Make the textarea fill its parent width (default)
    pub fn w_full(mut self) -> Self {
        self.config.full_width = true;
        self.config.width = None;
        self
    }

    /// Set the border width
    pub fn border_width(mut self, width: f32) -> Self {
        self.config.border_width = Some(width);
        self
    }

    /// Set the corner radius
    pub fn rounded(mut self, radius: f32) -> Self {
        self.config.corner_radius = Some(radius);
        self
    }

    /// Build the final Textarea component
    pub fn build_component(self) -> Textarea {
        Textarea::from_config(self.config)
    }
}

impl ElementBuilder for TextareaBuilder {
    fn build(&self, tree: &mut blinc_layout::tree::LayoutTree) -> blinc_layout::tree::LayoutNodeId {
        self.get_or_build().build(tree)
    }

    fn render_props(&self) -> blinc_layout::element::RenderProps {
        self.get_or_build().render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.get_or_build().children_builders()
    }

    fn element_type_id(&self) -> blinc_layout::div::ElementTypeId {
        self.get_or_build().element_type_id()
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        self.get_or_build().layout_style()
    }

    fn element_classes(&self) -> &[String] {
        self.get_or_build().element_classes()
    }
}

/// Create a styled textarea component
///
/// # Example
///
/// ```ignore
/// use blinc_cn::prelude::*;
///
/// let bio = text_area_state();
/// cn::textarea(&bio)
///     .label("Bio")
///     .placeholder("Tell us about yourself...")
///     .rows(5)
/// ```
pub fn textarea(state: &SharedTextAreaState) -> TextareaBuilder {
    TextareaBuilder::new(state)
}

#[cfg(test)]
mod tests {
    use super::*;
    use blinc_theme::TypographyTokens;

    fn init_theme() {
        let _ = ThemeState::try_get().unwrap_or_else(|| {
            ThemeState::init_default();
            ThemeState::get()
        });
    }

    #[test]
    fn test_textarea_size_values() {
        let typography = TypographyTokens::default();

        // Default rows for each size
        assert_eq!(TextareaSize::Small.default_rows(), 3);
        assert_eq!(TextareaSize::Medium.default_rows(), 4);
        assert_eq!(TextareaSize::Large.default_rows(), 6);

        // Font sizes
        assert_eq!(
            TextareaSize::Small.font_size(&typography),
            typography.text_xs
        );
        assert_eq!(
            TextareaSize::Medium.font_size(&typography),
            typography.text_sm
        );
        assert_eq!(
            TextareaSize::Large.font_size(&typography),
            typography.text_base
        );
    }

    #[test]
    fn test_textarea_builder() {
        init_theme();
        let state = blinc_layout::widgets::text_area::text_area_state();
        let ta = TextareaBuilder::new(&state)
            .label("Description")
            .placeholder("Enter description...")
            .rows(5)
            .size(TextareaSize::Large);

        assert_eq!(ta.config.size, TextareaSize::Large);
        assert_eq!(ta.config.label, Some("Description".to_string()));
        assert_eq!(ta.config.rows, Some(5));
    }

    #[test]
    fn test_textarea_wrap_settings() {
        init_theme();
        let state = blinc_layout::widgets::text_area::text_area_state();

        let ta = TextareaBuilder::new(&state);
        assert!(ta.config.wrap); // Default is wrap enabled

        let ta_no_wrap = TextareaBuilder::new(&state).no_wrap();
        assert!(!ta_no_wrap.config.wrap);
    }
}