@htmltools/hdom 0.1.1 → 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.
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
  */
@@ -37,6 +41,43 @@ export class hdom {
37
41
  static createInputElement(attributes: {
38
42
  [x: string]: string | number | undefined;
39
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
+ };
40
81
  /**
41
82
  * @param {string} name
42
83
  * @param {Object<string,string>|string} attributes
@@ -104,6 +145,11 @@ export class hdom {
104
145
  * @param {string} value
105
146
  */
106
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;
107
153
  /**
108
154
  * @param {string|Element} e
109
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
  */
@@ -85,6 +96,74 @@ export class hdom {
85
96
  );
86
97
  }
87
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());
120
+ }
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
+ };
165
+ }
166
+
88
167
  /**
89
168
  * @param {string} name
90
169
  * @param {Object<string,string>|string} attributes
@@ -194,7 +273,7 @@ export class hdom {
194
273
  */
195
274
  static isHidden(e) {
196
275
  const elem = this.getElement(e);
197
- return elem instanceof HTMLElement && elem.hidden;
276
+ return elem.hidden === true;
198
277
  }
199
278
 
200
279
  /**
@@ -240,6 +319,18 @@ export class hdom {
240
319
  elem.setAttribute(attName, value);
241
320
  }
242
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
+
243
334
  /**
244
335
  * @param {string|Element} e
245
336
  * @param {string|number} text
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmltools/hdom",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Tools to interact with the HTML DOM.",
5
5
  "license": "MIT",
6
6
  "repository": {