-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexclusion_rules.js
More file actions
45 lines (43 loc) · 1.68 KB
/
exclusion_rules.js
File metadata and controls
45 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function areConditionsMet (rule, propValues) {
return Object.entries(rule.when).every(([prop, condition]) =>
typeof condition === 'function'
? condition(propValues[prop])
: condition === propValues[prop],
);
}
/**
* Determines if a member should be excluded (hidden) based on exclusion rules
* and the current prop values.
*
* @param {string} memberName - The name of the member to check.
* @param {string} memberGroup - The member group ('props' or 'slots').
* @param {Array} exclusionRules - Array of exclusion rule objects.
* @param {object} propValues - Current prop values.
* @returns {boolean} Whether the member should be excluded.
*/
export function shouldExclude (memberName, memberGroup, exclusionRules, propValues) {
if (!exclusionRules?.length) return false;
return exclusionRules.some(rule => {
if (!areConditionsMet(rule, propValues)) return false;
return rule.hide?.[memberGroup]?.includes(memberName) ?? false;
});
}
/**
* Collects disabled values for a specific prop based on exclusion rules
* and the current prop values.
*
* @param {string} propName - The prop to check for disabled values.
* @param {Array} exclusionRules - Array of exclusion rule objects.
* @param {object} propValues - Current prop values.
* @returns {Set} Set of disabled value strings.
*/
export function getDisabledValues (propName, exclusionRules, propValues) {
const disabled = new Set();
if (!exclusionRules?.length) return disabled;
for (const rule of exclusionRules) {
if (!areConditionsMet(rule, propValues)) continue;
const values = rule.disableValues?.props?.[propName];
if (values) values.forEach(v => disabled.add(String(v)));
}
return disabled;
}