index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.searchQuestion(this.props.ids, 'ids');
  10. this.setState({ questionNos: this.props.questionNos });
  11. }
  12. // componentWillReceiveProps(nextProps) {
  13. // if (this.props.ids !== nextProps.ids) {
  14. // this.searchQuestion(nextProps.ids, 'ids');
  15. // } else if (this.props.nos !== nextProps.nos) {
  16. // this.searchQuestion(nextProps.nos, 'nos');
  17. // }
  18. // }
  19. componentWillReceiveProps(nextProps) {
  20. const { loading } = this.state;
  21. this.setState({ questionNos: nextProps.questionNos, loading: loading + 1 });
  22. }
  23. deleteQuestion(index) {
  24. const { questionNos, loading } = this.state;
  25. questionNos.splice(index, 1);
  26. this.setState({ questionNos, loading: loading + 1 });
  27. this.props.onChange(questionNos);
  28. }
  29. orderQuestion(oldIndex, newIndex) {
  30. const { questionNos, loading } = this.state;
  31. const tmp = questionNos[oldIndex];
  32. questionNos[oldIndex] = questionNos[newIndex];
  33. questionNos[newIndex] = tmp;
  34. this.setState({ questionNos, loading: loading + 1 });
  35. this.props.onChange(questionNos);
  36. }
  37. // searchQuestion(values, field = 'nos') {
  38. // // console.log('search', values, field);
  39. // if (!values || values.length === 0) {
  40. // this.setState({ questionNos: [] });
  41. // return;
  42. // }
  43. // // 查找练习题目
  44. // Question.listNo({ [field]: values, module: this.props.module }).then(result => {
  45. // const { loading } = this.state;
  46. // const map = getMap(result, 'no');
  47. // const questionNos = values.map(no => map[no]).filter(row => row);
  48. // this.setState({ questionNos, loading: loading + 1 });
  49. // this.props.onChange(questionNos);
  50. // });
  51. // }
  52. render() {
  53. return <DragList
  54. key={this.state.loading}
  55. loading={!!this.props.loading}
  56. dataSource={this.state.questionNos || []}
  57. handle={'.icon'}
  58. onMove={(oldIndex, newIndex) => {
  59. this.orderQuestion(oldIndex, newIndex);
  60. }}
  61. type={this.props.type}
  62. renderItem={(item, index) => {
  63. if (this.props.render) {
  64. return this.props.render(item, 'icon', () => {
  65. this.deleteQuestion(index);
  66. });
  67. }
  68. return <List.Item actions={[<Icon type='delete' onClick={() => {
  69. this.deleteQuestion(index);
  70. }} />, <Icon type='bars' className='icon' />]}>
  71. <List.Item.Meta
  72. avatar={<Avatar alt={index + 1} size='small' >{index + 1}</Avatar>}
  73. title={item.title}
  74. description={<Typography.Text ellipsis disabled>{item.description}</Typography.Text>}
  75. />
  76. </List.Item>;
  77. }} />;
  78. }
  79. }