Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/JsonSchemaEditor/SchemaItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
type SchemaItemProps = {
propertyName?: string;
nodeDepth?: number;
parentSchemaDepth?: number;
namePath?: number[];
isArrayItems?: boolean;
isRequire?: boolean;
Expand All @@ -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,
Expand All @@ -65,7 +60,6 @@ function SchemaItem(props: SchemaItemProps) {
renameProperty,
isArrayItems,
updateRequiredProperty,
parentSchemaDepth = 0,
removeProperty,
addProperty,
isRequire,
Expand Down Expand Up @@ -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);
}
}}
/>
Expand Down Expand Up @@ -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),
Expand All @@ -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}
Expand Down
40 changes: 24 additions & 16 deletions src/JsonSchemaEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,23 +168,31 @@ const JsonSchemaEditor = forwardRef<JsonSchemaEditorHandle, SchemaEditorProps>(
}
}

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);
}

Expand Down
16 changes: 16 additions & 0 deletions src/JsonSchemaEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down