fix: ui and ux improvements in booking questions (#7734)

This commit is contained in:
Udit Takkar authored and GitHub committed 2023-04-06 08:17:53 +00:00
1 parent 7b5e2c8bab
commit a363b1e903
16 files changed
+183 -100

No files matched your search

@@ -191,9 +191,9 @@ export const ensureBookingInputsHaveSystemFields = ({
type: "radioInput",
name: "location",
editable: "system",
hideWhenJustOneOption: true,
required: false,
// Populated on the fly from locations. I don't want to duplicate storing locations and instead would like to be able to refer to locations in eventType.
// options: `eventType.locations`
getOptionsAt: "locations",
optionsInputs: {
attendeeInPerson: {
type: "address",
@@ -121,12 +121,13 @@ function preprocess<T extends z.ZodType>({
const views = bookingField.views;
const isFieldApplicableToCurrentView =
currentView === "ALL_VIEWS" ? true : views ? views.find((view) => view.id === currentView) : true;
let hidden = bookingField.hidden;
const numOptions = bookingField.options?.length ?? 0;
if (bookingField.hideWhenJustOneOption) {
hidden = hidden || numOptions <= 1;
}
// If the field is hidden, then it can never be required
const isRequired = bookingField.hidden
? false
: isFieldApplicableToCurrentView
? bookingField.required
: false;
const isRequired = hidden ? false : isFieldApplicableToCurrentView ? bookingField.required : false;
if ((isPartialSchema || !isRequired) && value === undefined) {
return;
@@ -0,0 +1,29 @@
import type { LocationObject } from "@calcom/app-store/locations";
import { locationKeyToString } from "@calcom/app-store/locations";
import { getEventLocationType } from "@calcom/app-store/locations";
import type { useLocale } from "@calcom/lib/hooks/useLocale";
import notEmpty from "@calcom/lib/notEmpty";
export default function getLocationsOptionsForSelect(
locations: LocationObject[],
t: ReturnType<typeof useLocale>["t"]
) {
return locations
.map((location) => {
const eventLocation = getEventLocationType(location.type);
const locationString = locationKeyToString(location);
if (typeof locationString !== "string" || !eventLocation) {
// It's possible that location app got uninstalled
return null;
}
const type = eventLocation.type;
return {
label: t(locationString),
value: type,
inputPlaceholder: t(eventLocation?.attendeeInputPlaceholder || ""),
};
})
.filter(notEmpty);
}
+11 -4
View File
@@ -48,6 +48,7 @@ type Component =
value: { value: string; optionValue: string };
} & {
name?: string;
required?: boolean;
}
>(
props: TProps
@@ -323,8 +324,14 @@ export const Components: Record<BookingFieldType, Component> = {
})
) : (
// Show option itself as label because there is just one option
// TODO: Support asterisk for required fields
<Label>{options[0].label}</Label>
<>
<Label>
{options[0].label}
{!readOnly && optionsInputs[options[0].value]?.required ? (
<span className="ml-1 mb-1 text-sm font-medium leading-none dark:text-white">*</span>
) : null}
</Label>
</>
)}
</div>
</div>
@@ -342,10 +349,10 @@ export const Components: Record<BookingFieldType, Component> = {
name: "optionField",
}}
value={value?.optionValue}
setValue={(val: string) => {
setValue={(val: string | undefined) => {
setValue({
value: value?.value,
optionValue: val,
optionValue: val || "",
});
}}
/>
+82 -48
View File
@@ -44,11 +44,18 @@ export const FormBuilder = function FormBuilder({
description,
addFieldLabel,
formProp,
dataStore,
}: {
formProp: string;
title: string;
description: string;
addFieldLabel: string;
/**
* A readonly dataStore that is used to lookup the options for the fields. It works in conjunction with the field.getOptionAt property which acts as the key in options
*/
dataStore: {
options: Record<string, { label: string; value: string; inputPlaceholder?: string }[]>;
};
}) {
const FieldTypesMap: Record<
string,
@@ -112,6 +119,8 @@ export const FormBuilder = function FormBuilder({
value: "radioInput",
isTextType: false,
systemOnly: true,
// This is false currently because we don't want to show the options for Location field right now. It is the only field with type radioInput.
// needsOptions: true,
},
checkbox: {
label: "Checkbox Group",
@@ -148,13 +157,14 @@ export const FormBuilder = function FormBuilder({
function OptionsField({
label = "Options",
value,
onChange,
// eslint-disable-next-line @typescript-eslint/no-empty-function
onChange = () => {},
className = "",
readOnly = false,
}: {
label?: string;
value: { label: string; value: string }[];
onChange: (value: { label: string; value: string }[]) => void;
onChange?: (value: { label: string; value: string }[]) => void;
className?: string;
readOnly?: boolean;
}) {
@@ -257,6 +267,7 @@ export const FormBuilder = function FormBuilder({
const fieldType = FieldTypesMap[fieldForm.watch("type") || "text"];
const isFieldEditMode = fieldDialog.fieldIndex !== -1;
return (
<div>
<div>
@@ -264,9 +275,20 @@ export const FormBuilder = function FormBuilder({
<p className="text-subtle max-w-[280px] break-words py-1 text-sm sm:max-w-[500px]">{description}</p>
<ul className="border-default divide-subtle mt-2 divide-y-2 rounded-md border ">
{fields.map((field, index) => {
const options = field.options
? field.options
: field.getOptionsAt
? dataStore.options[field.getOptionsAt as keyof typeof dataStore]
: [];
const numOptions = options?.length ?? 0;
if (field.hideWhenJustOneOption && numOptions <= 1) {
return null;
}
const fieldType = FieldTypesMap[field.type];
const isRequired = field.required;
const isFieldEditableSystemButOptional = field.editable === "system-but-optional";
const isFieldEditableSystem = field.editable === "system";
const isUserField = !isFieldEditableSystem && !isFieldEditableSystemButOptional;
if (!fieldType) {
throw new Error(`Invalid field type - ${field.type}`);
@@ -286,7 +308,7 @@ export const FormBuilder = function FormBuilder({
<li
key={field.name}
data-testid={`field-${field.name}`}
className="group relative flex items-center justify-between p-4">
className="hover:bg-muted group relative flex items-center justify-between p-4 ">
{index >= 1 && (
<button
type="button"
@@ -305,15 +327,15 @@ export const FormBuilder = function FormBuilder({
)}
<div>
<div className="flex flex-col lg:flex-row lg:items-center">
<div className="text-default text-sm font-semibold ltr:mr-1 rtl:ml-1">
<div className="text-default text-sm font-semibold ltr:mr-2 rtl:ml-2">
{field.label || t(field.defaultLabel || "")}
</div>
<div className="flex items-center space-x-2">
{field.hidden ? (
// Hidden field can't be required, so we don't need to show the Optional badge
<Badge variant="gray">{t("hidden")}</Badge>
<Badge variant="grayWithoutHover">{t("hidden")}</Badge>
) : (
<Badge variant="gray">{isRequired ? t("required") : t("optional")}</Badge>
<Badge variant="grayWithoutHover">{isRequired ? t("required") : t("optional")}</Badge>
)}
{Object.entries(groupedBySourceLabel).map(([sourceLabel, sources], key) => (
// We don't know how to pluralize `sourceLabel` because it can be anything
@@ -323,43 +345,43 @@ export const FormBuilder = function FormBuilder({
))}
</div>
</div>
<p className="text-subtle max-w-[280px] break-words py-1 text-sm sm:max-w-[500px]">
<p className="text-subtle max-w-[280px] break-words pt-1 text-sm sm:max-w-[500px]">
{fieldType.label}
</p>
</div>
{field.editable !== "user-readonly" && (
<div className="flex items-center space-x-2">
<Switch
data-testid="toggle-field"
disabled={field.editable === "system"}
tooltip={field.editable === "system" ? t("form_builder_system_field_cant_toggle") : ""}
checked={!field.hidden}
onCheckedChange={(checked) => {
update(index, { ...field, hidden: !checked });
}}
/>
{!isFieldEditableSystem && (
<Switch
data-testid="toggle-field"
disabled={isFieldEditableSystem}
checked={!field.hidden}
onCheckedChange={(checked) => {
update(index, { ...field, hidden: !checked });
}}
classNames={{ container: "p-2 hover:bg-gray-100 rounded" }}
tooltip={t("show_on_booking_page")}
/>
)}
{isUserField && (
<Button
color="destructive"
disabled={!isUserField}
variant="icon"
onClick={() => {
removeField(index);
}}
StartIcon={FiTrash2}
/>
)}
<Button
data-testid="edit-field-action"
color="secondary"
onClick={() => {
editField(index, field);
}}>
Edit
{t("edit")}
</Button>
<Button
color="secondary"
tooltip={
field.editable === "system" || field.editable === "system-but-optional"
? t("form_builder_system_field_cant_delete")
: ""
}
disabled={field.editable === "system" || field.editable === "system-but-optional"}
variant="icon"
onClick={() => {
removeField(index);
}}
StartIcon={FiTrash2}
/>
</div>
)}
</li>
@@ -434,7 +456,7 @@ export const FormBuilder = function FormBuilder({
}}
value={FieldTypesMap[fieldForm.getValues("type")]}
options={FieldTypes.filter((f) => !f.systemOnly)}
label="Input Type"
label={t("input_type")}
/>
<InputField
required
@@ -444,7 +466,7 @@ export const FormBuilder = function FormBuilder({
fieldForm.getValues("editable") === "system" ||
fieldForm.getValues("editable") === "system-but-optional"
}
label="Name"
label="Identifier"
/>
<InputField
{...fieldForm.register("label")}
@@ -462,8 +484,7 @@ export const FormBuilder = function FormBuilder({
placeholder={t(fieldForm.getValues("defaultPlaceholder") || "")}
/>
) : null}
{fieldType?.needsOptions ? (
{fieldType?.needsOptions && !fieldForm.getValues("getOptionsAt") ? (
<Controller
name="options"
render={({ field: { value, onChange } }) => {
@@ -471,6 +492,14 @@ export const FormBuilder = function FormBuilder({
}}
/>
) : null}
{/* TODO: Maybe we should show location options in readOnly mode in Booking Questions. Right now options are not shown in Manage Booking Questions UI for location Booking Question */}
{/* {fieldForm.getValues("getOptionsAt") ? (
<OptionsField
readOnly={true}
value={dataStore.options[fieldForm.getValues("getOptionsAt") as keyof typeof dataStore]}
className="mt-6"
/>
) : null} */}
<Controller
name="required"
control={fieldForm.control}
@@ -489,7 +518,7 @@ export const FormBuilder = function FormBuilder({
}}
/>
<DialogFooter>
<DialogClose color="secondary">Cancel</DialogClose>
<DialogClose color="secondary">{t("cancel")}</DialogClose>
<Button data-testid="field-add-save" type="submit">
{isFieldEditMode ? t("save") : t("add")}
</Button>
@@ -520,10 +549,12 @@ const WithLabel = ({
{/* Component itself managing it's label should remove these checks */}
{field.type !== "boolean" && field.type !== "multiemail" && field.label && (
<div className="mb-2 flex items-center">
<Label className="!mb-0 flex items-center">{field.label}</Label>
<span className="dark:text-inverted ml-1 -mb-1 text-sm font-medium leading-none">
{!readOnly && field.required ? "*" : ""}
</span>
<Label className="!mb-0">
<span>{field.label}</span>
<span className="dark:text-inverted ml-1 -mb-1 text-sm font-medium leading-none">
{!readOnly && field.required ? "*" : ""}
</span>
</Label>
</div>
)}
{children}
@@ -587,7 +618,6 @@ export const ComponentForField = ({
`Value ${value} is not valid for type ${componentConfig.propsType} for field ${field.name}`
);
}
if (componentConfig.propsType === "text") {
return (
<WithLabel field={field} readOnly={readOnly}>
@@ -683,6 +713,7 @@ export const ComponentForField = ({
setValue={setValue as (arg: typeof value) => void}
optionsInputs={field.optionsInputs}
options={field.options}
required={field.required}
/>
</WithLabel>
) : null;
@@ -708,7 +739,7 @@ export const FormBuilderField = ({
control={control}
// Make it a variable
name={`responses.${field.name}`}
render={({ field: { value, onChange } }) => {
render={({ field: { value, onChange }, fieldState: { error } }) => {
return (
<div>
<ComponentForField
@@ -722,10 +753,13 @@ export const FormBuilderField = ({
<ErrorMessage
name="responses"
errors={formState.errors}
render={({ message }) => {
const name = message?.replace(/\{([^}]+)\}.*/, "$1");
// Use the message targeted for it.
if (name !== field.name) {
render={({ message }: { message: string | undefined }) => {
message = message || "";
// If the error comes due to parsing the `responses` object(which can have error for any field), we need to identify the field that has the error from the message
const name = message.replace(/\{([^}]+)\}.*/, "$1");
const isResponsesErrorForThisField = name === field.name;
// If the error comes for the specific property of responses(Possible for system fields), then also we would go ahead and show the error
if (!isResponsesErrorForThisField && !error) {
return null;
}
@@ -738,7 +772,7 @@ export const FormBuilderField = ({
data-testid={`error-message-${field.name}`}
className="mt-2 flex items-center text-sm text-red-700 ">
<FiInfo className="h-3 w-3 ltr:mr-2 rtl:ml-2" />
<p>{t(message)}</p>
<p>{t(message || "invalid_input")}</p>
</div>
);
}}
@@ -44,7 +44,14 @@ const fieldSchema = z.object({
.array()
.optional(),
type: fieldTypeEnum,
options: z.array(z.object({ label: z.string(), value: z.string() })).optional(),
/**
* This is an alternate way to specify options when the options are stored elsewhere. Form Builder expects options to be present at `dataStore[getOptionsAt]`
* This allows keeping a single source of truth in DB.
*/
getOptionsAt: z.string().optional(),
optionsInputs: z
.record(
z.object({
@@ -56,6 +63,10 @@ const fieldSchema = z.object({
})
)
.optional(),
/**
* It is used to hide fields such as location when there are less than two options
*/
hideWhenJustOneOption: z.boolean().default(false).optional(),
required: z.boolean().default(false).optional(),
hidden: z.boolean().optional(),