index.js 2.5 KB

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