mock平台

SchemaTable.js 2.8KB

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