import * as React from 'react'; import * as ReactForm from 'react-form'; import {FormValue} from 'react-form'; /* This provide a way to may a form field to an array of items. It allows you to * Add a new (maybe duplicate) item. * Replace an item. * Remove an item. E.g. env: - name: FOO value: bar - name: BAZ value: qux # You can have dup items - name: FOO value: bar It does not allow re-ordering of elements (maybe in a v2). */ export interface NameValue { name: string; value: string; } export const NameValueEditor = (item: NameValue, onChange?: (item: NameValue) => any) => { return ( onChange({...item, name: e.target.value})} // onBlur={e=>onChange({...item, name: e.target.value})} title='Name' readOnly={!onChange} />   =   onChange({...item, value: e.target.value})} title='Value' readOnly={!onChange} />   ); }; export const ValueEditor = (item: string, onChange: (item: string) => any) => { return ( onChange(e.target.value)} title='Value' readOnly={!onChange} /> ); }; interface Props { items: T[]; onChange: (items: T[]) => void; editor: (item: T, onChange: (updated: T) => any) => React.ReactNode; } export function ArrayInput(props: Props) { const addItem = (item: T) => { props.onChange([...props.items, item]); }; const replaceItem = (item: T, i: number) => { const items = props.items.slice(); items[i] = item; props.onChange(items); }; const removeItem = (i: number) => { const items = props.items.slice(); items.splice(i, 1); props.onChange(items); }; return (
{props.items.map((item, i) => (
{props.editor(item, (updated: T) => replaceItem(updated, i))}   {' '}
))} {props.items.length === 0 && }
); } export const ResetOrDeleteButton = (props: { isPluginPar: boolean; getValue: () => FormValue; name: string; index: number; setValue: (value: FormValue) => void; setAppParamsDeletedState: any; }) => { const handleDeleteChange = () => { if (props.index >= 0) { props.setAppParamsDeletedState((val: string[]) => val.concat(props.name)); } }; const handleResetChange = () => { if (props.index >= 0) { const items = [...props.getValue()]; items.splice(props.index, 1); props.setValue(items); } }; const disabled = props.index === -1; const content = props.isPluginPar ? 'Reset' : 'Delete'; let tooltip = ''; if (content === 'Reset' && !disabled) { tooltip = 'Resets the parameter to the value provided by the plugin. This removes the parameter override from the application manifest'; } else if (content === 'Delete' && !disabled) { tooltip = 'Deletes this parameter values from the application manifest.'; } return ( ); }; export const ArrayInputField = ReactForm.FormField((props: {fieldApi: ReactForm.FieldApi}) => { const { fieldApi: {getValue, setValue} } = props; return ; }); export const ArrayValueField = ReactForm.FormField( (props: {fieldApi: ReactForm.FieldApi; name: string; defaultVal: string[]; isPluginPar: boolean; setAppParamsDeletedState: any}) => { const { fieldApi: {getValue, setValue} } = props; let liveParamArray; const liveParam = getValue()?.find((val: {name: string; array: object}) => val.name === props.name); if (liveParam) { liveParamArray = liveParam?.array ?? []; } const index = getValue()?.findIndex((val: {name: string; array: object}) => val.name === props.name) ?? -1; const values = liveParamArray ?? props.defaultVal ?? []; return ( { const update = change.map((val: string | object) => (typeof val !== 'string' ? '' : val)); if (index >= 0) { getValue()[index].array = update; setValue([...getValue()]); } else { setValue([...(getValue() || []), {name: props.name, array: update}]); } }} /> ); } ); export const StringValueField = ReactForm.FormField( (props: {fieldApi: ReactForm.FieldApi; name: string; defaultVal: string; isPluginPar: boolean; setAppParamsDeletedState: any}) => { const { fieldApi: {getValue, setValue} } = props; let liveParamString; const liveParam = getValue()?.find((val: {name: string; string: string}) => val.name === props.name); if (liveParam) { liveParamString = liveParam?.string ? liveParam?.string : ''; } const values = liveParamString ?? props.defaultVal ?? ''; const index = getValue()?.findIndex((val: {name: string; string: string}) => val.name === props.name) ?? -1; return (
{ if (index >= 0) { getValue()[index].string = e.target.value; setValue([...getValue()]); } else { setValue([...(getValue() || []), {name: props.name, string: e.target.value}]); } }} title='Value' />
); } ); export const MapInputField = ReactForm.FormField((props: {fieldApi: ReactForm.FieldApi}) => { const { fieldApi: {getValue, setValue} } = props; const items = new Array(); const map = getValue() || {}; Object.keys(map).forEach(key => items.push({name: key, value: map[key]})); return ( { const newMap = {} as any; array.forEach(item => (newMap[item.name || ''] = item.value || '')); setValue(newMap); }} /> ); }); export const MapValueField = ReactForm.FormField( (props: {fieldApi: ReactForm.FieldApi; name: string; defaultVal: Map; isPluginPar: boolean; setAppParamsDeletedState: any}) => { const { fieldApi: {getValue, setValue} } = props; const items = new Array(); const liveParam = getValue()?.find((val: {name: string; map: object}) => val.name === props.name); const index = getValue()?.findIndex((val: {name: string; map: object}) => val.name === props.name) ?? -1; if (liveParam) { liveParam.map = liveParam.map ? liveParam.map : new Map(); } if (liveParam?.array) { items.push(...liveParam.array); } else { const map = liveParam?.map ?? props.defaultVal ?? new Map(); Object.keys(map).forEach(item => items.push({name: item || '', value: map[item] || ''})); if (liveParam?.map) { getValue()[index].array = items; } } return ( { if (index === -1) { getValue().push({ name: props.name, array: change }); } else { getValue()[index].array = change; } setValue([...getValue()]); }} /> ); } );