import React, { useState } from 'react'; import ContainerProperties from '../../Interfaces/Properties'; import { ToggleButton } from '../ToggleButton/ToggleButton'; import { INPUT_TYPES } from './PropertiesInputTypes'; interface IPropertiesProps { properties?: ContainerProperties onChange: (key: string, value: string | number | boolean) => void onSubmit: (event: React.FormEvent, properties: ContainerProperties) => void } export const Properties: React.FC = (props: IPropertiesProps) => { const [isDynamicInput, setIsDynamicInput] = useState(true); if (props.properties === undefined) { return
; } const groupInput: React.ReactNode[] = []; Object .entries(props.properties) .forEach((pair) => handleProperties(pair, groupInput, isDynamicInput, props.onChange)); const form = isDynamicInput ?
{ groupInput }
:
props.onSubmit(event, props.properties as ContainerProperties)} >
{ groupInput }
; return (
setIsDynamicInput(!isDynamicInput)} /> { form }
); }; const handleProperties = ( [key, value]: [string, string | number], groupInput: React.ReactNode[], 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 className = ` w-full text-xs font-medium transition-all text-gray-800 mt-1 px-3 py-2 bg-white border-2 border-white rounded-lg placeholder-gray-800 focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500 disabled:bg-slate-300 disabled:text-gray-500 disabled:border-slate-300 disabled:shadow-none`; const isDisabled = ['id', 'parentId'].includes(key); const input = isDynamicInput ? { if (type === 'checkbox') { onChange(key, event.target.checked); return; } onChange(key, event.target.value); }} disabled={isDisabled} /> : ; groupInput.push( ); groupInput.push(input); };