Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests to FormBuilder components #69

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/FormBuilder/fields/CheckboxField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "#/components/ui/Form";

import { BaseField, withConditional } from "../fields";
import isFieldDisabled from "../isFieldDisabled";

export interface CheckboxFieldProps extends BaseField {
type: "checkbox";
Expand All @@ -31,6 +32,7 @@ export const CheckboxField = withConditional<CheckboxFieldProps>(
<Checkbox
checked={formField.value}
onCheckedChange={formField.onChange}
disabled={isFieldDisabled(form, field)}
/>
</FormControl>
<div className="space-y-1 leading-none">
Expand Down
1 change: 1 addition & 0 deletions src/components/FormBuilder/fields/DatePickerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const DatePickerInput = withConditional<DatePickerInputProps>(
control={form.control}
name={field.name}
rules={field.required ? { required: true } : undefined}
defaultValue={field.defaultValue}
render={({ field: formField }) => (
<FormItem className="flex flex-col">
<FormLabel tooltip={field.tooltip}>{field.label}</FormLabel>
Expand Down
13 changes: 6 additions & 7 deletions src/components/FormBuilder/fields/FieldArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ export const FieldArray = withConditional<FieldArrayFieldProps>(
<DragDropContext onDragEnd={handleDrag}>
<ul>
<StrictModeDroppable droppableId={`${field.name}-items`}>
{/* eslint-disable-next-line no-shadow, @typescript-eslint/no-unused-vars */}
{(provided, snapshot) => (
{(provided) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
{fields.map((rhfField, index) => {
// @ts-expect-error
Expand All @@ -139,11 +138,10 @@ export const FieldArray = withConditional<FieldArrayFieldProps>(
draggableId={`item-${index}`}
index={index}
>
{/* eslint-disable-next-line no-shadow, @typescript-eslint/no-unused-vars */}
{(provided, snapshot) => (
{(innerProvided) => (
<li
ref={provided.innerRef}
{...provided.draggableProps}
ref={innerProvided.innerRef}
{...innerProvided.draggableProps}
className="mb-3"
>
<div
Expand All @@ -152,7 +150,7 @@ export const FieldArray = withConditional<FieldArrayFieldProps>(
{field.hasSequence && (
<div
className="flex h-full w-6 flex-col justify-center"
{...provided.dragHandleProps}
{...innerProvided.dragHandleProps}
>
<MoveIcon className="size-6 self-center" />
</div>
Expand All @@ -167,6 +165,7 @@ export const FieldArray = withConditional<FieldArrayFieldProps>(
// eslint-disable-next-line jsx-a11y/control-has-associated-label
<button
type="button"
data-testid="remove-button"
onClick={() => handleRemove(Number(index))}
className="mt-4"
>
Expand Down
6 changes: 3 additions & 3 deletions src/components/FormBuilder/fields/HiddenField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { FormControl, FormField } from "#/components/ui/Form";
import { FormField, FormItem } from "#/components/ui/Form";

import { withConditional } from "../fields";

Expand All @@ -9,9 +9,9 @@ export const HiddenField = withConditional(({ form, field }) => (
name={field.name}
defaultValue={field.value}
render={({ field: formField }) => (
<FormControl>
<FormItem>
<input type="hidden" {...formField} />
</FormControl>
</FormItem>
)}
/>
));
1 change: 1 addition & 0 deletions src/components/FormBuilder/fields/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const InputField = withConditional<InputFieldProps>(
}
: undefined
}
defaultValue={field.defaultValue}
render={({ field: formField }) => (
<FormItem className="w-full">
<FormLabel tooltip={field.tooltip}>{field.label}</FormLabel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
import { withConditional } from "../fields";
import { SelectFieldProps } from "./selects/SelectField";

export interface MultiSelectCheckboxesField extends SelectFieldProps {
export interface MultiSelectCheckboxesFieldProps extends SelectFieldProps {
type: "multi_select_checkbox";
}

export const MultiSelectCheckboxes =
withConditional<MultiSelectCheckboxesField>(({ form, field }) => (
withConditional<MultiSelectCheckboxesFieldProps>(({ form, field }) => (
<FormField
control={form.control}
name={field.name}
Expand Down
2 changes: 2 additions & 0 deletions src/components/FormBuilder/fields/TextAreaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Textarea } from "#/components/ui/Textarea";

import { BaseField, withConditional } from "../fields";
import isFieldDisabled from "../isFieldDisabled";

export interface TextAreaFieldProps extends BaseField {
length?: {
Expand Down Expand Up @@ -47,6 +48,7 @@ export const TextAreaField = withConditional<TextAreaFieldProps>(
<FormDescription>{field.description}</FormDescription>
<FormControl>
<Textarea
disabled={isFieldDisabled(form, field)}
placeholder={field.placeholder}
className="resize-none"
{...formField}
Expand Down
80 changes: 37 additions & 43 deletions src/components/FormBuilder/fields/selects/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import * as Select from "#/components/ui/Select";

import { BaseField, withConditional } from "../../fields";
import isFieldDisabled from "../../isFieldDisabled";

export interface SelectFieldProps extends BaseField {
options?: Array<{
Expand All @@ -20,48 +21,41 @@ export interface SelectFieldProps extends BaseField {
}

export const SelectField = withConditional<SelectFieldProps>(
({ form, field }) => {
const disabled =
typeof field.disabled === "function"
? field.disabled(form.getValues())
: field.disabled;
({ form, field }) => (
<FormField
control={form.control}
name={field.name}
rules={field.required ? { required: true } : undefined}
render={({ field: formField }) => (
<FormItem className="w-full">
<FormLabel tooltip={field.tooltip}>{field.label}</FormLabel>
<FormDescription>{field.description}</FormDescription>
<Select.SelectRoot
onValueChange={formField.onChange}
defaultValue={String(formField.value)}
name={field.name}
disabled={isFieldDisabled(form, field)}
>
<FormControl>
<Select.SelectTrigger className="h-10 w-full rounded-md border dark:border-2 shadow-none">
<Select.SelectValue placeholder={field.placeholder} />
</Select.SelectTrigger>
</FormControl>
<Select.SelectContent className="z-[10000]">
{field.options?.map((option) => (
<Select.SelectItem
key={String(option.value)}
value={String(option.value)}
>
{option.label}
</Select.SelectItem>
))}
</Select.SelectContent>
</Select.SelectRoot>

return (
<FormField
control={form.control}
name={field.name}
rules={field.required ? { required: true } : undefined}
render={({ field: formField }) => (
<FormItem className="w-full">
<FormLabel tooltip={field.tooltip}>{field.label}</FormLabel>
<FormDescription>{field.description}</FormDescription>
<Select.SelectRoot
onValueChange={formField.onChange}
defaultValue={String(formField.value)}
name={field.name}
disabled={disabled}
>
<FormControl>
<Select.SelectTrigger className="h-10 w-full rounded-md border dark:border-2 shadow-none">
<Select.SelectValue placeholder={field.placeholder} />
</Select.SelectTrigger>
</FormControl>
<Select.SelectContent className="z-[10000]">
{field.options?.map((option) => (
<Select.SelectItem
key={String(option.value)}
value={String(option.value)}
>
{option.label}
</Select.SelectItem>
))}
</Select.SelectContent>
</Select.SelectRoot>

<FormMessage />
</FormItem>
)}
/>
);
}
<FormMessage />
</FormItem>
)}
/>
)
);
12 changes: 12 additions & 0 deletions src/components/FormBuilder/isFieldDisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CommonFieldProps, FormFieldProps } from "./fields";

type FieldProps = CommonFieldProps<FormFieldProps>;

export default function isFieldDisabled(
form: FieldProps["form"],
field: FieldProps["field"]
) {
return typeof field.disabled === "function"
? field.disabled(form.getValues())
: field.disabled;
}
5 changes: 4 additions & 1 deletion src/components/RichTextEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import React, { Suspense, lazy } from "react";
import { Trans, useTranslation } from "react-i18next";
import { useTheme } from "#/components/ThemeToggle/context";

const JoditEditor = lazy(() => import("jodit-react"));
const JoditEditor = lazy(async () => {
const module = await import("jodit-react");
return { default: module.default };
});

const EDITOR_BUTTONS = [
"undo",
Expand Down
Loading