index.js 2.9 KB

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