/** * Repeat groups: how many items visitors fill in, the heading of each item, the text of the add * button, and the fields each item asks for, with their type, question, placeholder, whether they * are required and the settings of their type. */ import React, { useState } from 'react'; import type { DateField, DropdownField, Field, NumberField, RepeatField, RepeatItemField, StepNode, TextareaField, TextField, TimeField } from '@funnelforms/form-schema'; import { addRepeatField, moveRepeatField, newId, removeRepeatField, REPEAT_FIELD_TYPES, setRepeatFieldType, text, type RepeatFieldType } from '../document'; import { editText, setText } from '../languages'; import type { Edit } from './Answers'; import { stepFieldName } from './catalog'; import { useEnv } from './context'; import { __ } from './i18n'; import { PhoneCountryProps } from './PhoneProps'; import { DateProps, NumberProps, TimeProps } from './RangeProps'; import { TextFieldProps } from './TextProps'; import { LinesInput, put, TextProp } from './texts'; import { Button, IconButton, icons, NumberInput, Select, Toggle } from './ui'; type Num = ( key: string ) => ( value: number | undefined ) => void; export function RepeatProps( { node, field, edit, num }: { node: StepNode; field: RepeatField; edit: Edit; num: Num } ) { const { ctx, lang, readOnly, store } = useEnv(); const change = store.getState().change; const [ open, setOpen ] = useState( null ); const [ adding, setAdding ] = useState( 'text' ); const p = field.props; const types = REPEAT_FIELD_TYPES.map( ( t ) => ( { value: t, label: stepFieldName( t ) } ) ); const groupText = ( key: 'itemLabel' | 'addLabel' ) => ( value: string ): void => edit( ( f ) => put( ( f as RepeatField ).props, key, lang, value, ctx.locale ), 'props.' + key ); return ( <>

{ __( 'Fields of each item', 'funnelforms-pro' ) }

{ p.fields.map( ( sub, i ) => (
{ const shown = ( e.currentTarget as HTMLDetailsElement ).open; if ( shown !== ( open === sub.id ) ) { setOpen( shown ? sub.id : null ); } } } > { stepFieldName( sub.type ) } { sub.required ? ' *' : '' } { text( sub.label, ctx.locale ) || '—' } edit( ( f ) => { const inner = ( f as RepeatField ).props.fields.find( ( x ) => x.id === sub.id ); if ( inner ) { recipe( inner as Field ); } }, `fields.${ sub.id }${ key ? '.' + key : '' }` ) } setType={ ( type ) => change( ( d ) => setRepeatFieldType( d, node.id, field.id, sub.id, type, ctx ) ) } setOptions={ ( labels ) => change( ( d ) => { const group = ( d.nodes.find( ( n ) => n.id === node.id ) as StepNode | undefined )?.fields.find( ( f ) => f.id === field.id ) as RepeatField | undefined; const inner = group?.props.fields.find( ( x ) => x.id === sub.id ); if ( 'dropdown' !== inner?.type ) { return; } if ( lang !== ctx.locale ) { inner.options.forEach( ( o, n ) => setText( o.label, lang, labels[ n ] ?? '', ctx.locale ) ); return; } inner.options = labels.map( ( label, n ) => { const known = inner.options[ n ]; if ( known ) { setText( known.label, lang, label, ctx.locale ); return known; } return { id: newId( d, 'o' ), label: { [ lang ]: label }, value: String( n + 1 ) }; } ); }, `${ field.id }:fields.${ sub.id }.options` ) } />
) ) }
{ 'placeholder' in props ? ( } onChange={ ( v ) => edit( ( f ) => put( f.props as Record, 'placeholder', lang, v, ctx.locale ), 'props.placeholder' ) } /> ) : null } edit( ( f ) => void ( f.required = v ), 'required' ) } /> { settings }
); }