Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { buildHrLeaveFormDefinition } from "~/utils/integrations/hrM05Forms";
|
||||
|
||||
import { InternalFormRenderer } from "./InternalFormRenderer";
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
|
||||
describe("InternalFormRenderer", () => {
|
||||
it("renders the M05 definition and submits typed values", () => {
|
||||
const onSubmit = vi.fn();
|
||||
const definition = buildHrLeaveFormDefinition((key) => key);
|
||||
const { container } = render(
|
||||
<InternalFormRenderer
|
||||
definition={definition}
|
||||
initialValues={{ requestKind: "vacation" }}
|
||||
submitting={false}
|
||||
onSubmit={onSubmit}
|
||||
uiId="ui.hr.test.m05"
|
||||
/>,
|
||||
);
|
||||
|
||||
fireEvent.change(container.querySelector("input[name='startDate']") as HTMLInputElement, {
|
||||
target: { value: "2026-07-28" },
|
||||
});
|
||||
fireEvent.change(container.querySelector("input[name='endDate']") as HTMLInputElement, {
|
||||
target: { value: "2026-07-30" },
|
||||
});
|
||||
fireEvent.click(container.querySelector('[data-ui-id="ui.hr.test.m05.submit"]') as HTMLElement);
|
||||
|
||||
expect(onSubmit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
requestKind: "vacation",
|
||||
startDate: "2026-07-28",
|
||||
endDate: "2026-07-30",
|
||||
}),
|
||||
);
|
||||
expect(container.querySelector("[data-m05-renderer='true']")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows a clear validation error instead of submitting incomplete values", () => {
|
||||
const onSubmit = vi.fn();
|
||||
const { container } = render(
|
||||
<InternalFormRenderer
|
||||
definition={buildHrLeaveFormDefinition((key) => key)}
|
||||
submitting={false}
|
||||
onSubmit={onSubmit}
|
||||
uiId="ui.hr.test.m05"
|
||||
/>,
|
||||
);
|
||||
|
||||
fireEvent.click(container.querySelector('[data-ui-id="ui.hr.test.m05.submit"]') as HTMLElement);
|
||||
|
||||
expect(onSubmit).not.toHaveBeenCalled();
|
||||
expect(screen.getByText("hr.m05.validation.FIELD_REQUIRED")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
filterVisibleFields,
|
||||
validateFormValues,
|
||||
type FormDefinition,
|
||||
type FormField,
|
||||
type FormSubmissionPayload,
|
||||
} from "@hivegrid/form-widget";
|
||||
import { Alert } from "~/ui/components/Alert";
|
||||
import { Button } from "~/ui/components/Button";
|
||||
import { Checkbox } from "~/ui/components/Checkbox";
|
||||
import { Select } from "~/ui/components/Select";
|
||||
import { TextArea } from "~/ui/components/TextArea";
|
||||
import { TextInput } from "~/ui/components/TextInput";
|
||||
import { DateField } from "~/components/screens/tasks/DateField";
|
||||
|
||||
export type InternalFormValues = FormSubmissionPayload["values"];
|
||||
|
||||
export type InternalFormRendererProps = {
|
||||
definition: FormDefinition;
|
||||
initialValues?: InternalFormValues;
|
||||
submitting: boolean;
|
||||
onSubmit: (values: InternalFormValues) => void;
|
||||
onCancel?: () => void;
|
||||
uiId: string;
|
||||
};
|
||||
|
||||
function initialValuesFor(
|
||||
definition: FormDefinition,
|
||||
supplied: InternalFormValues | undefined,
|
||||
): InternalFormValues {
|
||||
return Object.fromEntries(
|
||||
definition.fields.map((field) => [
|
||||
field.key,
|
||||
supplied?.[field.key] ??
|
||||
(field.type === "boolean" ? false : field.type === "multiselect" ? [] : ""),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function M05Field({
|
||||
field,
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
field: FormField;
|
||||
value: InternalFormValues[string];
|
||||
onChange: (value: InternalFormValues[string]) => void;
|
||||
}) {
|
||||
if (field.type === "honeypot") return null;
|
||||
if (field.type === "textarea") {
|
||||
return (
|
||||
<TextArea label={field.label} showLabel className="w-full">
|
||||
<TextArea.Input
|
||||
value={typeof value === "string" ? value : ""}
|
||||
required={field.required}
|
||||
maxLength={field.validationRules?.maxLength ?? field.maxLength}
|
||||
onChange={(event) => onChange(event.target.value)}
|
||||
/>
|
||||
</TextArea>
|
||||
);
|
||||
}
|
||||
if (field.type === "select") {
|
||||
return (
|
||||
<Select
|
||||
label={field.label}
|
||||
showLabel
|
||||
value={typeof value === "string" && value ? value : undefined}
|
||||
onValueChange={(next: string) => onChange(next)}
|
||||
>
|
||||
{(field.options ?? []).map((option) => (
|
||||
<Select.Item key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Select.Item>
|
||||
))}
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
if (field.type === "boolean") {
|
||||
return (
|
||||
<Checkbox
|
||||
label={field.label}
|
||||
checked={value === true}
|
||||
onCheckedChange={(checked) => onChange(checked)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (field.type === "date") {
|
||||
return (
|
||||
<DateField
|
||||
label={field.label}
|
||||
name={field.key}
|
||||
value={typeof value === "string" ? value : ""}
|
||||
required={field.required}
|
||||
onChange={onChange}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (field.type === "file" || field.type === "photo" || field.type === "multiselect") {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<TextInput label={field.label} showLabel fillContainer>
|
||||
<TextInput.Input
|
||||
type={
|
||||
field.type === "email"
|
||||
? "email"
|
||||
: field.type === "number"
|
||||
? "number"
|
||||
: field.type === "phone"
|
||||
? "tel"
|
||||
: "text"
|
||||
}
|
||||
value={typeof value === "string" || typeof value === "number" ? String(value) : ""}
|
||||
required={field.required}
|
||||
maxLength={field.validationRules?.maxLength ?? field.maxLength}
|
||||
onChange={(event) => onChange(event.target.value)}
|
||||
/>
|
||||
</TextInput>
|
||||
);
|
||||
}
|
||||
|
||||
export function InternalFormRenderer({
|
||||
definition,
|
||||
initialValues,
|
||||
submitting,
|
||||
onSubmit,
|
||||
onCancel,
|
||||
uiId,
|
||||
}: InternalFormRendererProps) {
|
||||
const { t } = useTranslation();
|
||||
const [values, setValues] = useState<InternalFormValues>(() =>
|
||||
initialValuesFor(definition, initialValues),
|
||||
);
|
||||
const [errorKey, setErrorKey] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
setValues(initialValuesFor(definition, initialValues));
|
||||
setErrorKey(null);
|
||||
}, [definition, initialValues]);
|
||||
|
||||
const visibleFields = useMemo(
|
||||
() => filterVisibleFields(definition.fields, values),
|
||||
[definition.fields, values],
|
||||
);
|
||||
|
||||
function submit() {
|
||||
const error = validateFormValues(values, definition);
|
||||
if (error) {
|
||||
setErrorKey(`hr.m05.validation.${error.code}`);
|
||||
return;
|
||||
}
|
||||
setErrorKey(null);
|
||||
onSubmit(values);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex w-full flex-col gap-5" data-ui-id={uiId} data-m05-renderer="true">
|
||||
{definition.description ? (
|
||||
<p className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
||||
{definition.description}
|
||||
</p>
|
||||
) : null}
|
||||
{errorKey ? <Alert variant="error" title={t(errorKey)} /> : null}
|
||||
<div className="flex w-full flex-col gap-4">
|
||||
{visibleFields.map((field) => (
|
||||
<M05Field
|
||||
key={field.key}
|
||||
field={field}
|
||||
value={values[field.key] ?? null}
|
||||
onChange={(value) =>
|
||||
setValues((current) => ({ ...current, [field.key]: value }))
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex justify-end gap-2">
|
||||
{onCancel ? (
|
||||
<Button variant="neutral" onClick={onCancel}>
|
||||
{t("hr.action.cancel")}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button loading={submitting} onClick={submit} data-ui-id={`${uiId}.submit`}>
|
||||
{definition.submitLabel}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user