index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Icon from '../Icon';
  4. export default class OtherAnswer extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.Text = null;
  8. this.state = { show: false, more: false };
  9. this.checkHeight();
  10. }
  11. checkHeight() {
  12. if (this.Text != null) {
  13. if (this.Text.offsetHeight > 80) {
  14. this.setState({ more: true });
  15. }
  16. } else {
  17. setTimeout(() => {
  18. this.checkHeight();
  19. }, 1);
  20. }
  21. }
  22. render() {
  23. const { data } = this.props;
  24. const { show, more } = this.state;
  25. return (
  26. <div className={`other-answer ${more ? 'more' : ''} ${!show ? 'hide' : ''}`}>
  27. <div className="small-tag">提问</div>
  28. <div className="title">{data.content}</div>
  29. <div className="small-tag">回答</div>
  30. <div
  31. ref={ref => {
  32. this.Text = ref;
  33. }}
  34. className="desc"
  35. dangerouslySetInnerHTML={{ __html: data.answer }}
  36. />
  37. {more && show && <Icon name="up" onClick={() => this.setState({ show: false })} />}
  38. {more && !show && <Icon name="down" onClick={() => this.setState({ show: true })} />}
  39. </div>
  40. );
  41. }
  42. }