/* This file is part of GNU Taler (C) 2021-2024 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Taler; see the file COPYING. If not, see */ /** * * @author Sebastian Javier Marchano (sebasjm) */ import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, h, VNode } from "preact"; import { useState } from "preact/hooks"; import { InputProps, useField } from "./useField.js"; import { DropdownList } from "./InputSearchOnList.js"; export interface Props extends InputProps { isValid?: (e: any) => boolean; getSuggestion?: (e: any) => Promise<{ id: string; description: string }[]>; addonBefore?: string; toStr?: (v?: any) => string; fromStr?: (s: string) => any; unique?: boolean; } const defaultToString = (f?: any): string => (f ? String(f) : ""); const defaultFromString = (v: string): any => v as any; export function InputArray({ name, readonly, placeholder, tooltip, label, unique, help, addonBefore, getSuggestion, fromStr = defaultFromString, toStr = defaultToString, }: Props): VNode { const { error, value, onChange, required } = useField(name); const array: T[keyof T][] = value ? value! : []; const [currentValue, setCurrentValue] = useState(""); const [suggestions, setSuggestions] = useState< { id: string; description: string }[] >([]); const { i18n } = useTranslationContext(); return (
{addonBefore && ( )}

=> { const v = e.currentTarget.value; setCurrentValue(v); if (getSuggestion) { getSuggestion(v).then((ss) => { setSuggestions(ss); }); } }} /> {required && ( )}

{getSuggestion ? undefined : (

)}
{help} {error &&

{error}

} {suggestions.length > 0 ? (
{ if (!unique || array.indexOf(p as any) === -1) { onChange([p, ...array] as T[keyof T]); } setCurrentValue(""); setSuggestions([]); }} withImage={false} />
) : undefined} {array.map((v, i) => ( ))}
); }