@aarongustafson/form-show-if 2.0.3 → 3.0.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 CHANGED
@@ -1,7 +1,13 @@
1
1
  # Show If Web Component
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/@aarongustafson/form-show-if.svg)](https://www.npmjs.com/package/@aarongustafson/form-show-if) [![Build Status](https://img.shields.io/github/actions/workflow/status/aarongustafson/form-show-if/ci.yml?branch=main)](https://github.com/aarongustafson/form-show-if/actions)
4
+
3
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.
4
6
 
7
+ ## Demo
8
+
9
+ [Live Demo](https://aarongustafson.github.io/form-show-if/demo/) ([Source](./demo/index.html))
10
+
5
11
  ## Installation
6
12
 
7
13
  ```bash
@@ -10,26 +16,12 @@ npm install @aarongustafson/form-show-if
10
16
 
11
17
  ## Usage
12
18
 
13
- ### Option 1: Auto-define the custom element (easiest)
14
-
15
- Import the package to automatically define the `<form-show-if>` custom element:
16
-
17
- ```javascript
18
- import '@aarongustafson/form-show-if';
19
- ```
20
-
21
- Or use the define-only script in HTML:
22
-
23
- ```html
24
- <script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>
25
- ```
26
-
27
- ### Option 2: Import the class and define manually
19
+ ### Option 1: Import the class and define manually
28
20
 
29
21
  Import the class and define the custom element with your preferred tag name:
30
22
 
31
23
  ```javascript
32
- import { FormShowIfElement } from '@aarongustafson/form-show-if/form-show-if.js';
24
+ import { FormShowIfElement } from '@aarongustafson/form-show-if';
33
25
 
34
26
  // Define with default name
35
27
  customElements.define('form-show-if', FormShowIfElement);
@@ -38,13 +30,26 @@ customElements.define('form-show-if', FormShowIfElement);
38
30
  customElements.define('my-conditional-field', FormShowIfElement);
39
31
  ```
40
32
 
41
- ### Option 3: Import everything (class + auto-define)
33
+ ### Option 2: Auto-define the custom element (browser environments only)
42
34
 
43
- Get both the class export and automatic element definition:
35
+ Use the guarded definition helper to register the element when `customElements` is available:
44
36
 
45
37
  ```javascript
46
- import { FormShowIfElement } from '@aarongustafson/form-show-if';
47
- // 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>
48
53
  ```
49
54
 
50
55
  ### CDN Usage
@@ -52,7 +57,7 @@ import { FormShowIfElement } from '@aarongustafson/form-show-if';
52
57
  You can also use the component directly from a CDN:
53
58
 
54
59
  ```html
55
- <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>
56
61
  ```
57
62
 
58
63
  ## API
@@ -139,10 +144,6 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
139
144
  </form-show-if>
140
145
  ```
141
146
 
142
- ## Demo
143
-
144
- [Live Demo](https://aarongustafson.github.io/form-show-if/demo/index.html) ([Source](./demo/index.html))
145
-
146
147
  ## Browser Support
147
148
 
148
149
  This web component works in all modern browsers that support:
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
@@ -1,7 +1,7 @@
1
1
  export class FormShowIfElement extends HTMLElement {
2
2
  connectedCallback() {
3
- // Ensures Light DOM is available
4
- setTimeout(() => {
3
+ // Ensures Light DOM is available - use rAF for better performance
4
+ requestAnimationFrame(() => {
5
5
  this.__$wrapper = this;
6
6
  this.__$field = this.querySelector(
7
7
  'input:not([type=submit],[type=reset],[type=image],[type=button]),select,textarea',
@@ -9,33 +9,70 @@ export class FormShowIfElement extends HTMLElement {
9
9
  this.__$form = this.closest('form');
10
10
  this.__$fields = [this.__$field];
11
11
 
12
- this.__conditions = this.getAttribute('conditions').split('||');
12
+ // Parse conditions once and cache
13
+ const conditionsAttr = this.getAttribute('conditions');
14
+ this.__conditions = conditionsAttr
15
+ ? conditionsAttr.split('||')
16
+ : [];
13
17
 
14
18
  this.__is_shown = null;
15
19
  this.__disabledClass = this.getAttribute('disabled-class');
16
20
  this.__enabledClass = this.getAttribute('enabled-class');
17
21
 
22
+ // Cache bound methods to avoid creating new functions on each call
23
+ this.__boundCheckIfShouldShow = this.__checkIfShouldShow.bind(this);
24
+ this.__boundHandleReset = this.__handleReset.bind(this);
25
+
18
26
  this.__init();
19
27
  });
20
28
  }
21
29
 
30
+ disconnectedCallback() {
31
+ // Clean up event listeners to prevent memory leaks
32
+ if (
33
+ this.__$form &&
34
+ this.__boundCheckIfShouldShow &&
35
+ this.__boundHandleReset
36
+ ) {
37
+ this.__$form.removeEventListener(
38
+ 'reset',
39
+ this.__boundHandleReset,
40
+ false,
41
+ );
42
+ this.__$form.removeEventListener(
43
+ 'change',
44
+ this.__boundCheckIfShouldShow,
45
+ false,
46
+ );
47
+ this.__$form.removeEventListener(
48
+ 'input',
49
+ this.__boundCheckIfShouldShow,
50
+ false,
51
+ );
52
+ }
53
+ }
54
+
22
55
  __addObservers() {
23
- const reset = () => {
24
- setTimeout(this.__checkIfShouldShow.bind(this), 100);
25
- };
26
- this.__$form.addEventListener('reset', reset.bind(this), false);
56
+ this.__$form.addEventListener('reset', this.__boundHandleReset, false);
27
57
  this.__$form.addEventListener(
28
58
  'change',
29
- this.__checkIfShouldShow.bind(this),
59
+ this.__boundCheckIfShouldShow,
30
60
  false,
31
61
  );
32
62
  this.__$form.addEventListener(
33
63
  'input',
34
- this.__checkIfShouldShow.bind(this),
64
+ this.__boundCheckIfShouldShow,
35
65
  false,
36
66
  );
37
67
  }
38
68
 
69
+ __handleReset() {
70
+ // Use requestAnimationFrame for better performance than setTimeout
71
+ requestAnimationFrame(() => {
72
+ this.__checkIfShouldShow();
73
+ });
74
+ }
75
+
39
76
  __determineWrapper() {
40
77
  let $wrapper = this.__$field.parentElement;
41
78
  while ($wrapper.querySelector('label') === null) {
@@ -49,8 +86,8 @@ export class FormShowIfElement extends HTMLElement {
49
86
 
50
87
  __gatherSiblingFields() {
51
88
  const $fields = this.querySelectorAll(`[name="${this.__$field.name}"]`);
52
- if ($fields.length) {
53
- this.__$fields = [...this.__$fields, $fields];
89
+ if ($fields.length > 1) {
90
+ this.__$fields = Array.from($fields);
54
91
  }
55
92
  }
56
93
 
@@ -82,25 +119,33 @@ export class FormShowIfElement extends HTMLElement {
82
119
 
83
120
  // Show / Hide Logic
84
121
  __showField() {
122
+ // Early return if already shown
123
+ if (this.__is_shown === true) {
124
+ return;
125
+ }
85
126
  this.__is_shown = true;
86
127
  // Wrapper changes
87
128
  if (!this.__disabledClass) {
88
129
  this.__$wrapper.removeAttribute('hidden');
89
130
  }
90
131
  this.__toggleClasses();
91
- // Disable field submission
132
+ // Enable field submission
92
133
  this.__$fields.forEach(($field) => {
93
134
  $field.disabled = false;
94
135
  });
95
136
  }
96
137
  __hideField() {
138
+ // Early return if already hidden
139
+ if (this.__is_shown === false) {
140
+ return;
141
+ }
97
142
  this.__is_shown = false;
98
143
  // Wrapper changes
99
144
  if (!this.__disabledClass) {
100
145
  this.__$wrapper.hidden = true;
101
146
  }
102
147
  this.__toggleClasses();
103
- // Enable field submission
148
+ // Disable field submission
104
149
  this.__$fields.forEach(($field) => {
105
150
  $field.disabled = true;
106
151
  });
@@ -108,26 +153,27 @@ export class FormShowIfElement extends HTMLElement {
108
153
 
109
154
  __checkIfShouldShow() {
110
155
  let should_show = false;
111
- let test_conditions = this.__conditions;
112
- test_conditions.some((condition) => {
156
+ // Use cached conditions array directly
157
+ for (let i = 0; i < this.__conditions.length; i++) {
158
+ const condition = this.__conditions[i];
113
159
  const [name, value] = condition.split('=');
114
160
 
115
161
  const $field = this.__$form.elements[name];
116
162
  if (!$field) {
117
- return;
163
+ continue;
118
164
  }
119
165
 
120
166
  const current_value = FormShowIfElement.__getCurrentValue($field);
121
167
  if (FormShowIfElement.__valuesMatch(value, current_value)) {
122
168
  should_show = true;
123
- return true;
169
+ break;
124
170
  }
125
- return false;
126
- });
171
+ }
127
172
 
128
- if (should_show && this.__is_shown !== true) {
173
+ // Early returns in show/hide methods prevent redundant work
174
+ if (should_show) {
129
175
  this.__showField();
130
- } else if (!should_show && this.__is_shown !== false) {
176
+ } else {
131
177
  this.__hideField();
132
178
  }
133
179
  }
@@ -152,15 +198,12 @@ export class FormShowIfElement extends HTMLElement {
152
198
 
153
199
  // Checkbox array (multiple checkboxes with same name)
154
200
  if ($field.length && $field[0].type && $field[0].type == 'checkbox') {
155
- let value = [];
156
- let length = $field.length;
157
- while (length--) {
158
- let $current_field = $field[length];
159
- if ($current_field.checked) {
160
- value.push($current_field.value);
201
+ const value = [];
202
+ for (let i = 0; i < $field.length; i++) {
203
+ if ($field[i].checked) {
204
+ value.push($field[i].value);
161
205
  }
162
206
  }
163
- value.reverse();
164
207
  return value;
165
208
  }
166
209
 
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.3",
3
+ "version": "3.0.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",