import React, { useContext, useState } from 'react'; import { Button } from '@douyinfe/semi-ui'; import { IconPlus } from '@douyinfe/semi-icons'; import { JsonSchema } from '../../typings'; import { NodeRenderContext } from '../../context'; import { PropertyEdit } from './property-edit'; export interface PropertiesEditProps { value?: Record; onChange: (value: Record) => void; useFx?: boolean; } export const PropertiesEdit: React.FC = (props) => { const value = (props.value || {}) as Record; const { readonly } = useContext(NodeRenderContext); const [newProperty, updateNewPropertyFromCache] = useState<{ key: string; value: JsonSchema }>({ key: '', value: { type: 'string' }, }); const [newPropertyVisible, setNewPropertyVisible] = useState(); const clearCache = () => { updateNewPropertyFromCache({ key: '', value: { type: 'string' } }); setNewPropertyVisible(false); }; const updateProperty = ( propertyValue: JsonSchema, propertyKey: string, newPropertyKey?: string ) => { const newValue = { ...value }; if (newPropertyKey) { delete newValue[propertyKey]; newValue[newPropertyKey] = propertyValue; } else { newValue[propertyKey] = propertyValue; } props.onChange(newValue); }; const updateNewProperty = ( propertyValue: JsonSchema, propertyKey: string, newPropertyKey?: string ) => { // const newValue = { ...value } if (newPropertyKey) { if (!(newPropertyKey in value)) { updateProperty(propertyValue, propertyKey, newPropertyKey); } clearCache(); } else { updateNewPropertyFromCache({ key: newPropertyKey || propertyKey, value: propertyValue, }); } }; return ( <> {Object.keys(props.value || {}).map((key) => { const property = (value[key] || {}) as JsonSchema; return ( { const newValue = { ...value }; delete newValue[key]; props.onChange(newValue); }} /> ); })} {newPropertyVisible && ( { const key = newProperty.key; // after onblur setTimeout(() => { const newValue = { ...value }; delete newValue[key]; props.onChange(newValue); clearCache(); }, 10); }} /> )} {!readonly && (
)} ); };