index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Icon, Button} from 'antd';
  3. import './index.css';
  4. import Change from './change';
  5. const Search = Input.Search;
  6. class Schema extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. currentSchema: 'magazine',
  11. currentTable: 'user',
  12. switch: true
  13. }
  14. }
  15. changeSchema = (schema) => {
  16. return () => {
  17. this.setState({
  18. currentSchema: schema
  19. })
  20. }
  21. };
  22. changeTable = (table) => {
  23. return () => {
  24. this.setState({
  25. currentTable: table
  26. })
  27. }
  28. };
  29. findColumns = () => {
  30. let schema = this.props.schemas.find(schema => schema.name === this.state.currentSchema);
  31. if(schema === undefined) return [];
  32. // if user insert a new schema, after merge to schemas with a {name: 'xx', tables: []}
  33. // you can find the following table is undefined.
  34. // so give a state here if you want add a tip!
  35. else {
  36. let table = schema.tables.find(table => table.name === this.state.currentTable);
  37. if(table === undefined) return [];
  38. else return table.cols;
  39. }
  40. };
  41. render() {
  42. return (
  43. <div>
  44. <Row>
  45. <Col span={6}>
  46. <div className='wrapper'>
  47. <div className='current'>{this.state.currentSchema}</div>
  48. {
  49. this.state.switch?
  50. <Button className='add' type='dashed' icon="plus" onClick={()=>{this.setState({switch: false})}}>Add Schema</Button> :
  51. <Search
  52. className='add'
  53. placeholder="input schema_name"
  54. enterButton="Confirm"
  55. onSearch={value => {
  56. this.setState({
  57. switch: true,
  58. currentSchema: value,
  59. currentTable: ''
  60. });
  61. this.props.addSchema(value);
  62. }}
  63. disabled={this.state.switch}
  64. />
  65. }
  66. </div>
  67. <Search
  68. placeholder="search schema"
  69. enterButton
  70. onSearch={value => console.log(value)}
  71. style={{marginTop: 10}}
  72. />
  73. {
  74. this.props.schemas.map((schema) => {
  75. return <div key={schema.name} onClick={this.changeSchema(schema.name)} className='title'>
  76. <Row>
  77. <Col span={22}>Schemas: ({schema.name}) ({schema.tables.length})</Col>
  78. <Col span={2}><Button onClick={() => this.setState({currentTable: 'add'})} type="primary"
  79. shape="circle" icon="plus" size='small'/></Col>
  80. </Row>
  81. {
  82. schema.tables.map((table) =>
  83. <p onClick={this.changeTable(table.name)} key={table.name} className='show'><Icon type="ordered-list" theme="outlined" /> {table.name}
  84. </p>
  85. )
  86. }
  87. </div>
  88. })
  89. }
  90. </Col>
  91. <Col span={18}>
  92. {
  93. this.state.currentTable === 'add' ?
  94. <AddTable currentSchema={this.state.currentSchema} addTable={this.props.addTable} deleteTable={this.props.deleteTable}/> :
  95. <ShowTable currentSchema={this.state.currentSchema} currentTable={this.state.currentTable}
  96. columns={this.findColumns()} addTable={this.props.addTable} deleteTable={this.props.deleteTable}/>
  97. }
  98. </Col>
  99. </Row>
  100. </div>
  101. )
  102. }
  103. }
  104. export default Schema;
  105. class AddTable extends Component {
  106. constructor(props) {
  107. super(props);
  108. this.state = {
  109. columns: []
  110. }
  111. }
  112. render() {
  113. return (
  114. <div style={{margin: 10}}>
  115. <div style={{fontSize: 30}}>Add a new table to <b>{this.props.currentSchema}</b></div>
  116. <Change currentSchema={this.props.currentSchema} currentTable={this.props.currentTable} columns={this.state.columns} addTable={this.props.addTable} deleteTable={this.props.deleteTable}/>
  117. </div>
  118. )
  119. }
  120. }
  121. class ShowTable extends Component {
  122. render() {
  123. return (
  124. <div style={{margin: 10}}>
  125. <div style={{fontSize: 30}}>{this.props.currentSchema} > {this.props.currentTable}</div>
  126. <Change currentSchema={this.props.currentSchema} currentTable={this.props.currentTable} columns={this.props.columns} addTable={this.props.addTable} deleteTable={this.props.deleteTable}/>
  127. </div>
  128. )
  129. }
  130. }