/** * The answers of choice and dropdown fields: the list of answers with their values, numbers, * right answers and images, and how the answers look and how many can be chosen. */ import React, { useState } from 'react'; import type { ChoiceField, ContactField, DropdownField, Field, FormNode, Option } from '@funnelforms/form-schema'; import { addOption, addOptions, asksFor, moveOption, removeOption, splitOptionList, text } from '../document'; import { removeWithNotice } from './Removal'; import { editText } from '../languages'; import { setRightAnswer } from '../quiz'; import type { Recipe } from '../store'; import { useBuilder, useEnv } from './context'; import { __, sprintf } from './i18n'; import { put, TextProp } from './texts'; import { IconChoice } from './IconPicker'; import { Button, ChoiceTiles, Dialog, Field as FieldRow, Group, IconButton, icons, Notice, NumberInput, Sketch, TextInput, Toggle } from './ui'; export type Edit = ( recipe: ( f: Field | ContactField ) => void, key?: string ) => void; function useChange(): ( recipe: Recipe, key?: string ) => void { return useEnv().store.getState().change; } /** Sets or removes one of the field's props. */ function useProp( edit: Edit ): ( key: string, value: unknown ) => void { return ( key, value ) => edit( ( f ) => { const props = f.props as Record; if ( undefined === value || false === value ) { Reflect.deleteProperty( props, key ); } else { props[ key ] = value; } }, 'props.' + key ); } /** How many answers: at least, at most, and moving on at a number (choice only). */ function Limits( { field, edit }: { field: ChoiceField | DropdownField; edit: Edit } ) { const set = useProp( edit ); const max = field.options.length; const autoAdvance = 'choice' === field.type ? field.props.autoAdvanceAt : undefined; return ( <>
set( 'minChoices', v ) } /> set( 'maxChoices', v ) } />
{ 'choice' === field.type ? ( set( 'autoAdvanceAt', v ) } /> ) : null } { undefined !== field.props.minChoices && undefined !== field.props.maxChoices && field.props.minChoices > field.props.maxChoices ? ( { __( 'The smallest number is above the largest.', 'funnelforms-pro' ) } ) : null } ); } /* Small drawings of the look of the answers for the tiles of the properties panel. */ function TilesSketch() { return ( ); } function ListSketch( { columns = 1 }: { columns?: 1 | 2 } ) { const rows = [ 6, 13.5, 21 ]; return ( { 1 === columns ? rows.map( ( y ) => ) : rows.flatMap( ( y ) => [ , ] ) } ); } /** Tiles in a row; 0 is as many as fit. */ function RowSketch( { count }: { count: number } ) { if ( 0 === count ) { return ( ); } const gap = 2; const width = ( 36 - gap * ( count - 1 ) ) / count; return ( { Array.from( { length: count }, ( _, i ) => ( ) ) } ); } function HeightSketch( { height }: { height: number } ) { return ( ); } /** An answer tile with an image of this width to height. */ function RatioSketch( { ratio }: { ratio: number | null } ) { const width = 20; const height = null === ratio ? 0 : Math.round( width / ratio ); return ( { null === ratio ? ( ) : ( ) } ); } function IconSketch( { at }: { at: 'top' | 'left' | 'right' | 'bottom' } ) { const icon = { top: [ 19, 5 ], bottom: [ 19, 18 ], left: [ 8, 11.5 ], right: [ 31, 11.5 ] }[ at ]; const text = { top: 'M16 23h16', bottom: 'M16 10h16', left: 'M20 16h18', right: 'M10 16h18' }[ at ]; return ( ); } export function ChoiceProps( { node, field, edit }: { node: FormNode; field: ChoiceField; edit: Edit } ) { const set = useProp( edit ); const p = field.props; const columns = ( device: 'desktop' | 'mobile', value: string ): void => edit( ( f ) => { const props = ( f as ChoiceField ).props; const next = { ...props.columns, [ device ]: '' === value ? undefined : Number( value ) }; if ( undefined === next.desktop && undefined === next.mobile ) { delete props.columns; } else { props.columns = JSON.parse( JSON.stringify( next ) ) as ChoiceField[ 'props' ][ 'columns' ]; } }, 'props.columns' ); return ( <> edit( ( f ) => void ( ( f as ChoiceField ).props.multiple = v ), 'props.multiple' ) } /> { p.multiple ? : null } }, { value: 'list', label: __( 'List', 'funnelforms-pro' ), icon: }, { value: 'list2', label: __( 'List in two columns', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => edit( ( f ) => void ( ( f as ChoiceField ).props.layout.desktop = v ), 'props.layout' ) } /> { 'grid' === p.layout.desktop ? ( }, ...[ 1, 2, 3, 4 ].map( ( n ) => ( { value: String( n ), label: String( n ), icon: } ) ) ] } onChange={ ( v ) => columns( 'desktop', v ) } /> ) : null } }, { value: 'm', label: __( 'Medium', 'funnelforms-pro' ), icon: }, { value: 'l', label: __( 'Large', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => set( 'size', 'm' === v ? undefined : v ) } /> { p.hideIcons ? null : ( }, { value: 'left', label: __( 'Left of the text', 'funnelforms-pro' ), icon: }, { value: 'right', label: __( 'Right of the text', 'funnelforms-pro' ), icon: }, { value: 'bottom', label: __( 'Below the text', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => set( 'iconPosition', 'top' === v ? undefined : v ) } /> ) } { p.hideIcons ? null : ( edit( ( f ) => { const props = ( f as ChoiceField ).props; if ( v ) { props.imageFull = true; } else { delete props.imageFull; delete props.imageRatio; } }, 'props.imageFull' ) } /> ) } { ! p.hideIcons && p.imageFull ? ( }, { value: '1:1', label: __( 'Square', 'funnelforms-pro' ), icon: }, { value: '16:9', label: '16:9', icon: }, { value: 'auto', label: __( 'Original', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => set( 'imageRatio', '4:3' === v ? undefined : v ) } /> ) : null } edit( ( f ) => void ( ( f as ChoiceField ).props.hideIcons = v ), 'props.hideIcons' ) } /> }, { value: 'grid', label: __( 'Tiles', 'funnelforms-pro' ), icon: }, ] } onChange={ ( v ) => edit( ( f ) => void ( ( f as ChoiceField ).props.layout.mobile = v ), 'props.layout' ) } /> { 'grid' === p.layout.mobile ? ( ( { value: String( n ), label: String( n ), icon: } ) ) } onChange={ ( v ) => columns( 'mobile', '2' === v ? '' : v ) } /> ) : null } ); } export function DropdownProps( { node, field, edit }: { node: FormNode; field: DropdownField; edit: Edit } ) { const { ctx, lang } = useEnv(); const set = useProp( edit ); return ( <> edit( ( f ) => put( f.props as DropdownField[ 'props' ], 'placeholder', lang, v, ctx.locale ), 'props.placeholder' ) } /> set( 'search', v ) } /> set( 'multiple', v ) } /> { field.props.multiple ? : null } ); } /** Data type of an answer dragged to another place in the list. */ const OPTION_DRAG = 'application/x-ffx-option'; /** Answers pasted as a list. */ function PasteOptions( { onClose, onAdd }: { onClose: () => void; onAdd: ( labels: string[] ) => void } ) { const [ value, setValue ] = useState( '' ); const labels = splitOptionList( value ); return ( } > ); } export function Options( { node, field, edit }: { node: FormNode; field: Extract; edit: Edit } ) { const { api, ctx, lang, readOnly, store } = useEnv(); const change = useChange(); const [ pasting, setPasting ] = useState( false ); const pickImage = api.pickImage?.bind( api ); // A knowledge quiz marks right answers; otherwise numbers for calculations, once the form has // calculations or the answers have numbers. const knowledge = useBuilder( ( st ) => 'knowledge' === st.doc?.settings.quiz?.kind ); const calculations = useBuilder( ( st ) => ( st.doc?.calculations.length ?? 0 ) > 0 ); const calc = ! knowledge && ( calculations || field.options.some( ( o ) => undefined !== o.calcValue ) ); // Hidden values (3.x "hidden field"): choice and dropdown fields send them; shown while one is set or switched on. const hiddenable = 'choice' === field.type || 'dropdown' === field.type; const hasHidden = field.options.some( ( o ) => undefined !== o.hiddenValue ); const [ hiddenOn, setHiddenOn ] = useState( false ); const showHidden = hiddenable && ( hasHidden || hiddenOn ); // An answer with only an image has no name a screen reader could read out (the image is decorative). const unnamed = field.options.some( ( o ) => 'image' === o.media?.kind && '' === ( text( o.label, lang ) || text( o.label, ctx.locale ) ).replace( /<[^>]*>/g, '' ).trim() ); const move = ( id: string, to: number ): void => change( ( d ) => void moveOption( d, node.id, field.id, id, to ) ); const option = ( id: string, recipe: ( o: Option ) => void, key: string ): void => edit( ( f ) => { const o = ( f as typeof field ).options.find( ( x ) => x.id === id ); if ( o ) { recipe( o ); } }, `${ id }:${ key }` ); return ( { () => (
{ calc || knowledge ? ( ) : null } { field.options.map( ( o, i ) => (
{ if ( e.dataTransfer.types.includes( OPTION_DRAG ) ) { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; } } } onDrop={ ( e ) => { const id = e.dataTransfer.getData( OPTION_DRAG ); if ( id ) { e.preventDefault(); move( id, i ); } } } > option( o.id, ( x ) => put( x, 'label', lang, e.target.value, ctx.locale ), 'label' ) } /> option( o.id, ( x ) => void ( x.value = e.target.value ), 'value' ) } /> { calc ? ( option( o.id, ( x ) => { const n = Number.parseFloat( e.target.value ); if ( Number.isFinite( n ) ) { x.calcValue = n; } else { delete x.calcValue; } }, 'calcValue' ) } /> ) : null } { knowledge ? ( change( ( d ) => setRightAnswer( d, field.id, o.id, e.target.checked ) ) } /> ) : null } { 'choice' === field.type && ( api.icons || pickImage ) ? ( option( o.id, ( x ) => { if ( name ) { x.media = { kind: 'lucide', value: name }; } else { delete x.media; } }, 'media' ) } image={ pickImage ? { url: 'image' === o.media?.kind ? o.media.value : null, pick: async () => { const picked = await pickImage(); if ( picked ) { option( o.id, ( x ) => void ( x.media = { kind: 'image', value: picked.url } ), 'media' ); } }, remove: () => option( o.id, ( x ) => void delete x.media, 'media' ), removeLabel: sprintf( __( 'Remove the image of answer %d', 'funnelforms-pro' ), i + 1 ), } : undefined } /> ) : null } removeWithNotice( store, ( d ) => removeOption( d, node.id, field.id, o.id ), asksFor( field.id, o.id ) ) } /> { showHidden ? ( option( o.id, ( x ) => { if ( '' === e.target.value ) { delete x.hiddenValue; } else { x.hiddenValue = e.target.value; } }, 'hiddenValue' ) } /> ) : null }
) ) } { unnamed ? { __( 'Give answers with an image a text too. Without one, screen readers only read out their number.', 'funnelforms-pro' ) } : null }
{ hiddenable ? ( { setHiddenOn( on ); if ( ! on && hasHidden ) { edit( ( f ) => { for ( const x of ( f as typeof field ).options ) { delete x.hiddenValue; } }, 'hiddenValue' ); } } } /> ) : null } { calc ?

{ __( '"Counts as" is the number an answer adds to calculations, e.g. its price; empty counts as 0. "Saved as" is what entries, mails and integrations get; it does not count.', 'funnelforms-pro' ) }

: null } { pasting ? setPasting( false ) } onAdd={ ( labels ) => change( ( d ) => void addOptions( d, node.id, field.id, labels, ctx ) ) } /> : null }
) }
); }