/** * All questions of the form in one list, with a search, a filter by type and "required only". * A click selects the question and brings its step into view on the canvas. */ import React, { useState } from 'react'; import type { ContactField, Field } from '@funnelforms/form-schema'; import { fieldKind, reachable } from '../document'; import { contactFieldName, stepFieldName } from './catalog'; import { useBuilder, useEnv } from './context'; import { fieldTitle } from './describe'; import { __, sprintf } from './i18n'; import { Select, Toggle } from './ui'; /** Event the canvas listens to, with the id of the node to show. */ export const REVEAL_EVENT = 'ffxb:reveal'; interface Row { node: string; field: string; title: string; type: string; typeName: string; key: string; required: boolean; reachable: boolean; } export function QuestionList() { const doc = useBuilder( ( s ) => s.doc ); const selection = useBuilder( ( s ) => s.selection ); const { store, ctx } = useEnv(); const [ query, setQuery ] = useState( '' ); const [ type, setType ] = useState( '' ); const [ required, setRequired ] = useState( false ); if ( ! doc ) { return null; } const reached = reachable( doc ); const rows: Row[] = []; for ( const node of doc.nodes ) { if ( 'end' === node.type ) { continue; } for ( const field of node.fields as Array ) { const contact = 'contact' === node.type; rows.push( { node: node.id, field: field.id, title: fieldTitle( field as Field, ctx.locale ), type: contact ? 'contact:' + field.type : fieldKind( field ), typeName: contact ? contactFieldName( field.type ) : stepFieldName( fieldKind( field ) ), key: field.key, required: field.required, reachable: reached.has( node.id ), } ); } } const types = [ ...new Map( rows.map( ( r ) => [ r.type, r.typeName ] ) ) ].sort( ( a, b ) => a[ 1 ].localeCompare( b[ 1 ] ) ); const needle = query.trim().toLowerCase(); const shown = rows.filter( ( r ) => ( ! needle || `${ r.title } ${ r.key }`.toLowerCase().includes( needle ) ) && ( ! type || r.type === type ) && ( ! required || r.required ) ); return (
setQuery( e.target.value ) } />