import type { FieldSource, SchemaField } from "./schema-field.js"; import { encodeSchemaPath } from "./schema-paths.js"; const EMPTY_SUGGESTIONS: ReadonlyArray = []; export type BindingSourceControlProps = { readonly field: SchemaField; readonly source: FieldSource; readonly literalValue: unknown; readonly onChange: (source: FieldSource) => void; readonly suggestions?: ReadonlyArray; readonly idPrefix?: string; }; const fieldKey = (field: SchemaField): string => encodeSchemaPath(field.path); const displayTitle = (title: string): string => title.length === 0 ? "Value" : `${title.slice(0, 1).toUpperCase()}${title.slice(1)}`; export const BindingSourceControl = ({ field, source, literalValue, onChange, suggestions = EMPTY_SUGGESTIONS, idPrefix, }: BindingSourceControlProps) => { const key = idPrefix === undefined ? fieldKey(field) : `${idPrefix}-${fieldKey(field)}`; const literalId = `${key}-literal`; const bindId = `${key}-bind`; const sourceId = `${key}-source-path`; const listId = `${key}-source-suggestions`; return (
Value source
{source.mode === "bind" && (
0 ? listId : undefined} id={sourceId} list={suggestions.length > 0 ? listId : undefined} onChange={(event) => onChange({ mode: "bind", sourcePath: event.target.value })} type="text" value={source.sourcePath} /> {suggestions.length > 0 && ( {suggestions.map((suggestion) => )}
)}
); };