import React, { useState } from 'react'; import ContainerProperties from '../../Interfaces/Properties'; import { INPUT_TYPES } from './PropertiesInputTypes'; interface IPropertiesProps { properties?: ContainerProperties onChange: (key: string, value: string | number | boolean) => void onSubmit: (event: React.FormEvent, refs: Array>) => void } export const Properties: React.FC = (props: IPropertiesProps) => { if (props.properties === undefined) { return
; } const [isDynamicInput, setIsDynamicInput] = useState(true); const groupInput: React.ReactNode[] = []; const refs: Array> = []; Object .entries(props.properties) .forEach((pair) => handleProperties(pair, groupInput, refs, isDynamicInput, props.onChange)); const form = isDynamicInput ?
{ groupInput }
:
props.onSubmit(event, refs)} > { groupInput }
; return (
{ setIsDynamicInput(!isDynamicInput); }} checked={isDynamicInput} /> { form }
); }; const handleProperties = ( [key, value]: [string, string | number], groupInput: React.ReactNode[], refs: Array>, isDynamicInput: boolean, onChange: (key: string, value: string | number | boolean) => void ): void => { const id = `property-${key}`; let type = 'text'; let checked; /// hardcoded stuff for ergonomy /// if (typeof value === 'boolean') { checked = value; } if (key in INPUT_TYPES) { type = INPUT_TYPES[key]; } const ref: React.RefObject = React.useRef(null); refs.push(ref); const isDisabled = ['id', 'parentId'].includes(key); if (isDynamicInput) { groupInput.push(
{ if (type === 'checkbox') { onChange(key, event.target.checked); return; } onChange(key, event.target.value); }} disabled={isDisabled} />
); return; } /// groupInput.push(
); };