import { PencilIcon, TrashIcon, ViewListIcon } from '@heroicons/react/outline'; import * as React from 'react'; import { useCallback, useEffect, useRef } from 'react'; import { VsLabel } from '../VscodeComponents'; export interface IListFieldProps { label: string; value: string[] | null; onChange: (value: string | string[]) => void; } export const ListField: React.FunctionComponent = ({ label, value, onChange }: React.PropsWithChildren) => { const [ text, setText ] = React.useState(""); const [ list, setList ] = React.useState(null); const [ itemToEdit, setItemToEdit ] = React.useState(null); const inputRef = useRef(null); const onTextChange = (txtValue: string) => { setText(txtValue); }; const onSaveForm = useCallback(() => { if (itemToEdit !== null) { if (list && text) { const newList = [...(list || [])]; newList[itemToEdit] = text; setList(newList); onChange(newList); } } else { if (text) { const newList = list ? [ ...list, text ] : [ text ]; setList(newList); onChange(newList); } } onCancelForm(); }, [list, text]); const onEdit = useCallback((index: number) => { setItemToEdit(index); setText(list ? list[index] : ""); inputRef.current?.focus(); }, [list]); const onCancelForm = useCallback(() => { setText(""); setItemToEdit(null); }, []); const onDelete = useCallback((index: number) => { if (list) { const newList = [...list]; newList.splice(index, 1); setList(newList); onChange(newList); } }, [list]); useEffect(() => { if (value) { if (typeof value === "string") { setList([value]); } else { setList(value); } } }, [value]); let isValid = true; return (
{label}
onTextChange(e.currentTarget.value)} onKeyDown={(e) => { if (e.key === "Enter") { onSaveForm(); } }} style={{ border: "1px solid var(--vscode-inputValidation-infoBorder)" }} />
    { list && list.length > 0 && list.map((item, index) => (
  • {item}
  • )) }
); };