@aarongustafson/form-show-if 2.0.2 → 2.0.4

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
@@ -139,10 +145,6 @@ The "wrapper" mentioned below refers to the nearest mutual parent of the field &
139
145
  </form-show-if>
140
146
  ```
141
147
 
142
- ## Demo
143
-
144
- [Live Demo](https://aarongustafson.github.io/form-show-if/demo/index.html) ([Source](./demo/index.html))
145
-
146
148
  ## Browser Support
147
149
 
148
150
  This web component works in all modern browsers that support:
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aarongustafson/form-show-if",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
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",