/** * Contact step extras: the look of the step and the answers of a list (from Funnelforms 4), what * a text field accepts and which salutations visitors can choose (from Funnelforms 3). */ import React from 'react'; import type { ContactField, ContactLayout, ContactNode, Text } from '@funnelforms/form-schema'; import { editText, setText } from '../languages'; import { useEnv } from './context'; import { __ } from './i18n'; import { LinesInput } from './texts'; import { ChoiceTiles, Group, Notice, NumberInput, Select, Sketch, TextInput, Toggle } from './ui'; type Option = { id: string; label: Text }; type ContactEdit = ( recipe: ( f: ContactField ) => void, key?: string ) => void; /** * Masks the new renderer applies (as runtime maskSupported and Domain\Form\InputMask): 9 a digit, * a a letter, * a letter or digit, everything else as typed. Other masks keep the form on 3.x. */ export function maskWorks( mask: string ): boolean { return '' !== mask && ! /[[\]{}()|\\#&b-zA-Z]/.test( mask ); } /** Whether a pattern is a regular expression the browser understands (runtime and server add ^ and $). */ export function patternWorks( pattern: string ): boolean { try { new RegExp( `^(?:${ pattern })$`, 'u' ); return true; } catch { return false; } } /** What a text field of the contact step accepts: length, numbers only, a pattern, an input mask. */ export function ContactTextProps( { field, edit, readOnly }: { field: ContactField; edit: ContactEdit; readOnly: boolean } ) { const props = field.props; const set = ( key: string, value: unknown ): void => edit( ( f ) => { const p = f.props as Record; if ( undefined === value || '' === value ) { Reflect.deleteProperty( p, key ); } else { p[ key ] = value; } }, 'props.' + key ); const pattern = typeof props.pattern === 'string' ? props.pattern : ''; const mask = typeof props.mask === 'string' ? props.mask : ''; return (
{ __( 'Check the input', 'funnelforms-pro' ) }
set( 'minLength', v ) } /> 0 ? props.maxLength : undefined } min={ 1 } disabled={ readOnly } onChange={ ( v ) => set( 'maxLength', v ) } />
label={ __( 'Allowed input', 'funnelforms-pro' ) } value={ 'number' === props.inputType ? 'number' : 'text' } disabled={ readOnly } options={ [ { value: 'text', label: __( 'Anything', 'funnelforms-pro' ) }, { value: 'number', label: __( 'A number', 'funnelforms-pro' ) }, ] } onChange={ ( v ) => set( 'inputType', 'number' === v ? 'number' : undefined ) } /> set( 'mask', v ) } /> { '' !== mask && ! maskWorks( mask ) ? { __( 'The new form display cannot apply this mask, so the form stays on Funnelforms 3. Use only 9, a, * and other characters that are added as typed.', 'funnelforms-pro' ) } : null } set( 'pattern', v ) } /> { '' !== pattern && ! patternWorks( pattern ) ? { __( 'This pattern is not a valid regular expression, so it does not check anything.', 'funnelforms-pro' ) } : null }
); } /** Salutations visitors can choose; at least one stays. */ export function SalutationProps( { field, edit, readOnly }: { field: ContactField; edit: ContactEdit; readOnly: boolean } ) { const allow = ( field.props.allow ?? {} ) as Record; const all: Array<[ string, string ]> = [ [ 'male', __( 'Mr.', 'funnelforms-pro' ) ], [ 'female', __( 'Ms.', 'funnelforms-pro' ) ], [ 'diverse', __( 'Diverse', 'funnelforms-pro' ) ], [ 'company', __( 'Company', 'funnelforms-pro' ) ], ]; const shown = all.filter( ( [ key ] ) => false !== allow[ key ] ).length; return ( { all.map( ( [ key, label ] ) => { const on = false !== allow[ key ]; return ( edit( ( f ) => { const next = { ...( ( f.props.allow ?? {} ) as Record ) }; if ( v ) { Reflect.deleteProperty( next, key ); } else { next[ key ] = false; } if ( Object.values( next ).some( ( x ) => false === x ) ) { f.props.allow = next; } else { delete f.props.allow; } }, 'props.allow' ) } /> ); } ) } ); } /** Answers of a list in the contact step, one per line. A translation only changes the texts. */ export function ContactOptions( { field, edit }: { field: ContactField; edit: ( recipe: ( f: ContactField ) => void, key?: string ) => void } ) { const { ctx, lang } = useEnv(); const options = ( Array.isArray( field.props.options ) ? field.props.options : [] ) as Option[]; return ( editText( o.label, lang, ctx.locale ) ) } resetKey={ field.id + lang } onLines={ ( lines ) => edit( ( f ) => { const list = ( Array.isArray( f.props.options ) ? f.props.options : [] ) as Option[]; if ( lang !== ctx.locale ) { list.forEach( ( o, i ) => setText( o.label, lang, lines[ i ] ?? '', ctx.locale ) ); return; } const taken = new Set( list.map( ( o ) => o.id ) ); let n = list.length; f.props.options = lines.map( ( line, i ): Option => { const known = list[ i ]; if ( known ) { setText( known.label, lang, line, ctx.locale ); return known; } while ( taken.has( `o${ ++n }` ) ); taken.add( `o${ n }` ); return { id: `o${ n }`, label: { [ lang ]: line } }; } ); }, 'options' ) } /> ); } /** Columns, labels, input height and the button of a contact step. */ export function ContactLayoutProps( { node, change }: { node: ContactNode; change: ( recipe: ( layout: ContactLayout ) => void, key: string ) => void } ) { const layout = node.layout ?? {}; const columns = ( device: 'desktop' | 'mobile' ) => ( v: string ): void => change( ( l ) => { const c = { ...l.columns }; if ( '2' === v ) { c[ device ] = 2; } else { Reflect.deleteProperty( c, device ); } if ( Object.keys( c ).length ) { l.columns = c; } else { delete l.columns; } }, 'columns.' + device ); const button = ( key: 'size' | 'align' ) => ( v: string ): void => change( ( l ) => { const b = { ...l.button } as Record; if ( '' === v ) { Reflect.deleteProperty( b, key ); } else { b[ key ] = v; } if ( Object.keys( b ).length ) { l.button = b as ContactLayout[ 'button' ]; } else { delete l.button; } }, 'button.' + key ); const perRow = [ { value: '1', label: __( 'One field per row', 'funnelforms-pro' ), icon: }, { value: '2', label: __( 'Two fields per row', 'funnelforms-pro' ), icon: }, ]; return ( }, { value: 'left', label: __( 'On the left', 'funnelforms-pro' ), icon: }, { value: 'center', label: __( 'In the centre', 'funnelforms-pro' ), icon: }, { value: 'right', label: __( 'On the right', 'funnelforms-pro' ), icon: }, ] } onChange={ button( 'align' ) } /> }, { value: '', label: __( 'Normal', 'funnelforms-pro' ), icon: }, { value: 'l', label: __( 'Large', 'funnelforms-pro' ), icon: }, ] } onChange={ button( 'size' ) } /> change( ( l ) => { if ( v ) { l.hideLabels = true; } else { delete l.hideLabels; } }, 'hideLabels' ) } /> change( ( l ) => { if ( undefined === v ) { delete l.inputHeight; } else { l.inputHeight = Math.min( 120, Math.max( 30, Math.round( v ) ) ); } }, 'inputHeight' ) } /> ); } /* Small drawings of the contact step for the tiles of the properties panel. */ function FieldsSketch( { columns }: { columns: 1 | 2 } ) { return ( { 1 === columns ? ( <> ) : ( <> ) } ); } function ButtonSketch( { x, width, height = 8 }: { x: number; width: number; height?: number } ) { return ( ); }