/* eslint-disable @typescript-eslint/no-explicit-any */ import { Field, useFormikContext } from 'formik' import _identity from 'lodash/identity' import _isEmpty from 'lodash/isEmpty' import _isNull from 'lodash/isNull' import _map from 'lodash/map' import React from 'react' import { getFieldComponent, getFieldLabel, getValidateFunctions, } from '../billing-info-container/utils' import { NativeSelectField } from '../common' interface IAddonComponentProps { classNamePrefix: string; data: any; selectOptions: any; handleAddonChange?: (...res: any) => void; addOnDataWithCustomFields: any; configs: any; values: any; errors: any; useStepperQty?: boolean; siblingIds?: string[]; allAddonIds?: string[]; maxTotalQuantity?: number; onCustomFieldChange?: ( addon: any, groupId: string | number, fieldName: string, value: any ) => void; } const AddonStepper = ({ id, selectOptions, handleAddonChange, classNamePrefix, siblingIds = [], allAddonIds = [], maxTotalQuantity }: any) => { const { values, setFieldValue } = useFormikContext() const qty = Number(values[id] ?? 0) const max = selectOptions?.length > 0 ? selectOptions[selectOptions.length - 1].value : 0 const totalSelected = allAddonIds.length > 0 ? allAddonIds.reduce((sum: number, aid: string) => sum + Number(values[aid] ?? 0), 0) : 0 const change = (next: number) => { if (next > qty) { if (maxTotalQuantity != null) { allAddonIds.forEach((aid: string) => { if (aid !== id) setFieldValue(aid, 0) }) } else { siblingIds.forEach((siblingId: string) => setFieldValue(siblingId, 0)) } } setFieldValue(id, next) handleAddonChange(id, next) } const incrementDisabled = qty >= max || (maxTotalQuantity != null && totalSelected >= maxTotalQuantity && qty === 0) return (
{qty}
) } const AddonComponent = ({ classNamePrefix = '', data, selectOptions, handleAddonChange = _identity, addOnDataWithCustomFields, configs, values, errors, useStepperQty = false, siblingIds = [], allAddonIds = [], maxTotalQuantity, onCustomFieldChange = _identity, }: IAddonComponentProps) => { const { id, name, active, stock } = data return (
{!useStepperQty &&
{name}
}
{!active || (!_isNull(stock) && stock <= 0) ? (
{name}
SOLD OUT
) : ( <>
{useStepperQty ? (
{name}
) : ( { const { value } = e.target handleAddonChange(id, value) }} /> )}
{!_isEmpty(addOnDataWithCustomFields?.fields) && values[id] > 0 && (

{addOnDataWithCustomFields.label}

{_map(addOnDataWithCustomFields.fields, group => { const { id, groupClassname, groupItems } = group return (
{_map(groupItems, element => { const fieldName = `${data.id}-${id}-${element.name}` const Comp = getFieldComponent(element) const type = element.type === 'radio' || element.type === 'checkbox' ? undefined : element.type return (
{({ field, form }: any) => { const handleChange = (e: any) => { // extract value from native events or custom widgets const v = e?.target?.value ?? e?.value ?? e // allow custom components to pass raw value form.setFieldValue(field.name, v) onCustomFieldChange?.(data, id, element.name, v) } return ( ) }}
) })}
) })}
)} )}
) } export default AddonComponent