index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, {Component} from 'react';
  2. import {Select, Input, Icon, Button, notification} from 'antd';
  3. const Option = Select.Option;
  4. class Change extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. currentSchema: props.currentSchema,
  9. currentTable: props.currentTable,
  10. remark: props.remark,
  11. columns: props.columns,
  12. newColName: '',
  13. newColType: 'type',
  14. types: ['ID', 'String', 'Int', 'Float'],
  15. descriptions: ['description', 'key', 'non-null', 'non-null-list']
  16. }
  17. }
  18. handleNameChange = (index) => {
  19. return (e) => {
  20. let columns = this.state.columns;
  21. columns[index].name = e.target.value;
  22. this.setState({
  23. columns
  24. })
  25. };
  26. };
  27. handleNameNew = (e) => {
  28. this.setState({
  29. newColName: e.target.value,
  30. showNewColumn: false
  31. })
  32. };
  33. handleTypeChange = (index) => {
  34. return (value) => {
  35. let columns = this.state.columns;
  36. columns[index].type = value;
  37. this.setState({
  38. columns
  39. });
  40. }
  41. };
  42. handleTypeNew = (value) => {
  43. if (this.state.newColName !== '') {
  44. let columns = this.state.columns;
  45. columns.push({name: this.state.newColName, type: value, desc: 'description'});
  46. this.setState({
  47. columns,
  48. newColName: '',
  49. newColType: 'type'
  50. })
  51. } else {
  52. notification['warning']({
  53. message: 'Notification',
  54. description: 'Input a name first.',
  55. });
  56. }
  57. };
  58. handleDescChange = (index) => {
  59. return (value) => {
  60. let columns = this.state.columns;
  61. columns[index].desc = value;
  62. this.setState({
  63. columns
  64. });
  65. };
  66. };
  67. handleColDelete = (index) => {
  68. return () => {
  69. let columns = this.state.columns;
  70. columns.splice(index, 1);
  71. this.setState({
  72. columns
  73. });
  74. }
  75. };
  76. componentWillReceiveProps(next) {
  77. this.setState({
  78. currentTable: next.currentTable,
  79. currentSchema: next.currentSchema,
  80. columns: next.columns,
  81. remark: next.remark
  82. });
  83. };
  84. render() {
  85. return (
  86. <div style={{margin: 20}}>
  87. <div style={{marginBottom: 20}}>
  88. <span className='table-title'> Table name</span>
  89. <Input value={this.state.currentTable} placeholder="table_name" style={{width: 120}} onChange={(e) => {
  90. this.setState({currentTable: e.target.value})
  91. }}/>
  92. </div>
  93. <div style={{marginBottom: 20}}>
  94. <span className='table-title'> Table remark</span>
  95. <Input value={this.state.remark} placeholder="table_remark" style={{width: 250}} onChange={(e) => {
  96. this.setState({remark: e.target.value})
  97. }}/>
  98. </div>
  99. <div>
  100. <span className='table-title'> Table columns</span>
  101. <span className='column-title'>name</span>
  102. <span className='column-title'>type</span>
  103. <span className='column-title'>description</span>
  104. {
  105. this.state.columns.map((col, index) =>
  106. <div key={index} style={{marginBottom: 3}}>
  107. <Input style={{width: 120, marginRight: 10}} value={col.name} onChange={this.handleNameChange(index)}/>
  108. <Select defaultValue={col.type} style={{width: 120, marginRight: 10}}
  109. onChange={this.handleTypeChange(index)}>
  110. {
  111. this.state.types.map((value) =>
  112. <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
  113. )
  114. }
  115. </Select>
  116. <Select defaultValue={col.desc} style={{width: 120, marginRight: 10}}
  117. onChange={this.handleDescChange(index)}>
  118. {
  119. this.state.descriptions.map((value) =>
  120. <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
  121. )
  122. }
  123. </Select>
  124. <Icon type="delete" theme="outlined" style={{cursor: 'pointer'}}
  125. onClick={this.handleColDelete(index)}/>
  126. </div>
  127. )
  128. }
  129. <div>
  130. <Input placeholder="column_name" style={{width: 120, marginRight: 10}} onChange={this.handleNameNew}
  131. value={this.state.newColName}/>
  132. <Select defaultValue="type" style={{width: 120, marginRight: 10}} onChange={this.handleTypeNew}
  133. value={this.state.newColType}>
  134. {
  135. this.state.types.map((value) =>
  136. <Option key={Math.random()} value={value.toLowerCase()}>{value}</Option>
  137. )
  138. }
  139. </Select>
  140. </div>
  141. </div>
  142. <div style={{marginTop: 10}}>
  143. <Button type="primary" onClick={() => {
  144. this.props.addTable(this.state.columns, this.state.remark, this.state.currentTable, this.state.currentSchema);
  145. }}>
  146. SUBMIT
  147. </Button>
  148. <Button style={{marginLeft: 10}} type="danger" onClick={() => {
  149. this.props.deleteTable(this.state.currentTable, this.state.currentSchema);
  150. }}>DELETE</Button>
  151. </div>
  152. </div>
  153. )
  154. }
  155. }
  156. export default Change;