From d3f1e00b0c4f319fdbfe9465380dbef263bf16bf Mon Sep 17 00:00:00 2001 From: banqinghe Date: Mon, 17 Mar 2025 23:23:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20array=20=E7=9A=84=E5=AD=90=E9=A1=B9?= =?UTF-8?q?=E4=B8=BA=20object=20=E6=97=B6=E6=97=A0=E6=B3=95=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=AD=90=E9=A1=B9=E5=BF=85=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonSchemaEditor/SchemaItem/index.tsx | 16 ++------- src/JsonSchemaEditor/index.tsx | 40 ++++++++++++++--------- src/JsonSchemaEditor/utils.ts | 16 +++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/JsonSchemaEditor/SchemaItem/index.tsx b/src/JsonSchemaEditor/SchemaItem/index.tsx index 303e6d9..ec18d44 100644 --- a/src/JsonSchemaEditor/SchemaItem/index.tsx +++ b/src/JsonSchemaEditor/SchemaItem/index.tsx @@ -32,7 +32,6 @@ import { type SchemaItemProps = { propertyName?: string; nodeDepth?: number; - parentSchemaDepth?: number; namePath?: number[]; isArrayItems?: boolean; isRequire?: boolean; @@ -45,11 +44,7 @@ type SchemaItemProps = { renameProperty?: (namePath: number[], name: string) => void; removeProperty?: (namePath: number[]) => void; addProperty?: (path: number[], isChild: boolean) => void; - updateRequiredProperty?: ( - path: number[], - requiredProperty: string, - removed: boolean, - ) => void; + updateRequiredProperty?: (namePath: number[], removed: boolean) => void; handleAdvancedSettingClick?: ( namePath: number[], schema: JSONSchema7, @@ -65,7 +60,6 @@ function SchemaItem(props: SchemaItemProps) { renameProperty, isArrayItems, updateRequiredProperty, - parentSchemaDepth = 0, removeProperty, addProperty, isRequire, @@ -182,11 +176,7 @@ function SchemaItem(props: SchemaItemProps) { checked={isRequire} onChange={(e) => { if (updateRequiredProperty && propertyName) { - updateRequiredProperty( - namePath.slice(0, parentSchemaDepth), - propertyName, - !e.target.checked, - ); + updateRequiredProperty(namePath, !e.target.checked); } }} /> @@ -358,7 +348,6 @@ function SchemaItem(props: SchemaItemProps) { isRequire={schema.required?.includes(name)} isArrayItems={false} nodeDepth={nodeDepth + 1} - parentSchemaDepth={!isRoot ? parentSchemaDepth + 2 : 0} namePath={namePath.concat( getPropertyIndex(schema, 'properties'), getPropertyIndex(schema.properties, name), @@ -378,7 +367,6 @@ function SchemaItem(props: SchemaItemProps) { isRequire={false} isArrayItems={true} nodeDepth={nodeDepth + 1} - parentSchemaDepth={!isRoot ? parentSchemaDepth + 1 : 0} propertyName={'items'} namePath={namePath.concat(getPropertyIndex(schema, 'items'))} schema={schema.items as JSONSchema7} diff --git a/src/JsonSchemaEditor/index.tsx b/src/JsonSchemaEditor/index.tsx index e188a1a..472428e 100644 --- a/src/JsonSchemaEditor/index.tsx +++ b/src/JsonSchemaEditor/index.tsx @@ -8,7 +8,7 @@ import React, { } from 'react'; import SchemaItem from './SchemaItem'; import { JSONSchema7, SchemaEditorProps } from './types'; -import { getDefaultSchema, inferSchema } from './utils'; +import { getDefaultSchema, getValueByPath, inferSchema } from './utils'; export interface JsonSchemaEditorHandle { changeSchema: (namePath: number[], value: any, propertyName?: string) => void; @@ -168,23 +168,31 @@ const JsonSchemaEditor = forwardRef( } } - function updateRequiredProperty( - path: number[], - requiredProperty: string, - removed: boolean, - ) { - // console.log("updateRequiredProperty", path, requiredProperty, removed); + function updateRequiredProperty(namePath: number[], removed: boolean) { + if (namePath.length < 2) { + console.error('路径长度不足,无法更新必选属性'); + return; + } + let schemaClone = _.cloneDeep(schema); - let current: any = schemaClone; - for (let i = 0; i < path.length; i++) { - const index = path[i]; - const keys = Object.keys(current); - if (typeof current[keys[index]] === 'undefined') { - current[keys[index]] = {}; - } - current = current[keys[index]]; + + const parentPath = namePath.slice(0, -2); + const propertyIndex = namePath[namePath.length - 1]; + + const parentObject = getValueByPath(schemaClone, parentPath); + if (!parentObject || !parentObject.properties) { + console.error('无法找到必选属性的父对象', parentObject); + return; + } + + const propertyKeys = Object.keys(parentObject.properties); + if (propertyIndex < 0 || propertyIndex >= propertyKeys.length) { + console.error('属性索引超出范围', propertyIndex); + return; } - updateRequired(current, requiredProperty, removed); + + const propertyName = propertyKeys[propertyIndex]; + updateRequired(parentObject, propertyName, removed); setSchema(schemaClone); } diff --git a/src/JsonSchemaEditor/utils.ts b/src/JsonSchemaEditor/utils.ts index e4d8603..791a1c9 100644 --- a/src/JsonSchemaEditor/utils.ts +++ b/src/JsonSchemaEditor/utils.ts @@ -180,6 +180,22 @@ export function getPropertyIndex(obj: any, propName: string): number { return keys.indexOf(propName); } +export function getValueByPath(obj: any, path: number[]): any { + if (path.length === 0) { + return obj; + } + let current = obj; + for (let i = 0; i < path.length; i++) { + const key = Object.keys(current)[path[i]]; + if (key === undefined) { + return undefined; + } else { + current = current[key]; + } + } + return current; +} + export const StringFormat = [ { value: 'date-time' }, { value: 'date' },