@aarongustafson/form-show-if 3.0.1 → 3.1.0
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/README.md +9 -1
- package/form-show-if.d.ts +11 -0
- package/form-show-if.js +163 -30
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -4,9 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
Currently, HTML provides no mechanism to show & hide dependent fields. Sometimes you only want a field to show when certain other fields have a (particular) value. The `form-show-if` web component enables that.
|
|
6
6
|
|
|
7
|
+
## TypeScript Support
|
|
8
|
+
|
|
9
|
+
- Ships with bundled `.d.ts` definitions so editors and TypeScript builds fully understand `FormShowIfElement`.
|
|
10
|
+
- The package export map exposes the types automatically; no extra configuration is required to consume them in TS projects.
|
|
11
|
+
- `HTMLElementTagNameMap` is augmented so `form-show-if` elements are correctly typed when using JSX/TSX or querying via `document.querySelector`.
|
|
12
|
+
|
|
7
13
|
## Demo
|
|
8
14
|
|
|
9
|
-
[Live Demo](https://aarongustafson.github.io/form-show-if/demo/) ([Source](./demo/index.html))
|
|
15
|
+
- [Live Demo](https://aarongustafson.github.io/form-show-if/demo/) ([Source](./demo/index.html))
|
|
16
|
+
- [ESM Demo](https://aarongustafson.github.io/form-show-if/demo/esm.html) ([Source](./demo/esm.html))
|
|
17
|
+
- [unpkg Demo](https://aarongustafson.github.io/form-show-if/demo/unpkg.html) ([Source](./demo/unpkg.html))
|
|
10
18
|
|
|
11
19
|
## Installation
|
|
12
20
|
|
package/form-show-if.js
CHANGED
|
@@ -1,23 +1,122 @@
|
|
|
1
1
|
export class FormShowIfElement extends HTMLElement {
|
|
2
|
+
static get observedAttributes() {
|
|
3
|
+
return ['conditions', 'disabled-class', 'enabled-class'];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
this.__$wrapper = null;
|
|
9
|
+
this.__$field = null;
|
|
10
|
+
this.__$form = null;
|
|
11
|
+
this.__$fields = [];
|
|
12
|
+
this.__conditions = [];
|
|
13
|
+
this.__fieldCache = Object.create(null);
|
|
14
|
+
this.__is_shown = null;
|
|
15
|
+
this.__disabledClass = null;
|
|
16
|
+
this.__enabledClass = null;
|
|
17
|
+
this.__boundCheckIfShouldShow = null;
|
|
18
|
+
this.__boundHandleReset = null;
|
|
19
|
+
this.__rafId = null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
23
|
+
if (oldValue === newValue) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
switch (name) {
|
|
27
|
+
case 'conditions':
|
|
28
|
+
this.__conditions = newValue ? newValue.split('||') : [];
|
|
29
|
+
this.__fieldCache = Object.create(null);
|
|
30
|
+
if (this.isConnected && this.__$field) {
|
|
31
|
+
this.__checkIfShouldShow();
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case 'disabled-class':
|
|
35
|
+
this.__disabledClass = newValue || null;
|
|
36
|
+
if (this.isConnected && this.__$wrapper) {
|
|
37
|
+
this.__toggleClasses();
|
|
38
|
+
if (!this.__disabledClass && !this.__is_shown) {
|
|
39
|
+
this.__$wrapper.hidden = true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
case 'enabled-class':
|
|
44
|
+
this.__enabledClass = newValue || null;
|
|
45
|
+
if (this.isConnected && this.__$wrapper) {
|
|
46
|
+
this.__toggleClasses();
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get conditions() {
|
|
53
|
+
return this.getAttribute('conditions');
|
|
54
|
+
}
|
|
55
|
+
set conditions(value) {
|
|
56
|
+
if (value === null || value === undefined) {
|
|
57
|
+
this.removeAttribute('conditions');
|
|
58
|
+
} else {
|
|
59
|
+
this.setAttribute('conditions', value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get disabledClass() {
|
|
64
|
+
return this.getAttribute('disabled-class');
|
|
65
|
+
}
|
|
66
|
+
set disabledClass(value) {
|
|
67
|
+
if (value === null || value === undefined) {
|
|
68
|
+
this.removeAttribute('disabled-class');
|
|
69
|
+
} else {
|
|
70
|
+
this.setAttribute('disabled-class', value);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get enabledClass() {
|
|
75
|
+
return this.getAttribute('enabled-class');
|
|
76
|
+
}
|
|
77
|
+
set enabledClass(value) {
|
|
78
|
+
if (value === null || value === undefined) {
|
|
79
|
+
this.removeAttribute('enabled-class');
|
|
80
|
+
} else {
|
|
81
|
+
this.setAttribute('enabled-class', value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
__upgradeProperty(prop) {
|
|
86
|
+
if (Object.prototype.hasOwnProperty.call(this, prop)) {
|
|
87
|
+
const value = this[prop];
|
|
88
|
+
delete this[prop];
|
|
89
|
+
this[prop] = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
2
93
|
connectedCallback() {
|
|
94
|
+
this.__upgradeProperty('conditions');
|
|
95
|
+
this.__upgradeProperty('disabledClass');
|
|
96
|
+
this.__upgradeProperty('enabledClass');
|
|
3
97
|
// Ensures Light DOM is available - use rAF for better performance
|
|
4
|
-
requestAnimationFrame(() => {
|
|
98
|
+
this.__rafId = requestAnimationFrame(() => {
|
|
99
|
+
this.__rafId = null;
|
|
5
100
|
this.__$wrapper = this;
|
|
6
101
|
this.__$field = this.querySelector(
|
|
7
102
|
'input:not([type=submit],[type=reset],[type=image],[type=button]),select,textarea',
|
|
8
103
|
);
|
|
9
104
|
this.__$form = this.closest('form') || document.body;
|
|
10
|
-
this.__$fields = [this.__$field];
|
|
105
|
+
this.__$fields = this.__$field ? [this.__$field] : [];
|
|
106
|
+
if (!this.__$field) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
11
109
|
|
|
12
110
|
// Parse conditions once and cache
|
|
13
|
-
const conditionsAttr = this.
|
|
111
|
+
const conditionsAttr = this.conditions;
|
|
14
112
|
this.__conditions = conditionsAttr
|
|
15
113
|
? conditionsAttr.split('||')
|
|
16
114
|
: [];
|
|
17
115
|
|
|
18
116
|
this.__is_shown = null;
|
|
19
|
-
this.__disabledClass = this.
|
|
20
|
-
this.__enabledClass = this.
|
|
117
|
+
this.__disabledClass = this.disabledClass;
|
|
118
|
+
this.__enabledClass = this.enabledClass;
|
|
119
|
+
this.__fieldCache = Object.create(null);
|
|
21
120
|
|
|
22
121
|
// Cache bound methods to avoid creating new functions on each call
|
|
23
122
|
this.__boundCheckIfShouldShow = this.__checkIfShouldShow.bind(this);
|
|
@@ -29,6 +128,10 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
29
128
|
|
|
30
129
|
disconnectedCallback() {
|
|
31
130
|
// Clean up event listeners to prevent memory leaks
|
|
131
|
+
if (this.__rafId !== null) {
|
|
132
|
+
cancelAnimationFrame(this.__rafId);
|
|
133
|
+
this.__rafId = null;
|
|
134
|
+
}
|
|
32
135
|
if (
|
|
33
136
|
this.__$form &&
|
|
34
137
|
this.__boundCheckIfShouldShow &&
|
|
@@ -53,27 +156,34 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
53
156
|
false,
|
|
54
157
|
);
|
|
55
158
|
}
|
|
159
|
+
this.__$fields = [];
|
|
160
|
+
this.__$field = null;
|
|
161
|
+
this.__$form = null;
|
|
162
|
+
this.__$wrapper = null;
|
|
163
|
+
this.__fieldCache = Object.create(null);
|
|
56
164
|
}
|
|
57
165
|
|
|
58
166
|
__addObservers() {
|
|
59
167
|
// Only add reset listener if we're attached to an actual form
|
|
60
|
-
if (this.__$form
|
|
168
|
+
if (this.__$form?.tagName === 'FORM') {
|
|
61
169
|
this.__$form.addEventListener(
|
|
62
170
|
'reset',
|
|
63
171
|
this.__boundHandleReset,
|
|
64
172
|
false,
|
|
65
173
|
);
|
|
66
174
|
}
|
|
67
|
-
this.__$form
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
175
|
+
if (this.__$form) {
|
|
176
|
+
this.__$form.addEventListener(
|
|
177
|
+
'change',
|
|
178
|
+
this.__boundCheckIfShouldShow,
|
|
179
|
+
false,
|
|
180
|
+
);
|
|
181
|
+
this.__$form.addEventListener(
|
|
182
|
+
'input',
|
|
183
|
+
this.__boundCheckIfShouldShow,
|
|
184
|
+
false,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
77
187
|
}
|
|
78
188
|
|
|
79
189
|
__handleReset() {
|
|
@@ -95,6 +205,9 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
95
205
|
}
|
|
96
206
|
|
|
97
207
|
__gatherSiblingFields() {
|
|
208
|
+
if (!this.__$field?.name) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
98
211
|
const $fields = this.querySelectorAll(`[name="${this.__$field.name}"]`);
|
|
99
212
|
if ($fields.length > 1) {
|
|
100
213
|
this.__$fields = Array.from($fields);
|
|
@@ -103,7 +216,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
103
216
|
|
|
104
217
|
// Wrapper `class` Management
|
|
105
218
|
__toggleEnabledClass() {
|
|
106
|
-
if (!this.__enabledClass) {
|
|
219
|
+
if (!this.__enabledClass || !this.__$wrapper) {
|
|
107
220
|
return;
|
|
108
221
|
}
|
|
109
222
|
if (this.__is_shown) {
|
|
@@ -113,7 +226,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
113
226
|
}
|
|
114
227
|
}
|
|
115
228
|
__toggleDisabledClass() {
|
|
116
|
-
if (!this.__disabledClass) {
|
|
229
|
+
if (!this.__disabledClass || !this.__$wrapper) {
|
|
117
230
|
return;
|
|
118
231
|
}
|
|
119
232
|
if (!this.__is_shown) {
|
|
@@ -151,7 +264,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
151
264
|
}
|
|
152
265
|
this.__is_shown = false;
|
|
153
266
|
// Wrapper changes
|
|
154
|
-
if (!this.__disabledClass) {
|
|
267
|
+
if (!this.__disabledClass && this.__$wrapper) {
|
|
155
268
|
this.__$wrapper.hidden = true;
|
|
156
269
|
}
|
|
157
270
|
this.__toggleClasses();
|
|
@@ -162,23 +275,16 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
162
275
|
}
|
|
163
276
|
|
|
164
277
|
__checkIfShouldShow() {
|
|
278
|
+
if (!this.__$form) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
165
281
|
let should_show = false;
|
|
166
282
|
// Use cached conditions array directly
|
|
167
283
|
for (let i = 0; i < this.__conditions.length; i++) {
|
|
168
284
|
const condition = this.__conditions[i];
|
|
169
285
|
const [name, value] = condition.split('=');
|
|
170
286
|
|
|
171
|
-
|
|
172
|
-
let $field = this.__$form.elements
|
|
173
|
-
? this.__$form.elements[name]
|
|
174
|
-
: null;
|
|
175
|
-
if (!$field) {
|
|
176
|
-
// For non-form contexts, get all elements with this name
|
|
177
|
-
$field = this.__$form.querySelectorAll(`[name="${name}"]`);
|
|
178
|
-
if ($field.length === 0) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
287
|
+
const $field = this.__getConditionField(name);
|
|
182
288
|
if (!$field) {
|
|
183
289
|
continue;
|
|
184
290
|
}
|
|
@@ -205,6 +311,33 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
205
311
|
this.__checkIfShouldShow();
|
|
206
312
|
}
|
|
207
313
|
|
|
314
|
+
__getConditionField(name) {
|
|
315
|
+
if (this.__fieldCache[name]) {
|
|
316
|
+
return this.__fieldCache[name];
|
|
317
|
+
}
|
|
318
|
+
let $field = null;
|
|
319
|
+
if (
|
|
320
|
+
this.__$form &&
|
|
321
|
+
'elements' in this.__$form &&
|
|
322
|
+
this.__$form.elements
|
|
323
|
+
) {
|
|
324
|
+
const candidate = this.__$form.elements[name];
|
|
325
|
+
if (candidate) {
|
|
326
|
+
$field = candidate;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (!$field && this.__$form) {
|
|
330
|
+
const nodeList = this.__$form.querySelectorAll(`[name="${name}"]`);
|
|
331
|
+
if (nodeList.length > 0) {
|
|
332
|
+
$field = nodeList;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if ($field) {
|
|
336
|
+
this.__fieldCache[name] = $field;
|
|
337
|
+
}
|
|
338
|
+
return $field;
|
|
339
|
+
}
|
|
340
|
+
|
|
208
341
|
static __getCurrentValue($field) {
|
|
209
342
|
// Handle NodeList from querySelectorAll (non-form context)
|
|
210
343
|
if ($field instanceof NodeList) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aarongustafson/form-show-if",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Web component that enables you to make one HTML form field show (or not) based on the value(s) of another field.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"html",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"import": "./index.js"
|
|
33
33
|
},
|
|
34
34
|
"./form-show-if.js": {
|
|
35
|
-
"import": "./form-show-if.js"
|
|
35
|
+
"import": "./form-show-if.js",
|
|
36
|
+
"types": "./form-show-if.d.ts"
|
|
36
37
|
},
|
|
37
38
|
"./define.js": {
|
|
38
39
|
"import": "./define.js"
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
},
|
|
42
43
|
"files": [
|
|
43
44
|
"form-show-if.js",
|
|
45
|
+
"form-show-if.d.ts",
|
|
44
46
|
"define.js",
|
|
45
47
|
"index.js",
|
|
46
48
|
"custom-elements.json",
|