configForm.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React from 'react'
  2. import { connect } from 'dva'
  3. import { Form, Input, Divider, Button, Icon, Collapse, Spin, Select, Checkbox } from 'antd'
  4. const { TextArea } = Input;
  5. // const ConfigForm = ({ dashboardDesigner, dispatch }) => {
  6. class ConfigForm extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. selectedDataSource: null,
  11. selectedColumn: null,
  12. activeKey: [],
  13. editing: false
  14. };
  15. }
  16. addRelationColumn = () => {
  17. const { dispatch } = this.props;
  18. dispatch({ type: 'dashboardDesigner/addRelationColumn' });
  19. }
  20. deleteRelationColumn = (e) => {
  21. const { dispatch } = this.props;
  22. const code = e.target.dataset.code;
  23. dispatch({ type: 'dashboardDesigner/deleteRelationColumn', code });
  24. }
  25. render() {
  26. const { dashboardDesigner, dispatch } = this.props;
  27. const { activeKey, editing, selectedDataSource, selectedColumn } = this.state;
  28. const { relationColumns, dataSources, columnFetching } = dashboardDesigner;
  29. return <Form className='config-form'>
  30. <Divider>基础设置</Divider>
  31. <Form.Item label='备注'>
  32. <TextArea autosize={{ minRows: 2, maxRows: 6 }} />
  33. </Form.Item>
  34. <Divider>自定义过滤字段</Divider>
  35. <div className='filtercolumns'>
  36. <Collapse key='filtercolumnscollapse' activeKey={activeKey} onChange={k => {
  37. this.setState({
  38. selectedDataSource: null,
  39. selectedColumn: null,
  40. activeKey: k[k.length - 1], // 保持只有一个展开项
  41. });
  42. }}>
  43. {
  44. relationColumns.map((r, ri) => (
  45. <Collapse.Panel
  46. key={r.code}
  47. disabled={editing}
  48. header={<Form.Item className='filtercolumn-name' label='名称' labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
  49. <Input size='small' value={r.name}
  50. onChange={(e) => {
  51. dispatch({ type: 'dashboardDesigner/setRelationColumn', code: r.code, relationColumn: { ...r, name: e.target.value } });
  52. }} onFocus={() => {
  53. this.setState({
  54. editing: true,
  55. })
  56. }} onBlur={() => {
  57. this.setState({
  58. editing: false
  59. })
  60. }}/>
  61. </Form.Item>}
  62. >
  63. {(dataSources.length > 0 ? <div className='filtercolumn-relation'>
  64. {columnFetching && <div className='loading'>
  65. <Spin />
  66. </div>}
  67. <div className='datasources'>
  68. { dataSources.map((d, di) => (
  69. <Button
  70. className='datasource'
  71. key={d.code}
  72. type={!!selectedDataSource && selectedDataSource.code === d.code ? 'primary' : 'default'}
  73. onClick={() => {
  74. this.setState({
  75. selectedDataSource: {
  76. code: d.code,
  77. name: d.name
  78. },
  79. selectedColumn: null
  80. });
  81. dispatch({ type: 'dashboardDesigner/remoteGetColumns', dataSourceCode: d.code });
  82. }}
  83. >
  84. <span className='label'>{d.name}</span>
  85. {r.relations.findIndex(r => r.dataSource.code === d.code) !== -1 && <Icon type='check-circle' theme={r.relations[0].dataSource.code === d.code ? 'filled' : 'outlined'} />}
  86. </Button>
  87. )) }
  88. </div>
  89. <div className='columns'>
  90. { !!selectedDataSource &&
  91. !!dataSources.find(d => d.code === selectedDataSource.code) &&
  92. !!dataSources.find(d => d.code === selectedDataSource.code).columns &&
  93. dataSources.find(d => d.code === selectedDataSource.code).columns.filter(c => {
  94. if(!!r.relations[0]) {
  95. // 选中的不是第一个
  96. if(r.relations[0].dataSource.code !== selectedDataSource.code) {
  97. // 只能选用同类型的列
  98. return c.type === r.relations[0].column.type;
  99. }else {
  100. return true;
  101. }
  102. }else {
  103. return true;
  104. }
  105. }).map(c => (
  106. <Button
  107. className='column'
  108. type={!!selectedColumn && selectedColumn.name === c.name ? 'primary' : 'default'}
  109. key={c.name}
  110. onClick={() => {
  111. this.setState({
  112. selectedColumn: {
  113. name: c.name,
  114. label: c.label
  115. }
  116. }, () => {
  117. let { selectedColumn } = this.state;
  118. const { relations } = r;
  119. let idx = relations.findIndex(r => r.dataSource.code === selectedDataSource.code);
  120. if(idx === -1){
  121. relations.push({
  122. dataSource: {
  123. code: selectedDataSource.code,
  124. name: selectedDataSource.name
  125. },
  126. column: {
  127. label: c.label,
  128. name: c.name,
  129. type: c.type
  130. }
  131. });
  132. }else {
  133. let cr = relations[idx];
  134. if(cr.column.name === selectedColumn.name) {
  135. relations.splice(idx, 1);
  136. }else {
  137. relations[idx] = {
  138. dataSource: {
  139. code: selectedDataSource.code,
  140. name: selectedDataSource.name
  141. },
  142. column: {
  143. code: c.code,
  144. name: c.name,
  145. type: c.type
  146. }
  147. };
  148. }
  149. }
  150. let index = relationColumns.findIndex(rc => rc.code === r.code);
  151. relationColumns[index] = { ...r, relations };
  152. dispatch({ type: 'dashboardDesigner/setField', name: 'relationColumns', value: relationColumns });
  153. });
  154. }}
  155. >
  156. <span className='label'>{c.label}</span>
  157. {r.relations.findIndex(r => r.dataSource.code === selectedDataSource.code) !== -1 && r.relations[r.relations.findIndex(r => r.dataSource.code === selectedDataSource.code)].column.name === c.name && <Icon type='check-circle' theme={r.relations[0].column.name === c.name ? 'filled' : 'outlined'} />}
  158. </Button>
  159. )) }
  160. </div>
  161. </div> : <div className='filtercolumn-empty'>
  162. 无关联数据源
  163. </div>)}
  164. <div className='filtercolumn-delete'>
  165. <Button type='danger' className='delbtn' data-code={r.code} onClick={this.deleteRelationColumn}>
  166. <Icon type='delete' theme='outlined' />删除
  167. </Button>
  168. </div>
  169. </Collapse.Panel>
  170. ))
  171. }
  172. </Collapse>
  173. <div className='bottom-btns'>
  174. <Button className='addbtn' onClick={this.addRelationColumn}>
  175. <Icon type='plus' theme='outlined' />添加
  176. </Button>
  177. </div>
  178. </div>
  179. </Form>
  180. }
  181. }
  182. export default connect(({ present: { dashboardDesigner } }) => ({ dashboardDesigner }))(ConfigForm);