@stimulus-library/mixins 1.3.1 → 1.6.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.
@@ -7,4 +7,15 @@ export declare function useDirtyFormTracking(controller: Controller, form: HTMLF
7
7
  restore: () => void;
8
8
  teardown: () => void;
9
9
  };
10
+ export declare function getElementValue(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): boolean | string;
11
+ export declare function getMultiSelectLoadValues(element: HTMLSelectElement): string[];
12
+ export declare function getMultiSelectValues(element: HTMLSelectElement): string[];
13
+ export declare function getElementLoadValue(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): boolean | string;
14
+ export declare function elementHasCachedLoadValue(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): boolean;
15
+ export declare function isElementDirty(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): boolean;
16
+ export declare function restoreElementFromLoadValue(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): void;
17
+ export declare function restoreMultiSelect(element: HTMLSelectElement, cacheValue: string): void;
18
+ export declare function cacheLoadValues(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): void;
19
+ export declare function resetCachedLoadValues(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): void;
20
+ export declare function resetFormLoadValues(form: HTMLFormElement): void;
10
21
  export declare function isDirty(element: HTMLElement): boolean;
@@ -60,7 +60,34 @@ export function useDirtyFormTracking(controller, form) {
60
60
  teardown,
61
61
  };
62
62
  }
63
- function getElementValue(element) {
63
+ function checkDirty(element) {
64
+ var _a;
65
+ if (isHTMLInputElement(element) && element.type == "radio") {
66
+ getOtherRadiosInGroup(element).forEach((radio) => radio.removeAttribute("data-dirty"));
67
+ }
68
+ if (isElementDirty(element)) {
69
+ element.setAttribute("data-dirty", "true");
70
+ (_a = element.form) === null || _a === void 0 ? void 0 : _a.setAttribute("data-dirty", "true");
71
+ element.dispatchEvent(new CustomEvent("input-dirtied", {
72
+ bubbles: true,
73
+ cancelable: true,
74
+ detail: {
75
+ target: element,
76
+ },
77
+ }));
78
+ }
79
+ else {
80
+ element.removeAttribute("data-dirty");
81
+ element.dispatchEvent(new CustomEvent("input-cleaned", {
82
+ bubbles: true,
83
+ cancelable: true,
84
+ detail: {
85
+ target: element,
86
+ },
87
+ }));
88
+ }
89
+ }
90
+ export function getElementValue(element) {
64
91
  if (isElementCheckable(element)) {
65
92
  return element.checked;
66
93
  }
@@ -71,16 +98,16 @@ function getElementValue(element) {
71
98
  return element.value;
72
99
  }
73
100
  }
74
- function getMultiSelectLoadValues(element) {
101
+ export function getMultiSelectLoadValues(element) {
75
102
  let options = Array.from(element.options);
76
103
  options = options.filter(option => option.defaultSelected);
77
104
  return options.map(option => option.value);
78
105
  }
79
- function getMultiSelectValues(element) {
106
+ export function getMultiSelectValues(element) {
80
107
  let options = Array.from(element.selectedOptions);
81
108
  return options.map(option => option.value);
82
109
  }
83
- function getElementLoadValue(element) {
110
+ export function getElementLoadValue(element) {
84
111
  const value = element.getAttribute(CACHE_ATTR_NAME);
85
112
  if (isElementCheckable(element)) {
86
113
  return value == null ? element.defaultChecked : value == "true";
@@ -101,40 +128,13 @@ function getElementLoadValue(element) {
101
128
  }
102
129
  return value;
103
130
  }
104
- function elementHasCachedLoadValue(element) {
131
+ export function elementHasCachedLoadValue(element) {
105
132
  return element.hasAttribute(CACHE_ATTR_NAME);
106
133
  }
107
- function checkDirty(element) {
108
- var _a;
109
- if (isHTMLInputElement(element) && element.type == "radio") {
110
- getOtherRadiosInGroup(element).forEach((radio) => radio.removeAttribute("data-dirty"));
111
- }
112
- if (isElementDirty(element)) {
113
- element.setAttribute("data-dirty", "true");
114
- (_a = element.form) === null || _a === void 0 ? void 0 : _a.setAttribute("data-dirty", "true");
115
- element.dispatchEvent(new CustomEvent("input-dirtied", {
116
- bubbles: true,
117
- cancelable: true,
118
- detail: {
119
- target: element,
120
- },
121
- }));
122
- }
123
- else {
124
- element.removeAttribute("data-dirty");
125
- element.dispatchEvent(new CustomEvent("input-cleaned", {
126
- bubbles: true,
127
- cancelable: true,
128
- detail: {
129
- target: element,
130
- },
131
- }));
132
- }
133
- }
134
- function isElementDirty(element) {
134
+ export function isElementDirty(element) {
135
135
  return getElementValue(element) !== getElementLoadValue(element);
136
136
  }
137
- function restoreElementFromLoadValue(element) {
137
+ export function restoreElementFromLoadValue(element) {
138
138
  const cacheValue = element.getAttribute(CACHE_ATTR_NAME);
139
139
  if (isElementCheckable(element)) {
140
140
  element.setAttribute(CACHE_ATTR_NAME, element.checked.toString());
@@ -158,13 +158,15 @@ function restoreElementFromLoadValue(element) {
158
158
  }
159
159
  checkDirty(element);
160
160
  }
161
- function restoreMultiSelect(element, cacheValue) {
161
+ export function restoreMultiSelect(element, cacheValue) {
162
162
  let selectedOptions = JSON.parse(cacheValue);
163
163
  Array.from(element.options).forEach((option) => option.selected = selectedOptions.includes(option.value));
164
164
  }
165
- function cacheLoadValues(element) {
166
- if (!elementHasCachedLoadValue(element) && isElementCheckable(element)) {
167
- element.setAttribute(CACHE_ATTR_NAME, element.checked.toString());
165
+ export function cacheLoadValues(element) {
166
+ if (isElementCheckable(element)) {
167
+ if (!elementHasCachedLoadValue(element)) {
168
+ element.setAttribute(CACHE_ATTR_NAME, element.checked.toString());
169
+ }
168
170
  }
169
171
  else if (isHTMLSelectElement(element) && element.multiple) {
170
172
  element.setAttribute(CACHE_ATTR_NAME, JSON.stringify(getMultiSelectLoadValues(element)));
@@ -173,6 +175,19 @@ function cacheLoadValues(element) {
173
175
  element.setAttribute(CACHE_ATTR_NAME, element.value.toString());
174
176
  }
175
177
  }
178
+ export function resetCachedLoadValues(element) {
179
+ if (elementHasCachedLoadValue(element)) {
180
+ element.removeAttribute(CACHE_ATTR_NAME);
181
+ }
182
+ cacheLoadValues(element);
183
+ }
184
+ export function resetFormLoadValues(form) {
185
+ form.querySelectorAll("input, select, textarea").forEach((element) => {
186
+ const el = element;
187
+ resetCachedLoadValues(el);
188
+ checkDirty(el);
189
+ });
190
+ }
176
191
  export function isDirty(element) {
177
192
  return element.hasAttribute("data-dirty");
178
193
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "ruby on rails",
10
10
  "ruby-on-rails"
11
11
  ],
12
- "version": "1.3.1",
12
+ "version": "1.6.0",
13
13
  "license": "MIT",
14
14
  "author": {
15
15
  "name": "Sub-Xaero",
@@ -37,26 +37,26 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@hotwired/stimulus": "^3.0.0",
40
- "@stimulus-library/utilities": "^1.3.1"
40
+ "@stimulus-library/utilities": "^1.6.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/chai": "^5.0.1",
44
44
  "@types/mocha": "^10.0.1",
45
- "@types/node": "^22.13.17",
46
- "@types/sinon": "^17.0.1",
45
+ "@types/node": "^25.3.5",
46
+ "@types/sinon": "^21.0.0",
47
47
  "@types/sinon-chai": "^4.0.0",
48
48
  "agadoo": "^3.0.0",
49
- "chai": "^5.1.1",
49
+ "chai": "^6.2.2",
50
50
  "fast-glob": "^3.2.12",
51
- "lerna": "^8.0.0",
51
+ "lerna": "^9.0.4",
52
52
  "mocha": "^11.1.0",
53
53
  "rimraf": "^6.0.1",
54
- "sinon": "^20.0.0",
54
+ "sinon": "^21.0.0",
55
55
  "sinon-chai": "^4.0.0",
56
56
  "standard-version": "^9.5.0",
57
57
  "ts-node": "^10.9.2",
58
58
  "typescript": "^5.8.2",
59
- "vite": "^6.2.4"
59
+ "vite": "^7.0.2"
60
60
  },
61
- "gitHead": "0e40d14436b1c5541e9ad6d73553041f9c60cceb"
61
+ "gitHead": "19c221973f11d4a44579367a67636d46e47058ef"
62
62
  }