@htmltools/hdom 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/hdom.d.ts +56 -8
  2. package/lib/hdom.js +109 -13
  3. package/package.json +1 -1
package/lib/hdom.d.ts CHANGED
@@ -11,6 +11,10 @@ export class hdom {
11
11
  * @returns {Element}
12
12
  */
13
13
  static addEventListener(e: string | Element, type: string, fn: (arg0: Event) => void): Element;
14
+ /**
15
+ * @param {string|HTMLElement} e
16
+ */
17
+ static clickElement(e: string | HTMLElement): void;
14
18
  /**
15
19
  * @param {string|HTMLDialogElement} e
16
20
  */
@@ -23,28 +27,67 @@ export class hdom {
23
27
  static createCheckBox(id: string, checked: boolean): HTMLInputElement;
24
28
  /**
25
29
  * @param {string} name
26
- * @param {Object<string,string|number>|string} [attributes]
30
+ * @param {Object<string,string|number|undefined>|string} [attributes]
31
+ * @param {Element} [parent]
27
32
  * @returns {HTMLElement}
28
33
  */
29
34
  static createElement(name: string, attributes?: {
30
- [x: string]: string | number;
31
- } | string): HTMLElement;
35
+ [x: string]: string | number | undefined;
36
+ } | string, parent?: Element): HTMLElement;
32
37
  /**
33
- * @param {Object.<string,string|number>|string} attributes
38
+ * @param {Object.<string,string|number|undefined>|string} attributes
34
39
  * @returns {HTMLInputElement}
35
40
  */
36
41
  static createInputElement(attributes: {
37
- [x: string]: string | number;
42
+ [x: string]: string | number | undefined;
38
43
  } | string): HTMLInputElement;
44
+ /**
45
+ * @param {string} forId
46
+ * @param {string} text
47
+ * @returns {HTMLElement}
48
+ */
49
+ static createLabelElement(forId: string, text: string): HTMLElement;
50
+ /**
51
+ * @param {URL|string|undefined} url
52
+ * @param {Node|string|number} eLinkText
53
+ * @param {Object.<string,string>|string} [attributes]
54
+ * @returns {HTMLElement}
55
+ */
56
+ static createLinkElement(url: URL | string | undefined, eLinkText: Node | string | number, attributes?: {
57
+ [x: string]: string;
58
+ } | string): HTMLElement;
59
+ /**
60
+ * @param {string} id
61
+ * @param {{value?:string,label?:string}[]} options
62
+ * @returns {HTMLElement}
63
+ */
64
+ static createSelectElement(id: string, options: {
65
+ value?: string;
66
+ label?: string;
67
+ }[]): HTMLElement;
68
+ /**
69
+ * @param {string} id
70
+ * @param {string} label
71
+ * @param {{value?:string,label?:string}[]} options
72
+ * @returns {{label:HTMLElement,select:HTMLElement}}
73
+ */
74
+ static createSelectElementWithLabel(id: string, label: string, options: {
75
+ value?: string;
76
+ label?: string;
77
+ }[]): {
78
+ label: HTMLElement;
79
+ select: HTMLElement;
80
+ };
39
81
  /**
40
82
  * @param {string} name
41
- * @param {Object<string,string>} attributes
83
+ * @param {Object<string,string>|string} attributes
42
84
  * @param {string|number} text
43
- * @returns {Element}
85
+ * @param {Element} [parent]
86
+ * @returns {HTMLElement}
44
87
  */
45
88
  static createTextElement(name: string, attributes: {
46
89
  [x: string]: string;
47
- }, text: string | number): Element;
90
+ } | string, text: string | number, parent?: Element): HTMLElement;
48
91
  /**
49
92
  * @param {string|HTMLElement} e
50
93
  * @param {boolean} [enable=true]
@@ -102,6 +145,11 @@ export class hdom {
102
145
  * @param {string} value
103
146
  */
104
147
  static setAttribute(e: string | HTMLElement, attName: string, value: string): void;
148
+ /**
149
+ * @param {string|HTMLElement} e
150
+ * @param {boolean} state
151
+ */
152
+ static setCheckBoxState(e: string | HTMLElement, state: boolean): void;
105
153
  /**
106
154
  * @param {string|Element} e
107
155
  * @param {string|number} text
package/lib/hdom.js CHANGED
@@ -22,6 +22,17 @@ export class hdom {
22
22
  return elem;
23
23
  }
24
24
 
25
+ /**
26
+ * @param {string|HTMLElement} e
27
+ */
28
+ static clickElement(e) {
29
+ const elem = this.getElement(e);
30
+ if (!(elem instanceof HTMLInputElement)) {
31
+ throw new Error();
32
+ }
33
+ elem.click();
34
+ }
35
+
25
36
  /**
26
37
  * @param {string|HTMLDialogElement} e
27
38
  */
