-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathSchemaTable.js
More file actions
130 lines (123 loc) · 2.88 KB
/
SchemaTable.js
File metadata and controls
130 lines (123 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { Component } from 'react';
import { Table } from 'antd';
import json5 from 'json5';
import PropTypes from 'prop-types';
import { schemaTransformToTable } from '../../../common/schema-transformTo-table.js';
import _ from 'underscore';
import './index.scss';
const messageMap = {
desc: '备注',
default: '实例',
maximum: '最大值',
minimum: '最小值',
maxItems: '最大数量',
minItems: '最小数量',
maxLength: '最大长度',
minLength: '最小长度',
enum: '枚举',
enumDesc: '枚举备注',
uniqueItems: '元素是否都不同',
itemType: 'item 类型',
format: 'format',
itemFormat: 'format',
mock: 'mock',
nullable: '可为空',
const: '常量值'
};
const columns = [
{
title: '名称',
dataIndex: 'name',
key: 'name',
width: 200
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
width: 100,
render: (text, item) => {
// console.log('text',item.sub);
return text === 'array' ? (
<span>{item.sub ? item.sub.itemType || '' : 'array'} []</span>
) : (
<span>{text}</span>
);
}
},
{
title: '是否必须',
dataIndex: 'required',
key: 'required',
width: 80,
render: text => {
return <div>{text ? '必须' : '非必须'}</div>;
}
},
{
title: '默认值',
dataIndex: 'default',
key: 'default',
width: 80,
render: text => {
return <div>{_.isBoolean(text) ? text + '' : text}</div>;
}
},
{
title: '备注',
dataIndex: 'desc',
key: 'desc',
render: (text, item) => {
return _.isUndefined(item.childrenDesc) ? (
<span className="table-desc">{text}</span>
) : (
<span className="table-desc">{item.childrenDesc}</span>
);
}
},
{
title: '其他信息',
dataIndex: 'sub',
key: 'sub',
width: 180,
render: (text, record) => {
let result = text || record;
return Object.keys(result).map((item, index) => {
let name = messageMap[item];
let value = result[item];
let isShow = !_.isUndefined(result[item]) && !_.isUndefined(name);
return (
isShow && (
<p key={index}>
<span style={{ fontWeight: '700' }}>{name}: </span>
<span>{value.toString()}</span>
</p>
)
);
});
}
}
];
class SchemaTable extends Component {
static propTypes = {
dataSource: PropTypes.string
};
constructor(props) {
super(props);
}
render() {
let product;
try {
product = json5.parse(this.props.dataSource);
} catch (e) {
product = null;
}
if (!product) {
return null;
}
let data = schemaTransformToTable(product);
data = _.isArray(data) ? data : [];
return <Table bordered size="small" pagination={false} dataSource={data} columns={columns} />;
}
}
export default SchemaTable;