/** * Settings of "Live result": which calculations it shows, with which labels, and whether it is a * card in the step or a box that stays visible on the following steps. */ import React from 'react'; import type { ResultField, ResultLine } from '@funnelforms/form-schema'; import type { Edit } from './Answers'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { put, TextProp } from './texts'; import { Button, ChoiceTiles, IconButton, icons, Notice, Select, Sketch, TextInput, Toggle } from './ui'; import { LineTextToggle, ResultReveal } from './ResultReveal'; const MAX_LINES = 10; const NONE: never[] = []; export function ResultProps( { field, edit }: { field: ResultField; edit: Edit } ) { const { ctx, lang, readOnly } = useEnv(); const calculations = useBuilder( ( st ) => st.doc?.calculations ?? NONE ); const p = field.props; const props = ( recipe: ( props: ResultField[ 'props' ] ) => void, key?: string ): void => edit( ( f ) => recipe( ( f as ResultField ).props ), key ); const line = ( i: number, recipe: ( l: ResultLine ) => void, key: string ): void => props( ( pr ) => { const item = pr.lines[ i ]; if ( item ) { recipe( item ); } }, `lines.${ i }.${ key }` ); const choices = calculations.map( ( c ) => ( { value: c.id, label: c.label || c.id } ) ); return ( <> }, { value: 'card', label: __( 'Card in this step', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => props( ( pr ) => void ( pr.style = v ), 'style' ) } /> { 'sticky' === p.style ?

{ __( 'A bar at the bottom on phones, a card beside the steps on wide screens.', 'funnelforms-pro' ) }

: null } { choices.length ? null : { __( 'Add a calculation first, e.g. the price. The live result shows its result and updates it while visitors answer.', 'funnelforms-pro' ) } }

{ __( 'Results', 'funnelforms-pro' ) }

{ __( 'The first result is shown large, the others break it down. Currency and decimals come from the calculation.', 'funnelforms-pro' ) }

{ p.lines.map( ( l, i ) => (
{ 0 === i ? __( 'Main result', 'funnelforms-pro' ) : sprintf( /* translators: %d: number of the line in the breakdown */ __( 'Line %d', 'funnelforms-pro' ), i ) } props( ( pr ) => void pr.lines.splice( i, 1 ) ) } />
line( i, ( item ) => put( item, 'label', lang, v, ctx.locale ), 'label' ) } /> c.value !== l.calculation ) ] } onChange={ ( v ) => line( i, ( item ) => { if ( v ) { item.to = v; } else { delete item.to; } }, 'to' ) } /> line( i, ( item ) => { if ( v ) { item.from = true; } else { delete item.from; } }, 'from' ) } /> line( i, ( item ) => { if ( v ) { item.text = true; } else { delete item.text; } }, 'text' ) } />
line( i, ( item ) => affix( item, 'prefix', v ), 'prefix' ) } /> line( i, ( item ) => affix( item, 'suffix', v ), 'suffix' ) } />
) ) }
props( ( pr ) => put( pr, 'disclaimer', lang, v, ctx.locale ), 'disclaimer' ) } hint={ __( 'E.g. "Estimate without obligation, incl. VAT."', 'funnelforms-pro' ) } /> ); } /** Sets a prefix or suffix of a line; empty ones are removed. */ function affix( item: ResultLine, key: 'prefix' | 'suffix', value: string ): void { if ( value ) { item[ key ] = value.slice( 0, 40 ); } else { Reflect.deleteProperty( item, key ); } } /** The live result as a box beside the steps, or as a card in its step. */ function ShownSketch( { style }: { style: ResultField[ 'props' ][ 'style' ] } ) { return ( { 'sticky' === style ? ( <> ) : ( <> ) } ); }