Skip to content

Commit bd41adb

Browse files
authored
Merge pull request #16 from lin-mt/develop
feat: 支持自定义点击高级设置按钮后的行为,支持修改指定路径下的JsonSchema
2 parents a4de848 + e548f9e commit bd41adb

File tree

4 files changed

+288
-199
lines changed

4 files changed

+288
-199
lines changed

src/JsonSchemaEditor/SchemaItem/index.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ type SchemaItemProps = {
4646
isArrayItems?: boolean;
4747
isRequire?: boolean;
4848
schema: JSONSchema7;
49-
changeSchema?: (namePath: number[], value: any, propertyName: string) => void;
49+
changeSchema?: (
50+
namePath: number[],
51+
value: any,
52+
propertyName?: string,
53+
) => void;
5054
renameProperty?: (namePath: number[], name: string) => void;
5155
removeProperty?: (namePath: number[]) => void;
5256
addProperty?: (path: number[], isChild: boolean) => void;
@@ -55,6 +59,11 @@ type SchemaItemProps = {
5559
requiredProperty: string,
5660
removed: boolean,
5761
) => void;
62+
handleAdvancedSettingClick?: (
63+
namePath: number[],
64+
schema: JSONSchema7,
65+
propertyName?: string,
66+
) => boolean;
5867
};
5968

6069
function SchemaItem(props: SchemaItemProps) {
@@ -70,6 +79,7 @@ function SchemaItem(props: SchemaItemProps) {
7079
removeProperty,
7180
addProperty,
7281
isRequire,
82+
handleAdvancedSettingClick,
7383
} = props;
7484

7585
const [schema, setSchema] = useState(props.schema);
@@ -273,6 +283,18 @@ function SchemaItem(props: SchemaItemProps) {
273283
icon={<SettingOutlined />}
274284
style={{ color: 'green' }}
275285
onClick={() => {
286+
if (
287+
handleAdvancedSettingClick &&
288+
!handleAdvancedSettingClick(
289+
namePath,
290+
schema,
291+
isRoot || schema.type === 'object'
292+
? undefined
293+
: propertyName,
294+
)
295+
) {
296+
return;
297+
}
276298
setFormSchema(schema);
277299
setAdvancedModal(!advancedModal);
278300
}}
@@ -410,7 +432,7 @@ function SchemaItem(props: SchemaItemProps) {
410432
return;
411433
}
412434
if (isRoot || schema.type === 'object') {
413-
changeSchema(namePath, { ...schema, ...formSchema }, 'root');
435+
changeSchema(namePath, { ...schema, ...formSchema });
414436
setAdvancedModal(!advancedModal);
415437
return;
416438
}
@@ -821,7 +843,7 @@ function SchemaItem(props: SchemaItemProps) {
821843
break;
822844
}
823845
if (changeSchema) {
824-
changeSchema([], schema, 'root');
846+
changeSchema([], schema);
825847
setImportModal(!importModal);
826848
setImportValue(undefined);
827849
}

src/JsonSchemaEditor/index.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,48 @@ loader.config({ monaco });
3333

3434
## API
3535

36-
| 参数名称 | 描述 | 类型 | 默认值 |
37-
| -------- | --------------------- | ---------------------------------- | ----------------------------------------------------------------- |
38-
| onChange | JsonSchema 变更的回调 | (schema: JSONSchema7) => void | - |
39-
| data | 初始化组件数据 | JSONSchema7 \| string \| undefined | `{"type": "object", "properties": {"field": {"type": "string"}}}` |
36+
### onChange
37+
38+
JsonSchema 变更的回调。
39+
40+
类型:`(schema: JSONSchema7) => void`
41+
42+
默认值: `-`
43+
44+
### data
45+
46+
初始化组件数据。
47+
48+
类型:`JSONSchema7 | string | undefined`
49+
50+
默认值:
51+
52+
```json
53+
{
54+
"type": "object",
55+
"properties": {
56+
"field": {
57+
"type": "string"
58+
}
59+
}
60+
}
61+
```
62+
63+
### handleAdvancedSettingClick
64+
65+
点击`高级设置`按钮的回调,返回`false`:不使用默认表单,返回`true`:使用默认表单。
66+
67+
类型:`(namePath: number[], schema: JSONSchema7, propertyName?: string) => boolean`
68+
69+
默认值:`-`
70+
71+
说明:一般结合`组件引用`实现点击高级设置按钮后的自定义需求。
72+
73+
## 组件引用(ref)
74+
75+
```ts
76+
export interface JsonSchemaEditorHandle {
77+
/* 更新指定路径下的 JsonSchema */
78+
changeSchema: (namePath: number[], value: any, propertyName?: string) => void;
79+
}
80+
```

0 commit comments

Comments
 (0)