@aarongustafson/form-show-if 3.0.0 → 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 +205 -27
- package/package.json +6 -3
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
|
-
this.__$form = this.closest('form');
|
|
10
|
-
this.__$fields = [this.__$field];
|
|
104
|
+
this.__$form = this.closest('form') || document.body;
|
|
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,16 +128,23 @@ 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 &&
|
|
35
138
|
this.__boundHandleReset
|
|
36
139
|
) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.
|
|
40
|
-
|
|
41
|
-
|
|
140
|
+
// Only remove reset listener if we're attached to an actual form
|
|
141
|
+
if (this.__$form.tagName === 'FORM') {
|
|
142
|
+
this.__$form.removeEventListener(
|
|
143
|
+
'reset',
|
|
144
|
+
this.__boundHandleReset,
|
|
145
|
+
false,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
42
148
|
this.__$form.removeEventListener(
|
|
43
149
|
'change',
|
|
44
150
|
this.__boundCheckIfShouldShow,
|
|
@@ -50,20 +156,34 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
50
156
|
false,
|
|
51
157
|
);
|
|
52
158
|
}
|
|
159
|
+
this.__$fields = [];
|
|
160
|
+
this.__$field = null;
|
|
161
|
+
this.__$form = null;
|
|
162
|
+
this.__$wrapper = null;
|
|
163
|
+
this.__fieldCache = Object.create(null);
|
|
53
164
|
}
|
|
54
165
|
|
|
55
166
|
__addObservers() {
|
|
56
|
-
|
|
57
|
-
this.__$form
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
167
|
+
// Only add reset listener if we're attached to an actual form
|
|
168
|
+
if (this.__$form?.tagName === 'FORM') {
|
|
169
|
+
this.__$form.addEventListener(
|
|
170
|
+
'reset',
|
|
171
|
+
this.__boundHandleReset,
|
|
172
|
+
false,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
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
|
+
}
|
|
67
187
|
}
|
|
68
188
|
|
|
69
189
|
__handleReset() {
|
|
@@ -85,6 +205,9 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
85
205
|
}
|
|
86
206
|
|
|
87
207
|
__gatherSiblingFields() {
|
|
208
|
+
if (!this.__$field?.name) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
88
211
|
const $fields = this.querySelectorAll(`[name="${this.__$field.name}"]`);
|
|
89
212
|
if ($fields.length > 1) {
|
|
90
213
|
this.__$fields = Array.from($fields);
|
|
@@ -93,7 +216,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
93
216
|
|
|
94
217
|
// Wrapper `class` Management
|
|
95
218
|
__toggleEnabledClass() {
|
|
96
|
-
if (!this.__enabledClass) {
|
|
219
|
+
if (!this.__enabledClass || !this.__$wrapper) {
|
|
97
220
|
return;
|
|
98
221
|
}
|
|
99
222
|
if (this.__is_shown) {
|
|
@@ -103,7 +226,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
103
226
|
}
|
|
104
227
|
}
|
|
105
228
|
__toggleDisabledClass() {
|
|
106
|
-
if (!this.__disabledClass) {
|
|
229
|
+
if (!this.__disabledClass || !this.__$wrapper) {
|
|
107
230
|
return;
|
|
108
231
|
}
|
|
109
232
|
if (!this.__is_shown) {
|
|
@@ -141,7 +264,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
141
264
|
}
|
|
142
265
|
this.__is_shown = false;
|
|
143
266
|
// Wrapper changes
|
|
144
|
-
if (!this.__disabledClass) {
|
|
267
|
+
if (!this.__disabledClass && this.__$wrapper) {
|
|
145
268
|
this.__$wrapper.hidden = true;
|
|
146
269
|
}
|
|
147
270
|
this.__toggleClasses();
|
|
@@ -152,13 +275,16 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
152
275
|
}
|
|
153
276
|
|
|
154
277
|
__checkIfShouldShow() {
|
|
278
|
+
if (!this.__$form) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
155
281
|
let should_show = false;
|
|
156
282
|
// Use cached conditions array directly
|
|
157
283
|
for (let i = 0; i < this.__conditions.length; i++) {
|
|
158
284
|
const condition = this.__conditions[i];
|
|
159
285
|
const [name, value] = condition.split('=');
|
|
160
286
|
|
|
161
|
-
const $field = this.
|
|
287
|
+
const $field = this.__getConditionField(name);
|
|
162
288
|
if (!$field) {
|
|
163
289
|
continue;
|
|
164
290
|
}
|
|
@@ -185,7 +311,59 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
185
311
|
this.__checkIfShouldShow();
|
|
186
312
|
}
|
|
187
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
|
+
|
|
188
341
|
static __getCurrentValue($field) {
|
|
342
|
+
// Handle NodeList from querySelectorAll (non-form context)
|
|
343
|
+
if ($field instanceof NodeList) {
|
|
344
|
+
// Check if it's a checkbox or radio group
|
|
345
|
+
if ($field[0] && $field[0].type === 'checkbox') {
|
|
346
|
+
const value = [];
|
|
347
|
+
for (let i = 0; i < $field.length; i++) {
|
|
348
|
+
if ($field[i].checked) {
|
|
349
|
+
value.push($field[i].value);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return value;
|
|
353
|
+
}
|
|
354
|
+
// For radio buttons, find the checked one
|
|
355
|
+
if ($field[0] && $field[0].type === 'radio') {
|
|
356
|
+
for (let i = 0; i < $field.length; i++) {
|
|
357
|
+
if ($field[i].checked) {
|
|
358
|
+
return $field[i].value;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return '';
|
|
362
|
+
}
|
|
363
|
+
// For other types, just use the first element
|
|
364
|
+
return $field[0] ? $field[0].value : '';
|
|
365
|
+
}
|
|
366
|
+
|
|
189
367
|
// Single checkbox
|
|
190
368
|
if ($field.type === 'checkbox' && !$field.length) {
|
|
191
369
|
// Only return the value if the checkbox is actually checked
|
|
@@ -196,7 +374,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
196
374
|
return '';
|
|
197
375
|
}
|
|
198
376
|
|
|
199
|
-
// Checkbox array (multiple checkboxes with same name)
|
|
377
|
+
// Checkbox array (multiple checkboxes with same name) from form.elements
|
|
200
378
|
if ($field.length && $field[0].type && $field[0].type == 'checkbox') {
|
|
201
379
|
const value = [];
|
|
202
380
|
for (let i = 0; i < $field.length; i++) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aarongustafson/form-show-if",
|
|
3
|
-
"version": "3.
|
|
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",
|
|
@@ -60,7 +62,8 @@
|
|
|
60
62
|
"lint:eslint": "eslint .",
|
|
61
63
|
"format:eslint": "eslint . --fix",
|
|
62
64
|
"lint:prettier": "prettier \"**/*.js\" --check --ignore-path .gitignore",
|
|
63
|
-
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore"
|
|
65
|
+
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore",
|
|
66
|
+
"version": "node scripts/update-demo-versions.js && git add demo/esm.html"
|
|
64
67
|
},
|
|
65
68
|
"devDependencies": {
|
|
66
69
|
"@open-wc/eslint-config": "^13.0.0",
|