import { VerticalAlignBottomOutlined, VerticalAlignTopOutlined } from '@ant-design/icons' import { DeleteOutlined, PlusOutlined } from '@ant-design/icons/lib' import { Button, Card, Checkbox, Form, Input, Popconfirm, Popover, Space, Tag, Tooltip } from 'antd' import { FormInstance } from 'antd/lib/form' import { FieldData } from 'rc-field-form/lib/interface' import React, { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { FormFieldFragment, FormFieldLogicFragment } from '../../../graphql/fragment/form.fragment' import { fieldTypes } from '../types' import { LogicBlock } from './logic.block' interface Props { form: FormInstance fields: FormFieldFragment[] onChangeFields: (fields: FormFieldFragment[]) => void field: FieldData remove: (index: number) => void move: (from: number, to: number) => void index: number } export const FieldCard: React.FC = ({ form, field, fields, onChangeFields, remove, move, index, }) => { const { t } = useTranslation() const type = form.getFieldValue([ 'form', 'fields', field.name as string, 'type', ]) as string const TypeComponent = (fieldTypes[type] || fieldTypes['textfield']).adminFormField() const [shouldUpdate, setShouldUpdate] = useState(false) const [nextTitle, setNextTitle] = useState( form.getFieldValue([ 'form', 'fields', field.name as string, 'title', ]) ) useEffect(() => { if (!shouldUpdate) { return } const id = setTimeout(() => { setShouldUpdate(false) onChangeFields( fields.map((field, i) => { if (i === index) { return { ...field, title: nextTitle, } } else { return field } }) ) }, 500) return () => clearTimeout(id) }, [ nextTitle, shouldUpdate, fields, ]) const addLogic = useCallback((add: (defaults: unknown) => void, index: number) => { return ( ) }, []) return ( } > { setNextTitle(e.target.value) setShouldUpdate(true) }} /> {(logic, { add, remove, move }) => { const addAndMove = (index: number) => (defaults) => { add(defaults) move(fields.length, index) } return (
{addLogic(addAndMove(0), 0)} {logic.map((field, index) => (
{addLogic(addAndMove(index + 1), index + 1)}
))}
) }}
) }