@aarongustafson/form-show-if 2.0.4 → 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/README.md CHANGED
@@ -16,26 +16,12 @@ npm install @aarongustafson/form-show-if
16
16
 
17
17
  ## Usage
18
18
 
19
- ### Option 1: Auto-define the custom element (easiest)
20
-
21
- Import the package to automatically define the `<form-show-if>` custom element:
22
-
23
- ```javascript
24
- import '@aarongustafson/form-show-if';
25
- ```
26
-
27
- Or use the define-only script in HTML:
28
-
29
- ```html
30
- <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
31
- ```
32
-
33
- ### Option 2: Import the class and define manually
19
+ ### Option 1: Import the class and define manually
34
20
 
35
21
  Import the class and define the custom element with your preferred tag name:
36
22
 
37
23
  ```javascript
38
- import { FormShowIfElement } from '@aarongustafson/form-show-if/form-show-if.js';
24
+ import { FormShowIfElement } from '@aarongustafson/form-show-if';
39
25
 
40
26
  // Define with default name
41
27
  customElements.define('form-show-if', FormShowIfElement);
@@ -44,13 +30,26 @@ customElements.define('form-show-if', FormShowIfElement);
44
30
  customElements.define('my-conditional-field', FormShowIfElement);
45
31
  ```
46
32
 
47
- ### Option 3: Import everything (class + auto-define)
33
+ ### Option 2: Auto-define the custom element (browser environments only)
48
34
 
49
- Get both the class export and automatic element definition:
35
+ Use the guarded definition helper to register the element when `customElements` is available:
50
36
 
51
37
  ```javascript
52
- import { FormShowIfElement } from '@aarongustafson/form-show-if';
53
- // The custom element is now defined AND you have access to the class
38
+ import '@aarongustafson/form-show-if/define.js';
39
+ ```
40
+
41
+ If you prefer to control when the element is registered, call the helper directly:
42
+
43
+ ```javascript
44
+ import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';
45
+
46
+ defineFormShowIf();
47
+ ```
48
+
49
+ You can also include the guarded script from HTML:
50
+
51
+ ```html
52
+ <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
54
53
  ```
55
54
 
56
55
  ### CDN Usage
@@ -58,7 +57,7 @@ import { FormShowIfElement } from '@aarongustafson/form-show-if';
58
57
  You can also use the component directly from a CDN:
59
58
 
60
59
  ```html
61
- <script src="https://unpkg.com/@aarongustafson/form-show-if/define.js" type="module"></script>
60
+ <script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>
62
61
  ```
63
62
 
64
63
  ## API
package/define.js CHANGED
@@ -1,5 +1,18 @@
1
1
  import { FormShowIfElement } from './form-show-if.js';
2
2
 
3
- if (!!customElements) {
4
- customElements.define('form-show-if', FormShowIfElement);
3
+ export function defineFormShowIf(tagName = 'form-show-if') {
4
+ const hasWindow = typeof window !== 'undefined';
5
+ const registry = hasWindow ? window.customElements : undefined;
6
+
7
+ if (!registry || typeof registry.define !== 'function') {
8
+ return false;
9
+ }
10
+
11
+ if (!registry.get(tagName)) {
12
+ registry.define(tagName, FormShowIfElement);
13
+ }
14
+
15
+ return true;
5
16
  }
17
+
18
+ defineFormShowIf();
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
- this.__$form.removeEventListener(
38
- 'reset',
39
- this.__boundHandleReset,
40
- false,
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
- this.__$form.addEventListener('reset', this.__boundHandleReset, false);
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
- const $field = this.__$form.elements[name];
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/index.js CHANGED
@@ -1,2 +1 @@
1
1
  export { FormShowIfElement } from './form-show-if.js';
2
- import './define.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aarongustafson/form-show-if",
3
- "version": "2.0.4",
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",