1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.web.elements.input;
7 
8 import diamond.web.elements.element;
9 
10 /// Wrapper around an input element.
11 abstract class Input : Element
12 {
13   private:
14   /// The value.
15   string _value;
16   /// The placeholder.
17   string _placeholder;
18   /// The label.
19   string _label;
20 
21   public:
22   final
23   {
24     /// Creates a new input element.
25     this()
26     {
27       super("input");
28     }
29 
30     package(diamond.web.elements) this(string tagName)
31     {
32       super(tagName);
33     }
34 
35     @property
36     {
37       /// Gets the value.
38       string value() { return _value; }
39 
40       /// Sets the value.
41       void value(string newValue)
42       {
43         _value = newValue;
44 
45         addAttribute("value", _value);
46       }
47 
48       /// Gets the placeholder.
49       string placeholder() { return _placeholder; }
50 
51       /// Sets the placeholder.
52       void placeholder(string newPlaceholder)
53       {
54         _placeholder = newPlaceholder;
55 
56         addAttribute("placeholder", _placeholder);
57       }
58 
59       /// Gets the label.
60       string label() { return _label; }
61 
62       /// Sets the label.
63       void label(string newLabel)
64       {
65         _label = newLabel;
66       }
67     }
68   }
69 
70   protected:
71   /// Generates the appropriate html for the element.
72   override string generateHtml()
73   {
74     import std..string : format;
75 
76     return "%3$s<%1$s %2$s>".format
77     (
78       super.tagName, super.attributeHtml(),
79       _label && _label.length && super.id && super.id.length ?
80       "<label for=\"%s\">%s</label>".format(super.id, _label) : ""
81     );
82   }
83 }