Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import type { ConditionalLogicRule, FormField } from "./schema.js";
|
||||
|
||||
export type ConditionalValues = Readonly<Record<string, unknown>>;
|
||||
|
||||
function normalized(value: unknown): string {
|
||||
if (value === null || value === undefined) return "";
|
||||
if (Array.isArray(value)) return value.map((item) => normalized(item)).join(",");
|
||||
return String(value).trim().toLowerCase();
|
||||
}
|
||||
|
||||
function isFilled(value: unknown): boolean {
|
||||
if (Array.isArray(value)) return value.length > 0;
|
||||
return value !== undefined && value !== null && value !== "" && value !== false;
|
||||
}
|
||||
|
||||
export function evaluateConditionalLogic(rule: ConditionalLogicRule, values: ConditionalValues): boolean {
|
||||
const actual = values[rule.fieldKey];
|
||||
switch (rule.operator) {
|
||||
case "equals":
|
||||
return normalized(actual) === normalized(rule.value);
|
||||
case "not_equals":
|
||||
return normalized(actual) !== normalized(rule.value);
|
||||
case "contains":
|
||||
return normalized(actual).includes(normalized(rule.value));
|
||||
case "not_empty":
|
||||
return isFilled(actual);
|
||||
case "empty":
|
||||
return !isFilled(actual);
|
||||
case "in": {
|
||||
const expected = Array.isArray(rule.value) ? rule.value.map((item) => normalized(item)) : [normalized(rule.value)];
|
||||
return expected.includes(normalized(actual));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isFieldVisible(field: Pick<FormField, "conditionalLogic">, values: ConditionalValues): boolean {
|
||||
const rules = field.conditionalLogic ?? [];
|
||||
return rules.length === 0 || rules.every((rule) => evaluateConditionalLogic(rule, values));
|
||||
}
|
||||
|
||||
export function filterVisibleFields<T extends Pick<FormField, "conditionalLogic">>(fields: readonly T[], values: ConditionalValues): T[] {
|
||||
return fields.filter((field) => isFieldVisible(field, values));
|
||||
}
|
||||
Reference in new issue
Block a user