import { __ } from '@wordpress/i18n' import classNames from 'classnames' import { RestrictToVerticalAxis } from '@dnd-kit/abstract/modifiers' import { RestrictToElement } from '@dnd-kit/dom/modifiers' import { BuilderFieldProps } from '../types' import { Input } from '../../../Input/Input' import { Label } from '../../../Label/Label' import { Button } from '../../../Button/Button' import CloseIcon from '../../../../../../public/images/close-icon-medium.png' import { useArrayField } from '../../hooks/useGroup' import { SortableList, useSortableItem, } from '../../../SortableList/SortableList' import { QuantityFieldOption } from '../../utils/createBuilderField' import { useFormBuilderValidation } from '../../FormBuilderValidationContext' import styles from './QuantityFieldsField.module.css' const listContainerId = 'quantity-fields-sortable-list' const quantityFieldOptionValidator = (value: QuantityFieldOption) => { if ( !value || !String(value.label || '').trim() || !String(value.slug || '').trim() ) { return __('Required field', 'webba-booking-lite') } return null } const emptyQuantityField = (): QuantityFieldOption => ({ label: '', slug: '', }) interface QuantityFieldItemProps { id: number index: number value: QuantityFieldOption errors: string[] forceShowErrors: boolean onChange: (value: QuantityFieldOption) => void onRemove: () => void listRef: (element: Element | null) => void } const QuantityFieldItem = ({ id, index, value, errors, forceShowErrors, onChange, onRemove, listRef, }: QuantityFieldItemProps) => { const { handleRef } = useSortableItem() const labelInputId = `quantity-field-label-${id}-${index}` const slugInputId = `quantity-field-slug-${id}-${index}` return (
  • onChange({ ...value, label, }) } placeholder={__('Label', 'webba-booking-lite')} errors={errors} forceShowErrors={forceShowErrors} />
    onChange({ ...value, slug, }) } placeholder={__('Slug', 'webba-booking-lite')} />
  • ) } export const QuantityFieldsField = ({ group }: BuilderFieldProps) => { const quantityFields = useArrayField(group.id, 'quantityFields') const { showValidation } = useFormBuilderValidation() const hasNoFields = quantityFields.fields.length === 0 const showEmptyError = showValidation && hasNoFields const sortableItems = quantityFields.fields.map((field, index) => ({ id: index, value: (field.value || emptyQuantityField()) as QuantityFieldOption, errors: field.errors, })) return (
    ) }