@@ -50,10 +61,11 @@ export class hdom {
50
61
 
51
62
  /**
52
63
  * @param {string} name
53
- * @param {Object<string,string|number>|string} [attributes]
64
+ * @param {Object<string,string|number|undefined>|string} [attributes]
65
+ * @param {Element} [parent]
54
66
  * @returns {HTMLElement}
55
67
  */
56
- static createElement(name, attributes) {
68
+ static createElement(name, attributes, parent) {
57
69
  const e = document.createElement(name);
58
70
  switch (typeof attributes) {
59
71
  case "string":
@@ -62,33 +74,105 @@ export class hdom {
62
74
  break;
63
75
  case "object":
64
76
  for (const [k, v] of Object.entries(attributes)) {
65
- e.setAttribute(k, v.toString());
77
+ if (v !== undefined) {
78
+ e.setAttribute(k, v.toString());
79
+ }
66
80
  }
67
81
  break;
68
82
  }
83
+ if (parent) {
84
+ parent.appendChild(e);
85
+ }
69
86
  return e;
70
87
  }
71
88
 
72
89
  /**
73
- * @param {Object.<string,string|number>|string} attributes
90
+ * @param {Object.<string,string|number|undefined>|string} attributes
74
91
  * @returns {HTMLInputElement}
75
92
  */
76
93
  static createInputElement(attributes) {
77
- const e = this.createElement("input", attributes);
78
- if (!(e instanceof HTMLInputElement)) {
79
- throw new Error();
94
+ return /** @type {HTMLInputElement} */ (
95
+ this.createElement("input", attributes)
96
+ );
97
+ }
98
+
99
+ /**
100
+ * @param {string} forId
101
+ * @param {string} text
102
+ * @returns {HTMLElement}
103
+ */
104
+ static createLabelElement(forId, text) {
105
+ const el = this.createElement("label", { for: forId });
106
+ this.setTextValue(el, text);
107
+ return el;
108
+ }
109
+
110
+ /**
111
+ * @param {URL|string|undefined} url
112
+ * @param {Node|string|number} eLinkText
113
+ * @param {Object.<string,string>|string} [attributes]
114
+ * @returns {HTMLElement}
115
+ */
116
+ static createLinkElement(url, eLinkText, attributes) {
117
+ const eLink = this.createElement("a", attributes);
118
+ if (url !== undefined) {
119
+ eLink.setAttribute("href", url.toString());
80
120
  }
81
- return e;
121
+ eLink.appendChild(
122
+ eLinkText instanceof Node
123
+ ? eLinkText
124
+ : document.createTextNode(eLinkText.toString()),
125
+ );
126
+ return eLink;
127
+ }
128
+
129
+ /**
130
+ * @param {string} id
131
+ * @param {{value?:string,label?:string}[]} options
132
+ * @returns {HTMLElement}
133
+ */
134
+ static createSelectElement(id, options) {
135
+ const select = this.createElement("select", { id: id });
136
+ for (const option of options) {
137
+ const optionEl = this.createElement(
138
+ "option",
139
+ option.value
140
+ ? {
141
+ value: option.value,
142
+ }
143
+ : {},
144
+ );
145
+ if (option.label) {
146
+ this.setTextValue(optionEl, option.label);
147
+ }
148
+ select.appendChild(optionEl);
149
+ }
150
+ return select;
151
+ }
152
+
153
+ /**
154
+ * @param {string} id
155
+ * @param {string} label
156
+ * @param {{value?:string,label?:string}[]} options
157
+ * @returns {{label:HTMLElement,select:HTMLElement}}
158
+ */
159
+ static createSelectElementWithLabel(id, label, options) {
160
+ const labelEl = this.createLabelElement(id, "label");
161
+ return {
162
+ label: labelEl,
163
+ select: this.createSelectElement(id, options),
164
+ };
82
165
  }
83
166
 
84
167
  /**
85
168
  * @param {string} name
86
- * @param {Object<string,string>} attributes
169
+ * @param {Object<string,string>|string} attributes
87
170
  * @param {string|number} text
88
- * @returns {Element}
171
+ * @param {Element} [parent]
172
+ * @returns {HTMLElement}
89
173
  */
90
- static createTextElement(name, attributes, text) {
91
- const elem = this.createElement(name, attributes);
174
+ static createTextElement(name, attributes, text, parent) {
175
+ const elem = this.createElement(name, attributes, parent);
92
176
  this.setTextValue(elem, text.toString());
93
177
  return elem;
94
178
  }
@@ -189,7 +273,7 @@ export class hdom {
189
273
  */
190
274
  static isHidden(e) {
191
275
  const elem = this.getElement(e);
192
- return elem instanceof HTMLElement && elem.hidden;
276
+ return elem.hidden === true;
193
277
  }
194
278
 
195
279
  /**
@@ -235,6 +319,18 @@ export class hdom {
235
319
  elem.setAttribute(attName, value);
236
320
  }
237
321
 
322
+ /**
323
+ * @param {string|HTMLElement} e
324
+ * @param {boolean} state
325
+ */
326
+ static setCheckBoxState(e, state) {
327
+ const elem = this.getElement(e);
328
+ if (!(elem instanceof HTMLInputElement)) {
329
+ return;
330
+ }
331
+ elem.checked = state;
332
+ }
333
+
238
334
  /**
239
335
  * @param {string|Element} e
240
336
  * @param {string|number} text
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmltools/hdom",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Tools to interact with the HTML DOM.",
5
5
  "license": "MIT",
6
6
  "repository": {