Skip to content

Commit

Permalink
Merge pull request #1552 from rockingrohit9639/fix/custom-field
Browse files Browse the repository at this point in the history
fix(custom-field): fix issue with empty category in custom field
  • Loading branch information
DonKoko authored Jan 3, 2025
2 parents b2876ff + 1dfabc7 commit f0b3a6e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
6 changes: 5 additions & 1 deletion app/components/custom-fields/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const NewCustomFieldFormSchema = z.object({
.transform((val) => (val === "on" ? true : false)),
organizationId: z.string(),
options: z.array(z.string()).optional(),
categories: z.array(z.string()).optional(),
categories: z
.array(z.string().min(1, "Please select a category"))
.optional()
.default([]),
});

/** Pass props of the values to be used as default for the form fields */
Expand Down Expand Up @@ -263,6 +266,7 @@ export const CustomFieldForm = ({
<CategoriesInput
categories={categories}
name={(i) => zo.fields.categories(i)()}
error={(i) => zo.errors.categories(i)()?.message}
/>
)}
</FormRow>
Expand Down
78 changes: 46 additions & 32 deletions app/components/forms/categories-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { useState } from "react";
import { tw } from "~/utils/tw";
import DynamicSelect from "../dynamic-select/dynamic-select";
import { Button } from "../shared/button";
import When from "../when/when";

type CategoriesInputProps = {
className?: string;
style?: React.CSSProperties;
disabled?: boolean;
name: (index: number) => string;
categories: string[];
error: (index: number) => string | undefined;
};

export default function CategoriesInput({
Expand All @@ -17,46 +19,58 @@ export default function CategoriesInput({
disabled,
name,
categories: incomingCategories,
error,
}: CategoriesInputProps) {
const [categories, setCategories] = useState<string[]>(
incomingCategories.length === 0 ? [""] : incomingCategories
);

return (
<div className={tw("w-full", className)} style={style}>
{categories.map((category, i) => (
<div key={i} className="mb-3 flex items-center gap-x-2">
<DynamicSelect
disabled={disabled}
fieldName={name(i)}
defaultValue={category}
model={{ name: "category", queryKey: "name" }}
contentLabel="Category"
initialDataKey="categories"
countKey="totalCategories"
placeholder="Select Category"
className="flex-1"
excludeItems={categories}
onChange={(value) => {
if (value !== undefined) {
categories[i] = value;
setCategories([...categories]);
}
}}
/>
{categories.map((category, i) => {
const errorMessage = error(i);

<Button
icon="x"
className="py-2"
variant="outline"
type="button"
onClick={() => {
categories.splice(i, 1);
setCategories([...categories]);
}}
/>
</div>
))}
return (
<div key={i} className="mb-3">
<div className="flex items-center gap-x-2">
<DynamicSelect
disabled={disabled}
fieldName={name(i)}
defaultValue={category}
model={{ name: "category", queryKey: "name" }}
contentLabel="Category"
initialDataKey="categories"
countKey="totalCategories"
placeholder="Select Category"
className="flex-1"
excludeItems={categories}
onChange={(value) => {
if (value !== undefined) {
categories[i] = value;
setCategories([...categories]);
}
}}
/>

<Button
icon="x"
className="py-2"
variant="outline"
type="button"
disabled={categories.length === 1}
onClick={() => {
categories.splice(i, 1);
setCategories([...categories]);
}}
/>
</div>

<When truthy={!!errorMessage}>
<p className="mt-1 text-sm text-red-500">{errorMessage}</p>
</When>
</div>
);
})}

<Button
icon="plus"
Expand Down

0 comments on commit f0b3a6e

Please sign in to comment.