@aarongustafson/form-show-if 3.0.0 → 3.0.1
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/form-show-if.js +54 -9
- package/package.json +3 -2
package/form-show-if.js
CHANGED
|
@@ -6,7 +6,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
6
6
|
this.__$field = this.querySelector(
|
|
7
7
|
'input:not([type=submit],[type=reset],[type=image],[type=button]),select,textarea',
|
|
8
8
|
);
|
|
9
|
-
this.__$form = this.closest('form');
|
|
9
|
+
this.__$form = this.closest('form') || document.body;
|
|
10
10
|
this.__$fields = [this.__$field];
|
|
11
11
|
|
|
12
12
|
// Parse conditions once and cache
|
|
@@ -34,11 +34,14 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
34
34
|
this.__boundCheckIfShouldShow &&
|
|
35
35
|
this.__boundHandleReset
|
|
36
36
|
) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
// Only remove reset listener if we're attached to an actual form
|
|
38
|
+
if (this.__$form.tagName === 'FORM') {
|
|
39
|
+
this.__$form.removeEventListener(
|
|
40
|
+
'reset',
|
|
41
|
+
this.__boundHandleReset,
|
|
42
|
+
false,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
42
45
|
this.__$form.removeEventListener(
|
|
43
46
|
'change',
|
|
44
47
|
this.__boundCheckIfShouldShow,
|
|
@@ -53,7 +56,14 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
__addObservers() {
|
|
56
|
-
|
|
59
|
+
// Only add reset listener if we're attached to an actual form
|
|
60
|
+
if (this.__$form.tagName === 'FORM') {
|
|
61
|
+
this.__$form.addEventListener(
|
|
62
|
+
'reset',
|
|
63
|
+
this.__boundHandleReset,
|
|
64
|
+
false,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
57
67
|
this.__$form.addEventListener(
|
|
58
68
|
'change',
|
|
59
69
|
this.__boundCheckIfShouldShow,
|
|
@@ -158,7 +168,17 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
158
168
|
const condition = this.__conditions[i];
|
|
159
169
|
const [name, value] = condition.split('=');
|
|
160
170
|
|
|
161
|
-
|
|
171
|
+
// Try form.elements first, fall back to querySelectorAll for non-form contexts
|
|
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
|
+
}
|
|
162
182
|
if (!$field) {
|
|
163
183
|
continue;
|
|
164
184
|
}
|
|
@@ -186,6 +206,31 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
186
206
|
}
|
|
187
207
|
|
|
188
208
|
static __getCurrentValue($field) {
|
|
209
|
+
// Handle NodeList from querySelectorAll (non-form context)
|
|
210
|
+
if ($field instanceof NodeList) {
|
|
211
|
+
// Check if it's a checkbox or radio group
|
|
212
|
+
if ($field[0] && $field[0].type === 'checkbox') {
|
|
213
|
+
const value = [];
|
|
214
|
+
for (let i = 0; i < $field.length; i++) {
|
|
215
|
+
if ($field[i].checked) {
|
|
216
|
+
value.push($field[i].value);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
221
|
+
// For radio buttons, find the checked one
|
|
222
|
+
if ($field[0] && $field[0].type === 'radio') {
|
|
223
|
+
for (let i = 0; i < $field.length; i++) {
|
|
224
|
+
if ($field[i].checked) {
|
|
225
|
+
return $field[i].value;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return '';
|
|
229
|
+
}
|
|
230
|
+
// For other types, just use the first element
|
|
231
|
+
return $field[0] ? $field[0].value : '';
|
|
232
|
+
}
|
|
233
|
+
|
|
189
234
|
// Single checkbox
|
|
190
235
|
if ($field.type === 'checkbox' && !$field.length) {
|
|
191
236
|
// Only return the value if the checkbox is actually checked
|
|
@@ -196,7 +241,7 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
196
241
|
return '';
|
|
197
242
|
}
|
|
198
243
|
|
|
199
|
-
// Checkbox array (multiple checkboxes with same name)
|
|
244
|
+
// Checkbox array (multiple checkboxes with same name) from form.elements
|
|
200
245
|
if ($field.length && $field[0].type && $field[0].type == 'checkbox') {
|
|
201
246
|
const value = [];
|
|
202
247
|
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.0.
|
|
3
|
+
"version": "3.0.1",
|
|
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",
|
|
@@ -60,7 +60,8 @@
|
|
|
60
60
|
"lint:eslint": "eslint .",
|
|
61
61
|
"format:eslint": "eslint . --fix",
|
|
62
62
|
"lint:prettier": "prettier \"**/*.js\" --check --ignore-path .gitignore",
|
|
63
|
-
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore"
|
|
63
|
+
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore",
|
|
64
|
+
"version": "node scripts/update-demo-versions.js && git add demo/esm.html"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
67
|
"@open-wc/eslint-config": "^13.0.0",
|