viewLayout.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from "react"
  2. import "./viewLayout.less"
  3. import ReactGridLayout from 'react-grid-layout'
  4. import { connect } from 'dva'
  5. import ChartView from './chartView'
  6. class ViewLayout extends React.PureComponent {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. items: [0, 1, 2, 3, 4].map(function (i, key, list) {
  11. return {
  12. i: i.toString(),
  13. x: i * 2,
  14. y: 0,
  15. w: i + 1,
  16. h: 2,
  17. add: i === (list.length - 1).toString()
  18. };
  19. }),
  20. newCounter: 0,
  21. width: 1100,
  22. editMode: true
  23. };
  24. }
  25. createElement = (item) => {
  26. const { dispatch } = this.props;
  27. const { code, layout, chartCode } = item;
  28. const removeStyle = {
  29. position: "absolute",
  30. right: "2px",
  31. top: 0,
  32. cursor: "pointer"
  33. };
  34. return (
  35. <div key={code} data-grid={{...layout, i: code}}>
  36. <ChartView chartCode={chartCode}/>
  37. <span className="remove" style={removeStyle} onClick={() => {
  38. dispatch({ type: 'dashboardDesigner/delete', item: item });
  39. }}>x</span>
  40. </div>
  41. );
  42. }
  43. onLayoutChange = (layout) => {
  44. const { dispatch } = this.props;
  45. dispatch({ type: 'dashboardDesigner/changeLayout', layout });
  46. }
  47. render() {
  48. const { width, editMode } = this.state;
  49. const { dashboardDesigner } = this.props;
  50. const { items } = dashboardDesigner;
  51. const children = items.map(item => this.createElement(item));
  52. return (
  53. <div>
  54. <ReactGridLayout
  55. width={width}
  56. cols={12}
  57. margin = {editMode ? [2, 2] : [0, 0]}
  58. rowHeight = {100}
  59. isDraggable={editMode}
  60. isResizable={editMode}
  61. onLayoutChange={this.onLayoutChange}
  62. onBreakpointChange={this.onBreakpointChange}
  63. verticalCompact={true}
  64. compactType='vertical'
  65. >
  66. {children}
  67. </ReactGridLayout>
  68. </div>
  69. );
  70. }
  71. }
  72. export default connect(({ present: { dashboardDesigner } }) => ({ dashboardDesigner }))(ViewLayout);