forked from nsjcy/frontEnd/nsjcy

Mr Ke
2020-05-27 58ae2ba21efcd85df331cf996a94038a77302b51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from 'react';
import DragSort from '../../libs/DragSort';
import './index.scss';
 
const tabs = [{
  name: '题目控件',
}, {
  name: '问题大纲',
}, ];
 
const subjects = [{
  name: '单选题',
  type: 'radio',
  icon: 'icon-danxuanicon',
},
//  {
//   name: '下拉题',
//   type: 'dropdown',
//   icon: 'icon-xialaicon',
// },
 {
  name: '多选题',
  type: 'checkbox',
  icon: 'icon-duoxuan-icon',
}, 
// {
//   name: '单行文本题',
//   type: 'text',
//   icon: 'icon-danhangicon',
// }, {
//   name: '多行文本题',
//   type: 'textarea',
//   icon: 'icon-duohangicon',
// }, {
//   name: '填空题',
//   type: 'input',
//   icon: 'icon-tiankongtiicon',
// }, 
];
 
class QuestionnairSiderbar extends React.PureComponent {
  state = {
    tabIndex: 0,
    curMoveItem: null,
    editors: [],
  };
 
  componentWillReceiveProps(nextProps) {
    this.setState({
      editors: nextProps.editors,
    })
  }
 
  toggleTab = (index) => {
    this.setState({
      tabIndex: index,
    })
  }
 
  switchIcon = (type) => {
    switch (type) {
      case 'radio':
        return 'icon-danxuanicon';
      case 'dropdown':
        return 'icon-xialaicon';
      case 'checkbox':
        return 'icon-duoxuan-icon';
      case 'textarea':
        return 'icon-duohangicon';
      case 'text':
        return 'icon-danhangicon';
      case 'input':
        return 'icon-tiankongtiicon';
    }
  }
 
  handleDragMove = (editors, from, to) => {
    const {
      onDragOutline,
    } = this.props;
    if (onDragOutline) {
      onDragOutline(editors)
    }
    this.setState({
      curMoveItem: to,
      editors,
    })
  }
 
  handleDragEnd = () => {
    this.setState({
      curMoveItem: null,
    })
  }
 
  clickOutline = (index) => {
    const {
      onClickOutline,
    } = this.props;
    if (onClickOutline) {
      onClickOutline(index)
    }
  }
 
  render() {
    this.tabs = [];
    const {
      onSelectEditor,
    } = this.props;
    const {
      tabIndex,
      editors,
      curMoveItem,
    } = this.state;
    return (
      <div className='questionnair-siderbar'>
        <div className='siderbar-tab'>
            {tabs.map((data, index) => {
              return (
                <div
                  key={data.name}
                  className={`tab-item ${tabIndex === index && 'tab-item-active'}`}
                  onClick={() => this.toggleTab(index)}
                  ref={(tab) => tab && this.tabs.push(tab)}>
                    {data.name}
                </div>
              )
            })}
        </div>
        <div className='siderbar-menu'>
          <div className='siderbar-menu-content' style={{ display:  0 !== tabIndex && 'none' }}>
            {subjects.map((data, index) => {
              return (
                <div
                  key={data.name}
                  className='siderbar-menu-editors'
                  onClick={() => onSelectEditor(data.type)}>
                    <i className={ `iconfont ${data.icon} siderbar-menu-icon`}></i>
                  <span>{data.name}</span>
                </div>
              )
            })}
          </div>
          <div className='siderbar-menu-content' style={{ display:  1 !== tabIndex && 'none', paddingTop: 12 }}>
            <DragSort
              draggable={true}
              data={editors}
              onDragEnd={this.handleDragEnd}
              onDragMove={this.handleDragMove}>
              {editors.map((data, index) => {
                return (
                  <div
                    className='siderbar-menu-summary'
                    onClick={() => this.clickOutline(index)}
                    style={{
                      border: 'none',
                      cursor: 'move'
                    }}
                    key={data.questionId}>
                    <i className={`iconfont ${this.switchIcon(data.type)} siderbar-menu-icon`}></i>
                    {data.type === 'input' ? (
                      <span className='summary-text'>{data.completionForwards}____{data.completionBackwards}</span>
                    ) : (
                      <span className='summary-text'>{data.title}</span>
                    )}
                    <div
                      className='summary-drag-mask'
                      style={{
                        background: curMoveItem === index ? 'rgba(245,245,245,0.50)' : ''
                      }}>
                    </div>
                  </div>
                )
              })}
            </DragSort>
          </div>
        </div>
      </div>
    )
  }
}
 
export default QuestionnairSiderbar;