feat: min max chars for long text (#15691)

* feat: min max chars for long text

* refactor: remove console log

* refactor: revert redundant change

* refactor: review changes

* fix: min max validation

* fix: fix typo

* refactor: update comments

* refactor: review changes

* refactor: review changes

* test: Add test

* Make maxLength more configurable per field type

* Reduce maxLength to 1000

* Update update-event-type.input.ts

* revert changes in main

* Update update-event-type.input.ts

* refactor get-event-type-public.output.ts

* add e2e tests

* add min max char common.json

* Update FormBuilder.tsx

* refactor changes

* Update fieldTypes.ts

* Update schema.ts

* add min max length in api-request

* Update booking-fields.input.ts

* fix e2e tests

* move min-max-char tests to manage-booking-questions.e2e.ts

* remove test

* reorder imports to fix lint

* fix lint

* fix e2e tests

* Abstract out SupportsLengthcheck type Field

* Revert newline change

* Improve the schema validation a bit and fix undefined in the message issue

* revert: v2 API changes

---------

Co-authored-by: Ayush Mhetre <[email protected]>
Co-authored-by: Hariom <[email protected]>
Co-authored-by: supalarry <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-07-30 12:21:27 +00:00
1 parent 40df4cf511
commit b8ba95dbb0
6 files changed
+288 -3

No files matched your search

+69 -1
View File
@@ -41,6 +41,10 @@ type RhfFormFields = RhfForm["fields"];
type RhfFormField = RhfFormFields[number];
function getCurrentFieldType(fieldForm: UseFormReturn<RhfFormField>) {
return fieldTypesConfigMap[fieldForm.watch("type") || "text"];
}
/**
* It works with a react-hook-form only.
* `formProp` specifies the name of the property in the react-hook-form that has the fields. This is where fields would be updated.
@@ -433,7 +437,7 @@ function FieldEditDialog({
fieldForm.setValue("variantsConfig", variantsConfig);
}, [fieldForm]);
const isFieldEditMode = !!dialog.data;
const fieldType = fieldTypesConfigMap[fieldForm.watch("type") || "text"];
const fieldType = getCurrentFieldType(fieldForm);
const variantsConfig = fieldForm.watch("variantsConfig");
@@ -493,6 +497,7 @@ function FieldEditDialog({
containerClassName="mt-6"
label={t("label")}
/>
{fieldType?.isTextType ? (
<InputField
{...fieldForm.register("placeholder")}
@@ -509,6 +514,11 @@ function FieldEditDialog({
}}
/>
) : null}
{!!fieldType?.supportsLengthCheck ? (
<FieldWithLengthCheckSupport containerClassName="mt-6" fieldForm={fieldForm} />
) : null}
<Controller
name="required"
control={fieldForm.control}
@@ -550,6 +560,64 @@ function FieldEditDialog({
);
}
function FieldWithLengthCheckSupport({
fieldForm,
containerClassName = "",
className,
...rest
}: {
fieldForm: UseFormReturn<RhfFormField>;
containerClassName?: string;
} & React.HTMLAttributes<HTMLDivElement>) {
const { t } = useLocale();
const fieldType = getCurrentFieldType(fieldForm);
if (!fieldType.supportsLengthCheck) {
return null;
}
const supportsLengthCheck = fieldType.supportsLengthCheck;
const maxAllowedMaxLength = supportsLengthCheck.maxLength;
return (
<div className={classNames("grid grid-cols-2 gap-4", className)} {...rest}>
<InputField
{...fieldForm.register("minLength", {
valueAsNumber: true,
})}
defaultValue={0}
containerClassName={containerClassName}
label={t("min_characters")}
type="number"
onChange={(e) => {
fieldForm.setValue("minLength", parseInt(e.target.value ?? 0));
// Ensure that maxLength field adjusts its restrictions
fieldForm.trigger("maxLength");
}}
min={0}
max={fieldForm.getValues("maxLength") || maxAllowedMaxLength}
/>
<InputField
{...fieldForm.register("maxLength", {
valueAsNumber: true,
})}
defaultValue={maxAllowedMaxLength}
containerClassName={containerClassName}
label={t("max_characters")}
type="number"
onChange={(e) => {
if (!supportsLengthCheck) {
return;
}
fieldForm.setValue("maxLength", parseInt(e.target.value ?? maxAllowedMaxLength));
// Ensure that minLength field adjusts its restrictions
fieldForm.trigger("minLength");
}}
min={fieldForm.getValues("minLength") || 0}
max={maxAllowedMaxLength}
/>
</div>
);
}
/**
* Shows the label of the field, taking into account the current variant selected
*/
@@ -230,6 +230,8 @@ export const ComponentForField = ({
<WithLabel field={field} readOnly={readOnly}>
<componentConfig.factory
placeholder={field.placeholder}
minLength={field.minLength}
maxLength={field.maxLength}
name={field.name}
label={field.label}
readOnly={readOnly}
@@ -104,6 +104,10 @@ const configMap: Record<FieldType, Omit<z.infer<typeof fieldTypeConfigSchema>, "
label: "Long Text",
value: "textarea",
isTextType: true,
supportsLengthCheck: {
// Keep it as small as possible. It is easier to change to a higher value but coming back to a lower value(due to any reason) would be problematic for users who have saved higher value.
maxLength: 1000,
},
},
select: {
label: "Select",
+52 -2
View File
@@ -83,6 +83,21 @@ const baseFieldSchema = z.object({
})
)
.optional(),
/**
* It is the minimum number of characters that can be entered in the field.
* It is used for types with `supportsLengthCheck= true`.
* @default 0
* @requires supportsLengthCheck = true
*/
minLength: z.number().optional(),
/**
* It is the maximum number of characters that can be entered in the field.
* It is used for types with `supportsLengthCheck= true`.
* @requires supportsLengthCheck = true
*/
maxLength: z.number().optional(),
});
export const variantsConfigSchema = z.object({
@@ -116,6 +131,11 @@ export const fieldTypeConfigSchema = z
isTextType: z.boolean().default(false).optional(),
systemOnly: z.boolean().default(false).optional(),
needsOptions: z.boolean().default(false).optional(),
supportsLengthCheck: z
.object({
maxLength: z.number(),
})
.optional(),
propsType: z.enum([
"text",
"textList",
@@ -233,7 +253,7 @@ export const fieldTypesSchemaMap: Partial<
*/
preprocess: (data: {
field: z.infer<typeof fieldSchema>;
response: any;
response: string;
isPartialSchema: boolean;
}) => unknown;
/**
@@ -243,7 +263,7 @@ export const fieldTypesSchemaMap: Partial<
*/
superRefine: (data: {
field: z.infer<typeof fieldSchema>;
response: any;
response: string;
isPartialSchema: boolean;
ctx: z.RefinementCtx;
m: (key: string) => string;
@@ -318,6 +338,36 @@ export const fieldTypesSchemaMap: Partial<
});
},
},
textarea: {
preprocess: ({ response }) => {
return response.trim();
},
superRefine: ({ field, response, ctx, m }) => {
const fieldTypeConfig = fieldTypesConfigMap[field.type];
const value = response ?? "";
const maxLength = field.maxLength ?? fieldTypeConfig.supportsLengthCheck?.maxLength;
const minLength = field.minLength ?? 0;
if (!maxLength) {
throw new Error("maxLength must be there for textarea field");
}
const hasExceededMaxLength = value.length > maxLength;
const hasNotReachedMinLength = value.length < minLength;
if (hasExceededMaxLength) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: m(`Max. ${maxLength} characters allowed`),
});
return;
}
if (hasNotReachedMinLength) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: m(`Min. ${minLength} characters required`),
});
return;
}
},
},
};
/**