import { PlusOutlined } from '@ant-design/icons/lib' import { Button, Form, Select, Space, Tabs } from 'antd' import { FormInstance } from 'antd/lib/form' import { TabPaneProps } from 'antd/lib/tabs' import debug from 'debug' import { FieldData } from 'rc-field-form/lib/interface' import React, { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import { FormFieldFragment } from '../../../graphql/fragment/form.fragment' import { fieldTypes } from '../types' import { FieldCard } from './field.card' const logger = debug('FieldsTab') interface Props extends TabPaneProps { form: FormInstance fields: FormFieldFragment[] onChangeFields: (fields: FormFieldFragment[]) => void } export const FieldsTab: React.FC = (props) => { const { t } = useTranslation() const [nextType, setNextType] = useState('textfield') const renderType = useCallback( ( field: FieldData, index: number, remove: (index: number) => void, move: (from: number, to: number) => void ) => { return ( { logger('remove %d', index) remove(index) }} move={(from: number, to: number) => { logger('move %d TO %d', from, to) move(from, to) }} fields={props.fields} onChangeFields={props.onChangeFields} /> ) }, [props.fields] ) const addField = useCallback( (add: (defaults: unknown) => void, index: number) => { return ( ) }, [props.fields, nextType] ) return ( {(fields, { add, remove, move }) => { const addAndMove = (index: number) => (defaults) => { add(defaults) move(fields.length, index) } return (
{addField(addAndMove(0), 0)} {fields.map((field, index) => (
{renderType(field, index, remove, move)} {addField(addAndMove(index + 1), index + 1)}
))}
) }}
) }