import { useMemo } from "react" import { Control, FieldPath, FieldValues, useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import Button from "../../../components/fundamentals/button" import MinusIcon from "../../../components/fundamentals/icons/minus-icon" import PlusIcon from "../../../components/fundamentals/icons/plus-icon" type TableQuantitySelectorProps< TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath > = { index: number updateQuantity: (index: number, change: number) => void control: Control name: TFieldName maxQuantity?: number } & ( | { isSelectable: true isSelectedPath: TFieldName } | { isSelectable?: false isSelectedPath?: never } ) const TableQuantitySelector = < TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath >({ control, name, isSelectedPath, isSelectable = false, updateQuantity, index, maxQuantity, }: TableQuantitySelectorProps) => { const { t } = useTranslation() const currentQuantity = useWatch({ control, name: name, }) const isSelected = useWatch({ control, name: isSelectedPath as TFieldName, disabled: !isSelectedPath || !isSelectable, }) const quantityFlag = useMemo(() => { return isSelectable ? isSelected && (maxQuantity ?? 0) > 1 : true }, [isSelectable, isSelected, maxQuantity]) return (
{quantityFlag ? (

{currentQuantity}

) : (

{currentQuantity}

)}
) } export default TableQuantitySelector