@aarongustafson/form-show-if 3.1.5 → 3.2.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 +48 -12
- package/custom-elements.json +24 -1
- package/define.d.ts +4 -0
- package/form-show-if.d.ts +51 -3
- package/form-show-if.js +154 -61
- package/package.json +11 -4
package/README.md
CHANGED
|
@@ -93,17 +93,15 @@ You can also use the component directly from a CDN:
|
|
|
93
93
|
These string properties reflect their attributes. Setting a property to `null`
|
|
94
94
|
removes the attribute. Native `input`, `change`, and form `reset` events trigger
|
|
95
95
|
reevaluation; changing `conditions` also reevaluates an initialized component.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
upgrade; if you change them later, explicitly reconcile the old classes and
|
|
106
|
-
visibility while preserving entered values and focus.
|
|
96
|
+
Logical changes also emit `form-show-if:change`, as described below.
|
|
97
|
+
|
|
98
|
+
You can change `disabled-class` and `enabled-class` after initialization.
|
|
99
|
+
The component removes the old token and applies the current state's token
|
|
100
|
+
without replacing fields or clearing their values. Adding `disabled-class`
|
|
101
|
+
clears component-applied `hidden` so your CSS controls visibility; removing it restores
|
|
102
|
+
native hiding when unavailable. The fields remain disabled while unavailable,
|
|
103
|
+
regardless of the classes. If class-controlled visibility is configured initially,
|
|
104
|
+
an authored `hidden` attribute stays in place; remove it yourself when appropriate.
|
|
107
105
|
|
|
108
106
|
Conditions support OR, not AND. Names and values cannot contain the `||` or `=`
|
|
109
107
|
delimiters; there is no escaping syntax. Comparisons use current values, not the
|
|
@@ -125,6 +123,42 @@ Referenced controls are looked up in the nearest form, or the document body when
|
|
|
125
123
|
there is no form. Missing names do not match; a later input/change can find newly
|
|
126
124
|
added controls. Successful lookups are cached. If you replace a referenced node,
|
|
127
125
|
reconnect the component or change its conditions to clear that cache.
|
|
126
|
+
You can also call `refresh()` after replacing controls or changing values in
|
|
127
|
+
script without dispatching a native event.
|
|
128
|
+
|
|
129
|
+
### State and commands
|
|
130
|
+
|
|
131
|
+
`isShown` is a read-only boolean: the last evaluated condition match. It starts
|
|
132
|
+
as `false` before setup and retains its last value while disconnected. It describes
|
|
133
|
+
logical availability, **not** computed CSS visibility or native validity. With a
|
|
134
|
+
`disabled-class`, your styles still decide whether unavailable content is visible.
|
|
135
|
+
|
|
136
|
+
Call `refresh()` on a connected component to rediscover its first eligible native
|
|
137
|
+
field, same-name siblings, and containing form, clear cached controller references,
|
|
138
|
+
and reevaluate synchronously. It returns `isShown`, not a validity result. An early
|
|
139
|
+
call can complete pending initial setup. It throws a `DOMException` named
|
|
140
|
+
`InvalidStateError` when disconnected, or `NotFoundError` without an eligible field.
|
|
141
|
+
It preserves native field nodes and entered values. If the managed wrapper changes,
|
|
142
|
+
it removes component-applied hiding and state classes from the previous wrapper.
|
|
143
|
+
|
|
144
|
+
### Events
|
|
145
|
+
|
|
146
|
+
`form-show-if:change` bubbles, crosses shadow boundaries, and cannot be canceled.
|
|
147
|
+
Its detail is `{ isShown, previousIsShown }`, with two booleans and no field values
|
|
148
|
+
or element references. Listeners run after `isShown`, native `disabled`, wrapper
|
|
149
|
+
`hidden`, and state classes have been updated.
|
|
150
|
+
|
|
151
|
+
Initial setup and reconnect are silent. Later actual changes from native input,
|
|
152
|
+
change, reset, live `conditions` edits, or `refresh()` notify once per logical
|
|
153
|
+
transition. Repeated evaluation and presentation-only class changes don't emit.
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
const conditional = document.querySelector('form-show-if');
|
|
157
|
+
conditional.addEventListener('form-show-if:change', (event) => {
|
|
158
|
+
console.log(event.detail.isShown);
|
|
159
|
+
});
|
|
160
|
+
const available = conditional.refresh();
|
|
161
|
+
```
|
|
128
162
|
|
|
129
163
|
## Implementation notes
|
|
130
164
|
|
|
@@ -137,7 +171,9 @@ delegates visibility to your styles.
|
|
|
137
171
|
`enabled-class` is added only while available; `disabled-class` is added only while
|
|
138
172
|
unavailable. The component does not supply either class’s styles. Disconnection
|
|
139
173
|
cancels pending initialization, removes delegated listeners, and clears cached
|
|
140
|
-
references.
|
|
174
|
+
references. Repeated resets share one deferred evaluation after the browser
|
|
175
|
+
resets its controls; disconnection cancels that pending evaluation too.
|
|
176
|
+
Reconnection reevaluates the existing fields; disconnection alone
|
|
141
177
|
does not undo their last `hidden` or `disabled` state.
|
|
142
178
|
|
|
143
179
|
## Examples
|
package/custom-elements.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"customElement": true,
|
|
12
12
|
"name": "FormShowIfElement",
|
|
13
13
|
"tagName": "form-show-if",
|
|
14
|
-
"description": "Light-DOM conditional form fields. Unmatched managed fields are disabled and their wrapper is hidden unless disabled-class supplies author-controlled visibility. No colors, status strings, or focus changes are generated.
|
|
14
|
+
"description": "Light-DOM conditional form fields. Unmatched managed fields are disabled and their wrapper is hidden unless disabled-class supplies author-controlled visibility. No colors, status strings, or focus changes are generated. Live state-class changes remove old tokens and reconcile native hiding without replacing fields.",
|
|
15
15
|
"members": [
|
|
16
16
|
{
|
|
17
17
|
"kind": "field",
|
|
@@ -39,6 +39,29 @@
|
|
|
39
39
|
"type": { "text": "string | null" },
|
|
40
40
|
"default": "null",
|
|
41
41
|
"description": "A single authored class token applied when available. Alone, it does not replace native hiding when unavailable."
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"kind": "field",
|
|
45
|
+
"name": "isShown",
|
|
46
|
+
"readonly": true,
|
|
47
|
+
"type": { "text": "boolean" },
|
|
48
|
+
"default": "false",
|
|
49
|
+
"description": "Last evaluated logical condition match, not computed CSS visibility or validity. Initially false; retained while detached."
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"kind": "method",
|
|
53
|
+
"name": "refresh",
|
|
54
|
+
"return": { "type": { "text": "boolean" } },
|
|
55
|
+
"description": "Synchronously rediscover the native fields and form, clear condition caches, and return logical availability. Initial setup/reconnect are silent. Throws InvalidStateError when detached or NotFoundError without an eligible field."
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
"events": [
|
|
59
|
+
{
|
|
60
|
+
"name": "form-show-if:change",
|
|
61
|
+
"type": {
|
|
62
|
+
"text": "CustomEvent<FormShowIfChangeDetail>"
|
|
63
|
+
},
|
|
64
|
+
"description": "Bubbling, composed, non-cancelable notification after a real logical change commits native disabled/hidden/classes. Detail contains isShown and previousIsShown booleans only. Initial setup/reconnect are silent."
|
|
42
65
|
}
|
|
43
66
|
],
|
|
44
67
|
"attributes": [
|
package/define.d.ts
ADDED
package/form-show-if.d.ts
CHANGED
|
@@ -1,7 +1,55 @@
|
|
|
1
|
+
export interface FormShowIfChangeDetail {
|
|
2
|
+
readonly isShown: boolean;
|
|
3
|
+
readonly previousIsShown: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface FormShowIfEventMap extends HTMLElementEventMap {
|
|
7
|
+
'form-show-if:change': CustomEvent<FormShowIfChangeDetail>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Logical conditional availability, not computed CSS visibility. */
|
|
1
11
|
export class FormShowIfElement extends HTMLElement {
|
|
2
|
-
conditions: string | null;
|
|
3
|
-
|
|
4
|
-
|
|
12
|
+
get conditions(): string | null;
|
|
13
|
+
set conditions(value: string | null | undefined);
|
|
14
|
+
get enabledClass(): string | null;
|
|
15
|
+
set enabledClass(value: string | null | undefined);
|
|
16
|
+
get disabledClass(): string | null;
|
|
17
|
+
set disabledClass(value: string | null | undefined);
|
|
18
|
+
|
|
19
|
+
/** Last evaluated condition match; initially false and retained while detached. */
|
|
20
|
+
readonly isShown: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Rediscover fields/form and recompute synchronously; not visibility or validity.
|
|
23
|
+
* @throws {DOMException} InvalidStateError if detached; NotFoundError without an eligible field.
|
|
24
|
+
*/
|
|
25
|
+
refresh(): boolean;
|
|
26
|
+
|
|
27
|
+
addEventListener<K extends keyof FormShowIfEventMap>(
|
|
28
|
+
type: K,
|
|
29
|
+
listener: (
|
|
30
|
+
this: FormShowIfElement,
|
|
31
|
+
event: FormShowIfEventMap[K],
|
|
32
|
+
) => unknown,
|
|
33
|
+
options?: boolean | AddEventListenerOptions,
|
|
34
|
+
): void;
|
|
35
|
+
addEventListener(
|
|
36
|
+
type: string,
|
|
37
|
+
listener: EventListenerOrEventListenerObject | null,
|
|
38
|
+
options?: boolean | AddEventListenerOptions,
|
|
39
|
+
): void;
|
|
40
|
+
removeEventListener<K extends keyof FormShowIfEventMap>(
|
|
41
|
+
type: K,
|
|
42
|
+
listener: (
|
|
43
|
+
this: FormShowIfElement,
|
|
44
|
+
event: FormShowIfEventMap[K],
|
|
45
|
+
) => unknown,
|
|
46
|
+
options?: boolean | EventListenerOptions,
|
|
47
|
+
): void;
|
|
48
|
+
removeEventListener(
|
|
49
|
+
type: string,
|
|
50
|
+
listener: EventListenerOrEventListenerObject | null,
|
|
51
|
+
options?: boolean | EventListenerOptions,
|
|
52
|
+
): void;
|
|
5
53
|
}
|
|
6
54
|
|
|
7
55
|
declare global {
|
package/form-show-if.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/** @typedef {{ isShown: boolean, previousIsShown: boolean }} FormShowIfChangeDetail */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Conditional logical availability, not computed CSS visibility.
|
|
5
|
+
* @element form-show-if
|
|
6
|
+
* @fires {CustomEvent<FormShowIfChangeDetail>} form-show-if:change - Emitted after
|
|
7
|
+
* a logical change commits. Initial setup and reconnect are silent.
|
|
8
|
+
*/
|
|
1
9
|
export class FormShowIfElement extends HTMLElement {
|
|
2
10
|
static get observedAttributes() {
|
|
3
11
|
return ['conditions', 'disabled-class', 'enabled-class'];
|
|
@@ -11,12 +19,18 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
11
19
|
this.__$fields = [];
|
|
12
20
|
this.__conditions = [];
|
|
13
21
|
this.__fieldCache = Object.create(null);
|
|
14
|
-
this.__is_shown =
|
|
22
|
+
this.__is_shown = false;
|
|
23
|
+
this.__connectedReady = false;
|
|
24
|
+
this.__needsSync = false;
|
|
15
25
|
this.__disabledClass = null;
|
|
16
26
|
this.__enabledClass = null;
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
27
|
+
this.__appliedClassWrapper = null;
|
|
28
|
+
this.__appliedClassTokens = [];
|
|
29
|
+
this.__nativeHiddenWrapper = null;
|
|
30
|
+
this.__boundCheckIfShouldShow = () => this.__checkIfShouldShow();
|
|
31
|
+
this.__boundHandleReset = this.__handleReset.bind(this);
|
|
19
32
|
this.__rafId = null;
|
|
33
|
+
this.__resetRafId = null;
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
@@ -35,9 +49,6 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
35
49
|
this.__disabledClass = newValue || null;
|
|
36
50
|
if (this.isConnected && this.__$wrapper) {
|
|
37
51
|
this.__toggleClasses();
|
|
38
|
-
if (!this.__disabledClass && !this.__is_shown) {
|
|
39
|
-
this.__$wrapper.hidden = true;
|
|
40
|
-
}
|
|
41
52
|
}
|
|
42
53
|
break;
|
|
43
54
|
case 'enabled-class':
|
|
@@ -82,6 +93,46 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
82
93
|
}
|
|
83
94
|
}
|
|
84
95
|
|
|
96
|
+
/** Last evaluated logical availability; retained while detached. */
|
|
97
|
+
get isShown() {
|
|
98
|
+
return this.__is_shown;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Rediscover fields/form and synchronously recompute logical availability.
|
|
103
|
+
* @returns {boolean} Condition match, not computed visibility or validity.
|
|
104
|
+
* @throws {DOMException} InvalidStateError if detached; NotFoundError without a field.
|
|
105
|
+
*/
|
|
106
|
+
refresh() {
|
|
107
|
+
if (!this.isConnected) {
|
|
108
|
+
throw new DOMException(
|
|
109
|
+
'Connect form-show-if before calling refresh().',
|
|
110
|
+
'InvalidStateError',
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const field = this.__findField();
|
|
114
|
+
if (!field) {
|
|
115
|
+
throw new DOMException(
|
|
116
|
+
'form-show-if requires an input, select, or textarea.',
|
|
117
|
+
'NotFoundError',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const emitChanges = this.__connectedReady;
|
|
121
|
+
if (this.__rafId !== null) {
|
|
122
|
+
cancelAnimationFrame(this.__rafId);
|
|
123
|
+
this.__rafId = null;
|
|
124
|
+
}
|
|
125
|
+
this.__bindField(field);
|
|
126
|
+
this.__checkIfShouldShow(emitChanges);
|
|
127
|
+
return this.isShown;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
__findField() {
|
|
131
|
+
return this.querySelector(
|
|
132
|
+
'input:not([type=submit],[type=reset],[type=image],[type=button]),select,textarea',
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
85
136
|
__upgradeProperty(prop) {
|
|
86
137
|
if (Object.prototype.hasOwnProperty.call(this, prop)) {
|
|
87
138
|
const value = this[prop];
|
|
@@ -94,49 +145,65 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
94
145
|
this.__upgradeProperty('conditions');
|
|
95
146
|
this.__upgradeProperty('disabledClass');
|
|
96
147
|
this.__upgradeProperty('enabledClass');
|
|
97
|
-
|
|
148
|
+
this.__$form = this.closest('form') || document.body;
|
|
149
|
+
this.__addObservers();
|
|
98
150
|
this.__rafId = requestAnimationFrame(() => {
|
|
99
151
|
this.__rafId = null;
|
|
100
|
-
this.
|
|
101
|
-
this.__$field = this.querySelector(
|
|
102
|
-
'input:not([type=submit],[type=reset],[type=image],[type=button]),select,textarea',
|
|
103
|
-
);
|
|
104
|
-
this.__$form = this.closest('form') || document.body;
|
|
105
|
-
this.__$fields = this.__$field ? [this.__$field] : [];
|
|
106
|
-
if (!this.__$field) {
|
|
152
|
+
if (!this.isConnected) {
|
|
107
153
|
return;
|
|
108
154
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const conditionsAttr = this.conditions;
|
|
112
|
-
this.__conditions = conditionsAttr
|
|
113
|
-
? conditionsAttr.split('||')
|
|
114
|
-
: [];
|
|
115
|
-
|
|
116
|
-
this.__is_shown = null;
|
|
117
|
-
this.__disabledClass = this.disabledClass;
|
|
118
|
-
this.__enabledClass = this.enabledClass;
|
|
119
|
-
this.__fieldCache = Object.create(null);
|
|
120
|
-
|
|
121
|
-
// Cache bound methods to avoid creating new functions on each call
|
|
122
|
-
this.__boundCheckIfShouldShow = this.__checkIfShouldShow.bind(this);
|
|
123
|
-
this.__boundHandleReset = this.__handleReset.bind(this);
|
|
124
|
-
|
|
125
|
-
this.__init();
|
|
155
|
+
this.__bindField(this.__findField());
|
|
156
|
+
this.__checkIfShouldShow(false);
|
|
126
157
|
});
|
|
127
158
|
}
|
|
128
159
|
|
|
160
|
+
__bindField(field) {
|
|
161
|
+
const form = this.closest('form') || document.body;
|
|
162
|
+
const formChanged = form !== this.__$form;
|
|
163
|
+
// Keep same-form listeners stable when refresh runs inside reset dispatch.
|
|
164
|
+
if (formChanged) {
|
|
165
|
+
this.__removeObservers();
|
|
166
|
+
}
|
|
167
|
+
this.__$form = form;
|
|
168
|
+
this.__$field = field;
|
|
169
|
+
this.__$fields = field ? [field] : [];
|
|
170
|
+
this.__$wrapper = null;
|
|
171
|
+
this.__conditions = this.conditions ? this.conditions.split('||') : [];
|
|
172
|
+
this.__disabledClass = this.disabledClass || null;
|
|
173
|
+
this.__enabledClass = this.enabledClass || null;
|
|
174
|
+
this.__fieldCache = Object.create(null);
|
|
175
|
+
this.__needsSync = true;
|
|
176
|
+
this.__connectedReady = Boolean(field);
|
|
177
|
+
if (field) {
|
|
178
|
+
this.__determineWrapper();
|
|
179
|
+
this.__gatherSiblingFields();
|
|
180
|
+
}
|
|
181
|
+
if (formChanged) {
|
|
182
|
+
this.__addObservers();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
129
186
|
disconnectedCallback() {
|
|
130
187
|
// Clean up event listeners to prevent memory leaks
|
|
188
|
+
if (this.__resetRafId !== null) {
|
|
189
|
+
cancelAnimationFrame(this.__resetRafId);
|
|
190
|
+
this.__resetRafId = null;
|
|
191
|
+
}
|
|
131
192
|
if (this.__rafId !== null) {
|
|
132
193
|
cancelAnimationFrame(this.__rafId);
|
|
133
194
|
this.__rafId = null;
|
|
134
195
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
196
|
+
this.__removeObservers();
|
|
197
|
+
this.__$fields = [];
|
|
198
|
+
this.__$field = null;
|
|
199
|
+
this.__$form = null;
|
|
200
|
+
this.__$wrapper = null;
|
|
201
|
+
this.__fieldCache = Object.create(null);
|
|
202
|
+
this.__connectedReady = false;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
__removeObservers() {
|
|
206
|
+
if (this.__$form) {
|
|
140
207
|
// Only remove reset listener if we're attached to an actual form
|
|
141
208
|
if (this.__$form.tagName === 'FORM') {
|
|
142
209
|
this.__$form.removeEventListener(
|
|
@@ -156,11 +223,6 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
156
223
|
false,
|
|
157
224
|
);
|
|
158
225
|
}
|
|
159
|
-
this.__$fields = [];
|
|
160
|
-
this.__$field = null;
|
|
161
|
-
this.__$form = null;
|
|
162
|
-
this.__$wrapper = null;
|
|
163
|
-
this.__fieldCache = Object.create(null);
|
|
164
226
|
}
|
|
165
227
|
|
|
166
228
|
__addObservers() {
|
|
@@ -187,8 +249,11 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
187
249
|
}
|
|
188
250
|
|
|
189
251
|
__handleReset() {
|
|
190
|
-
|
|
191
|
-
|
|
252
|
+
if (this.__resetRafId !== null) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.__resetRafId = requestAnimationFrame(() => {
|
|
256
|
+
this.__resetRafId = null;
|
|
192
257
|
this.__checkIfShouldShow();
|
|
193
258
|
});
|
|
194
259
|
}
|
|
@@ -236,21 +301,49 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
236
301
|
}
|
|
237
302
|
}
|
|
238
303
|
__toggleClasses() {
|
|
304
|
+
if (
|
|
305
|
+
this.__nativeHiddenWrapper &&
|
|
306
|
+
this.__nativeHiddenWrapper !== this.__$wrapper
|
|
307
|
+
) {
|
|
308
|
+
this.__nativeHiddenWrapper.hidden = false;
|
|
309
|
+
this.__nativeHiddenWrapper = null;
|
|
310
|
+
}
|
|
311
|
+
const tokens = [this.__enabledClass, this.__disabledClass].filter(
|
|
312
|
+
Boolean,
|
|
313
|
+
);
|
|
314
|
+
for (const token of this.__appliedClassTokens) {
|
|
315
|
+
if (
|
|
316
|
+
this.__appliedClassWrapper !== this.__$wrapper ||
|
|
317
|
+
!tokens.includes(token)
|
|
318
|
+
) {
|
|
319
|
+
this.__appliedClassWrapper.classList.remove(token);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
239
322
|
this.__toggleEnabledClass();
|
|
240
323
|
this.__toggleDisabledClass();
|
|
324
|
+
if (this.__$wrapper) {
|
|
325
|
+
if (!this.__disabledClass) {
|
|
326
|
+
this.__$wrapper.hidden = !this.__is_shown;
|
|
327
|
+
this.__nativeHiddenWrapper = this.__is_shown
|
|
328
|
+
? null
|
|
329
|
+
: this.__$wrapper;
|
|
330
|
+
} else if (this.__nativeHiddenWrapper === this.__$wrapper) {
|
|
331
|
+
this.__$wrapper.hidden = false;
|
|
332
|
+
this.__nativeHiddenWrapper = null;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
this.__appliedClassWrapper = this.__$wrapper;
|
|
336
|
+
this.__appliedClassTokens = tokens;
|
|
241
337
|
}
|
|
242
338
|
|
|
243
339
|
// Show / Hide Logic
|
|
244
340
|
__showField() {
|
|
245
341
|
// Early return if already shown
|
|
246
|
-
if (this.__is_shown === true) {
|
|
342
|
+
if (this.__is_shown === true && !this.__needsSync) {
|
|
247
343
|
return;
|
|
248
344
|
}
|
|
249
345
|
this.__is_shown = true;
|
|
250
346
|
// Wrapper changes
|
|
251
|
-
if (!this.__disabledClass) {
|
|
252
|
-
this.__$wrapper.removeAttribute('hidden');
|
|
253
|
-
}
|
|
254
347
|
this.__toggleClasses();
|
|
255
348
|
// Enable field submission
|
|
256
349
|
this.__$fields.forEach(($field) => {
|
|
@@ -259,14 +352,11 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
259
352
|
}
|
|
260
353
|
__hideField() {
|
|
261
354
|
// Early return if already hidden
|
|
262
|
-
if (this.__is_shown === false) {
|
|
355
|
+
if (this.__is_shown === false && !this.__needsSync) {
|
|
263
356
|
return;
|
|
264
357
|
}
|
|
265
358
|
this.__is_shown = false;
|
|
266
359
|
// Wrapper changes
|
|
267
|
-
if (!this.__disabledClass && this.__$wrapper) {
|
|
268
|
-
this.__$wrapper.hidden = true;
|
|
269
|
-
}
|
|
270
360
|
this.__toggleClasses();
|
|
271
361
|
// Disable field submission
|
|
272
362
|
this.__$fields.forEach(($field) => {
|
|
@@ -274,8 +364,8 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
274
364
|
});
|
|
275
365
|
}
|
|
276
366
|
|
|
277
|
-
__checkIfShouldShow() {
|
|
278
|
-
if (!this.__$form) {
|
|
367
|
+
__checkIfShouldShow(emitChanges = this.__connectedReady) {
|
|
368
|
+
if (!this.__$form || !this.__$field) {
|
|
279
369
|
return;
|
|
280
370
|
}
|
|
281
371
|
let should_show = false;
|
|
@@ -296,19 +386,22 @@ export class FormShowIfElement extends HTMLElement {
|
|
|
296
386
|
}
|
|
297
387
|
}
|
|
298
388
|
|
|
299
|
-
|
|
389
|
+
const previousIsShown = this.isShown;
|
|
300
390
|
if (should_show) {
|
|
301
391
|
this.__showField();
|
|
302
392
|
} else {
|
|
303
393
|
this.__hideField();
|
|
304
394
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
395
|
+
this.__needsSync = false;
|
|
396
|
+
if (emitChanges && previousIsShown !== this.isShown) {
|
|
397
|
+
this.dispatchEvent(
|
|
398
|
+
new CustomEvent('form-show-if:change', {
|
|
399
|
+
detail: { isShown: this.isShown, previousIsShown },
|
|
400
|
+
bubbles: true,
|
|
401
|
+
composed: true,
|
|
402
|
+
}),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
312
405
|
}
|
|
313
406
|
|
|
314
407
|
__getConditionField(name) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aarongustafson/form-show-if",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.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",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"import": "./form-show-if.js"
|
|
39
39
|
},
|
|
40
40
|
"./define.js": {
|
|
41
|
+
"types": "./define.d.ts",
|
|
41
42
|
"import": "./define.js"
|
|
42
43
|
},
|
|
43
44
|
"./custom-elements.json": "./custom-elements.json"
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"form-show-if.js",
|
|
47
48
|
"form-show-if.d.ts",
|
|
48
49
|
"define.js",
|
|
50
|
+
"define.d.ts",
|
|
49
51
|
"index.js",
|
|
50
52
|
"custom-elements.json",
|
|
51
53
|
"README.md",
|
|
@@ -58,9 +60,11 @@
|
|
|
58
60
|
"test": "vitest",
|
|
59
61
|
"test:ui": "vitest --ui",
|
|
60
62
|
"test:run": "vitest run",
|
|
63
|
+
"test:types": "tsc --noEmit --strict --module nodenext --target es2022 --types node test/types.ts && tsc --noEmit --strict --module nodenext --target es2022 --types node test/registration-types.ts",
|
|
61
64
|
"test:coverage": "vitest run --coverage",
|
|
62
65
|
"test:browser": "playwright test",
|
|
63
|
-
"
|
|
66
|
+
"demo:check": "node scripts/check-demo-shared.js",
|
|
67
|
+
"lint": "npm run demo:check && npm run lint:eslint && npm run lint:prettier",
|
|
64
68
|
"format": "npm run format:eslint && npm run format:prettier",
|
|
65
69
|
"lint:eslint": "eslint .",
|
|
66
70
|
"format:eslint": "eslint . --fix",
|
|
@@ -70,16 +74,19 @@
|
|
|
70
74
|
"postversion": "git push --follow-tags"
|
|
71
75
|
},
|
|
72
76
|
"devDependencies": {
|
|
73
|
-
"@
|
|
77
|
+
"@aarongustafson/tabbed-interface": "3.2.0",
|
|
74
78
|
"@open-wc/eslint-config": "^13.1.0",
|
|
79
|
+
"@playwright/test": "^1.64.0",
|
|
75
80
|
"@testing-library/dom": "^10.4.2",
|
|
76
81
|
"@testing-library/user-event": "^14.6.7",
|
|
77
82
|
"@vitest/coverage-v8": "^5.0.5",
|
|
78
83
|
"@vitest/ui": "^5.0.5",
|
|
79
|
-
"eslint": "^10.
|
|
84
|
+
"eslint": "^10.15.0",
|
|
80
85
|
"eslint-config-prettier": "^10.1.8",
|
|
81
86
|
"happy-dom": "^21.0.4",
|
|
87
|
+
"microlighter": "2.2.0",
|
|
82
88
|
"prettier": "^3.9.9",
|
|
89
|
+
"typescript": "^7.0.2",
|
|
83
90
|
"vitest": "^5.0.5"
|
|
84
91
|
},
|
|
85
92
|
"publishConfig": {
|