index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { Component } from 'react';
  2. import { Icon, List, Avatar, Typography } from 'antd';
  3. import './index.less';
  4. import DragList from '@src/components/DragList';
  5. export default class QuestionNoList extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = { loading: 0 };
  9. this.setState({ questionNos: this.props.questionNos });
  10. }
  11. componentWillReceiveProps(nextProps) {
  12. const { loading } = this.state;
  13. this.setState({ questionNos: nextProps.questionNos, loading: loading + 1 });
  14. }
  15. deleteQuestion(index) {
  16. const { questionNos, loading } = this.state;
  17. questionNos.splice(index, 1);
  18. this.setState({ questionNos, loading: loading + 1 });
  19. this.props.onChange(questionNos);
  20. }
  21. orderQuestion(oldIndex, newIndex) {
  22. const { questionNos, loading } = this.state;
  23. const tmp = questionNos.splice(oldIndex, 1);
  24. if (newIndex === questionNos.length) {
  25. questionNos.push(tmp[0]);
  26. } else {
  27. questionNos.splice(newIndex, 0, tmp[0]);
  28. }
  29. this.setState({ questionNos, loading: loading + 1 });
  30. this.props.onChange(questionNos);
  31. }
  32. render() {
  33. return <DragList
  34. key={this.state.loading}
  35. loading={!!this.props.loading}
  36. dataSource={this.state.questionNos || []}
  37. handle={'.icon'}
  38. onMove={(oldIndex, newIndex) => {
  39. this.orderQuestion(oldIndex, newIndex);
  40. }}
  41. type={this.props.type}
  42. renderItem={(item, index) => {
  43. if (this.props.render) {
  44. return this.props.render(item, 'icon', () => {
  45. this.deleteQuestion(index);
  46. });
  47. }
  48. return <List.Item actions={[<a href={`/subject/question/${item.question.id}`} target='_blank'><Icon type='small-dash' /></a>, <Icon type='delete' onClick={() => {
  49. this.deleteQuestion(index);
  50. }} />, <Icon type='bars' className='icon' />]}>
  51. <List.Item.Meta
  52. avatar={<Avatar alt={index + 1} size='small' >{index + 1}</Avatar>}
  53. title={item.title}
  54. description={<Typography.Paragraph ellipsis={{ rows: 2, expandable: true }} disabled >{item.question ? item.question.description : ''}</Typography.Paragraph>}
  55. />
  56. </List.Item>;
  57. }} />;
  58. }
  59. }