Skip to content

Commit

Permalink
added ability to delete properties and added property function for ca…
Browse files Browse the repository at this point in the history
…ses which path contain `array<object>`
  • Loading branch information
Gmin2 committed Apr 5, 2024
1 parent 06a2f4e commit d5104da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
35 changes: 30 additions & 5 deletions packages/ui/components/VisualEditor/SchemaObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ const SchemaObject: React.FC<SchemaObjectProps> = ({
};

const handleRemoveProperty = (propertyPath: string) => {
const currentSchema = _.cloneDeep(schema);
_.unset(currentSchema, propertyPath);
console.log(`Removed property at ${propertyPath}`);
onSchemaChange(currentSchema);
const updatedSchema = _.cloneDeep(schema);
const normalizedPath = propertyPath.startsWith('.') ? propertyPath.slice(1) : propertyPath;
console.log("fullPath: ",normalizedPath)
console.log("propertyPath: ",propertyPath)
const isRemoved = _.unset(updatedSchema, normalizedPath);

if (isRemoved) {
const propertyName = normalizedPath.split('.').pop();
if (updatedSchema.required) {
updatedSchema.required = updatedSchema.required.filter(
(requiredProp: any) => requiredProp !== propertyName
);
}
console.log(`Removed property at ${propertyPath}`);
console.log("removed schema",updatedSchema);
onSchemaChange(updatedSchema);
} else {
console.log(`Failed to remove property at ${propertyPath}`);
}
};

const handleTypeChange = (propertyPath: string, newSchema: any, newType: any) => { // Added types to resolve TS7006
Expand All @@ -48,6 +63,16 @@ const SchemaObject: React.FC<SchemaObjectProps> = ({
console.log(`handleTypeChange called with path: ${propertyPath}, newType: ${newTypeValue}`);
};

const handleToggleRequired = (path:string, name: string) => {
const normalizedPath = path.startsWith('.') ? path.slice(1) : path;
/** Todo */
// const updatedSchema = _.cloneDeep(schema);
// console.log("normalizedPath", normalizedPath)
// console.log("path",path)
// console.log("name",name)
// console.log("updatedSchema",JSON.stringify(updatedSchema))
}

return (
<div style={{ margin: '10px 0' }}>
{_.map(schema.properties, (propertySchema, propertyName) => (
Expand All @@ -56,7 +81,7 @@ const SchemaObject: React.FC<SchemaObjectProps> = ({
name={propertyName}
schema={propertySchema}
onRemove={handleRemoveProperty}
onToggleRequired={() => console.log('Toggling required')}
onToggleRequired={handleToggleRequired}
isRequired={_.includes(schema.required, propertyName)}
onTypeChange={handleTypeChange}
onAddNestedProperty={handleAddProperty}
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/components/VisualEditor/SchemaProperty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ const SchemaProperty: React.FC<SchemaPropertyProps> = ({
</div>
{renderNestedProperties()}
{renderArrayItemsProperties()}
{schema.type === 'object' && (
{schema.type === 'array' && schema.items && schema.items.type === "object" && (
<PropertyControls
onAdd={onAddNestedProperty}
schemaPath={`${path}`}
schemaPath={`${path}.items.properties`}
level={level + 1}
/>
)}
Expand Down

0 comments on commit d5104da

Please sign in to comment.