Skip to content

Commit 36d3838

Browse files
committed
Add fieldset/util and tests
1 parent 4797bfd commit 36d3838

File tree

4 files changed

+411
-2
lines changed

4 files changed

+411
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"./request-processing": "./src/request-processing.js",
1414
"./plugins/mrujs": "./src/plugins/mrujs.js",
1515
"./utils": "./src/util.js",
16-
"./fieldset/rendering": "./src/fieldset/rendering.js"
16+
"./fieldset/rendering": "./src/fieldset/rendering.js",
17+
"./fieldset/util": "./src/fieldset/util.js"
1718
},
1819
"scripts": {
1920
"start": "web-dev-server --open demo/ --node-resolve",

src/fieldset/util.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
skipChangeValidation,
3+
skipFocusoutValidation,
4+
skipValidation
5+
} from "../element-utilities.js"
6+
7+
/**
8+
* Checks that there is at least N values in the `FormData` for the given
9+
* `fieldName` in the `fieldset`'s form.
10+
* @param {FieldsetElement} fieldset
11+
* @param {string} fieldName
12+
* @param {integer} minimum
13+
* @return {boolean} if there is at least 1 record for `fieldName` in the fieldset form's `FormData`
14+
*/
15+
export function minimumCountOfFieldNameSelected(fieldset, fieldName, minimum) {
16+
const formData = new FormData(fieldset.form)
17+
return formData.getAll(fieldName).length >= minimum
18+
}
19+
20+
/**
21+
* Helper method to check if the the `ChangeEvent` is inside of
22+
* its target.
23+
*
24+
* @param {ChangeEvent} event
25+
* @return {boolean}
26+
*/
27+
export function isChangeEventForTarget(event) {
28+
return event.currentTarget == event.target
29+
}
30+
31+
/**
32+
* Helper method to check that the the `FocusEvent` is inside of
33+
* its target.
34+
*
35+
* @param {FocusEvent} event
36+
* @return {boolean}
37+
*/
38+
export function isFocusEventInsideTarget(event) {
39+
return event.currentTarget.contains(event.relatedTarget)
40+
}
41+
42+
/**
43+
* Helper method to check if the `change` event should be skipped by checking:
44+
* - {@link isChangeEventForTarget}
45+
* - {@link skipChangeValidation}
46+
* - {@link skipValidation}
47+
*
48+
* @param {Event} event
49+
* @returns {boolean} true if the `change` event should be skipped
50+
*/
51+
export function skipFieldsetChangeEvent(event) {
52+
if(isChangeEventForTarget(event)){ return true }
53+
54+
const element = event.currentTarget
55+
if(skipChangeValidation(element)){ return true }
56+
if(skipValidation(element)){ return true }
57+
58+
return false
59+
}
60+
61+
/**
62+
* Helper method to check if the `focousout` event should be skipped by checking:
63+
* - {@link isFocusEventInsideTarget}
64+
* - {@link skipFocusoutValidation}
65+
* - {@link skipValidation}
66+
*
67+
* @param {Event} event
68+
* @returns {boolean} true if the `focousout` event should be skipped
69+
*/
70+
export function skipFieldsetFocusoutEvent(event) {
71+
if(isFocusEventInsideTarget(event)){ return true}
72+
73+
const element = event.currentTarget
74+
if(skipFocusoutValidation(element)){ return true }
75+
if(skipValidation(element)){ return true }
76+
77+
return false
78+
}

0 commit comments

Comments
 (0)