Skip to content

Commit 3cdeaca

Browse files
committed
v1.0.5
1 parent 8ea0941 commit 3cdeaca

File tree

7 files changed

+58
-60
lines changed

7 files changed

+58
-60
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export default () => {
2222
const [jsonData, setJsonData] = useState({});
2323
return (
2424
<JsonSchemaEditor
25-
mock={true}
2625
data={jsonData}
2726
onChange={(data) => {
2827
setJsonData(data);

package-lock.json

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JsonSchemaEditor/SchemaItem/index.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Option = Select.Option;
3434
type SchemaItemProps = {
3535
propertyName?: string;
3636
nodeDepth?: number;
37-
parentSchemaDepth?: number;
3837
namePath?: number[];
3938
isArrayItems?: boolean;
4039
isRequire?: boolean;
@@ -48,11 +47,7 @@ type SchemaItemProps = {
4847
renameProperty?: (namePath: number[], name: string) => void;
4948
removeProperty?: (namePath: number[]) => void;
5049
addProperty?: (path: number[], isChild: boolean) => void;
51-
updateRequiredProperty?: (
52-
path: number[],
53-
requiredProperty: string,
54-
removed: boolean,
55-
) => void;
50+
updateRequiredProperty?: (namePath: number[], removed: boolean) => void;
5651
handleAdvancedSettingClick?: (
5752
namePath: number[],
5853
schema: JSONSchema7,
@@ -66,7 +61,6 @@ function SchemaItem(props: SchemaItemProps) {
6661
renameProperty,
6762
isArrayItems,
6863
updateRequiredProperty,
69-
parentSchemaDepth = 0,
7064
removeProperty,
7165
addProperty,
7266
isRequire,
@@ -165,11 +159,7 @@ function SchemaItem(props: SchemaItemProps) {
165159
style={{ padding: 0 }}
166160
onChange={(checked) => {
167161
if (updateRequiredProperty && propertyName) {
168-
updateRequiredProperty(
169-
namePath.slice(0, parentSchemaDepth),
170-
propertyName,
171-
!checked,
172-
);
162+
updateRequiredProperty(namePath, !checked);
173163
}
174164
}}
175165
/>
@@ -344,9 +334,8 @@ function SchemaItem(props: SchemaItemProps) {
344334
{...props}
345335
isRequire={schema.required?.includes(name)}
346336
isArrayItems={false}
347-
defaultExpand={props.defaultExpand}
337+
defaultExpand={defaultExpand}
348338
nodeDepth={nodeDepth + 1}
349-
parentSchemaDepth={!isRoot ? parentSchemaDepth + 2 : 0}
350339
namePath={namePath.concat(
351340
getPropertyIndex(schema, 'properties'),
352341
getPropertyIndex(schema.properties, name),
@@ -366,8 +355,7 @@ function SchemaItem(props: SchemaItemProps) {
366355
isRequire={false}
367356
isArrayItems={true}
368357
nodeDepth={nodeDepth + 1}
369-
defaultExpand={props.defaultExpand}
370-
parentSchemaDepth={!isRoot ? parentSchemaDepth + 1 : 0}
358+
defaultExpand={defaultExpand}
371359
propertyName={'items'}
372360
namePath={namePath.concat(getPropertyIndex(schema, 'items'))}
373361
schema={schema.items as JSONSchema7}

src/JsonSchemaEditor/defaultMock.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,7 @@ export const defaultSchemaMock = {
33
type: 'object',
44
properties: {
55
field: {
6-
title: '默认标题',
7-
type: 'object',
8-
properties: {
9-
name: {
10-
title: '',
11-
type: 'string',
12-
},
13-
},
14-
},
15-
col: {
16-
title: '默认标题',
17-
type: 'object',
18-
properties: {
19-
age: {
20-
title: '',
21-
type: 'string',
22-
description: '默认描述',
23-
},
24-
},
6+
type: 'string',
257
},
268
},
279
};

src/JsonSchemaEditor/index.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ yarn add @quiet-front-end/json-schema-editor-arco
1010
```jsx
1111
import { JsonSchemaEditor } from '@quiet-front-end/json-schema-editor-arco';
1212

13-
export default () => <JsonSchemaEditor defaultExpand={false} />;
13+
export default () => <JsonSchemaEditor />;
1414
```
1515

1616
## Notice
@@ -59,6 +59,14 @@ JsonSchema 变更的回调。
5959
}
6060
```
6161

62+
### defaultExpand
63+
64+
默认是否展开节点。
65+
66+
类型:`boolean`
67+
68+
默认值:`true`
69+
6270
### handleAdvancedSettingClick
6371

6472
点击`高级设置`按钮的回调,返回`false`:不使用默认表单,返回`true`:使用默认表单。

src/JsonSchemaEditor/index.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React, {
1010
import { defaultSchemaMock } from './defaultMock';
1111
import SchemaItem from './SchemaItem';
1212
import { JSONSchema7, SchemaEditorProps } from './types';
13-
import { getDefaultSchema, inferSchema } from './utils';
13+
import { getDefaultSchema, getValueByPath, inferSchema } from './utils';
1414

1515
export interface JsonSchemaEditorHandle {
1616
changeSchema: (namePath: number[], value: any, propertyName?: string) => void;
@@ -161,23 +161,31 @@ const JsonSchemaEditor = forwardRef<JsonSchemaEditorHandle, SchemaEditorProps>(
161161
}
162162
}
163163

164-
function updateRequiredProperty(
165-
path: number[],
166-
requiredProperty: string,
167-
removed: boolean,
168-
) {
169-
// console.log("updateRequiredProperty", path, requiredProperty, removed);
164+
function updateRequiredProperty(namePath: number[], removed: boolean) {
165+
if (namePath.length < 2) {
166+
console.error('路径长度不足,无法更新必选属性');
167+
return;
168+
}
169+
170170
let schemaClone = _.cloneDeep(schema);
171-
let current: any = schemaClone;
172-
for (let i = 0; i < path.length; i++) {
173-
const index = path[i];
174-
const keys = Object.keys(current);
175-
if (typeof current[keys[index]] === 'undefined') {
176-
current[keys[index]] = {};
177-
}
178-
current = current[keys[index]];
171+
172+
const parentPath = namePath.slice(0, -2);
173+
const propertyIndex = namePath[namePath.length - 1];
174+
175+
const parentObject = getValueByPath(schemaClone, parentPath);
176+
if (!parentObject || !parentObject.properties) {
177+
console.error('无法找到必选属性的父对象', parentObject);
178+
return;
179+
}
180+
181+
const propertyKeys = Object.keys(parentObject.properties);
182+
if (propertyIndex < 0 || propertyIndex >= propertyKeys.length) {
183+
console.error('属性索引超出范围', propertyIndex);
184+
return;
179185
}
180-
updateRequired(current, requiredProperty, removed);
186+
187+
const propertyName = propertyKeys[propertyIndex];
188+
updateRequired(parentObject, propertyName, removed);
181189
setSchema(schemaClone);
182190
}
183191

src/JsonSchemaEditor/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import { JsonSchema } from 'json-schema-library';
33
import _ from 'lodash';
44
import { JSONSchema7Definition, JSONSchema7TypeName } from './types';
55

6+
export function getValueByPath(obj: any, path: number[]): any {
7+
if (path.length === 0) {
8+
return obj;
9+
}
10+
let current = obj;
11+
for (let i = 0; i < path.length; i++) {
12+
const key = Object.keys(current)[path[i]];
13+
if (key === undefined) {
14+
return undefined;
15+
} else {
16+
current = current[key];
17+
}
18+
}
19+
return current;
20+
}
21+
622
// 解析 JSON Schema 中的 $ref
723
function resolveRef(schema: JsonSchema, rootSchema: JsonSchema): JsonSchema {
824
if (schema.$ref) {

0 commit comments

Comments
 (0)