Skip to content

Commit

Permalink
fix: Dynamic form sub category and feature name auto delete fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtrazy committed Dec 4, 2024
1 parent 31d7314 commit 2403ff6
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/components/BranchManager/Forms/DynamicForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,24 @@ const DynamicForm: FC = () => {
}
};

// const [categories] = useState<Category[]>([
// { key: 'Non-Vegetarian', label: 'Non-Vegetarian' },
// { key: 'Vegetarian', label: 'Vegetarian' },
// { key: 'Other', label: 'Other' },
// ]);
const addSubCategory = (featureIndex: number) => {
const currentFeature = fields[featureIndex];
update(featureIndex, {
name: currentFeature.name, // Preserve the feature name
subCategories: [...currentFeature.subCategories, { name: '', price: 0 }],
});
};

const removeSubCategory = (featureIndex: number, subIndex: number) => {
const currentFeature = fields[featureIndex];
update(featureIndex, {
name: currentFeature.name, // Preserve the feature name
subCategories: currentFeature.subCategories.filter(
(_, i) => i !== subIndex,
),
});
};

if (!categories.length) {
return <div>Loading...</div>;
}
Expand Down Expand Up @@ -320,14 +333,7 @@ const DynamicForm: FC = () => {
<button
type="button"
className="flex w-1/4 ml-3 sm:mb-0 mb-4 sm:mt-2 justify-center rounded-3xl bg-transparent p-3 font-medium text-gray hover:bg-opacity-90"
onClick={() =>
update(index, {
...fields[index],
subCategories: fields[index].subCategories.filter(
(_, i) => i !== subIndex,
),
})
}
onClick={() => removeSubCategory(index, subIndex)}
>
<TrashIcon className="h-7 w-7 text-red-500 dark:text-[#ffffff]" />
</button>
Expand All @@ -336,15 +342,7 @@ const DynamicForm: FC = () => {
<Button
type="button"
className="flex w-full mt-3 mb-3 justify-center rounded-lg border-2 border-solid text-black p-3 font-medium dark:text-white hover:bg-opacity-90"
onClick={() =>
update(index, {
...fields[index],
subCategories: [
...fields[index].subCategories,
{ name: '', price: 0 },
],
})
}
onClick={() => addSubCategory(index)}
>
Add Sub Category
</Button>
Expand Down

0 comments on commit 2403ff6

Please sign in to comment.