index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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="title">Q: {data.content}</div>
  28. <div
  29. ref={ref => {
  30. this.Text = ref;
  31. }}
  32. className="desc"
  33. dangerouslySetInnerHTML={{ __html: `A: ${data.answer}` }}
  34. />
  35. {more && show && <Icon name="up" onClick={() => this.setState({ show: false })} />}
  36. {more && !show && <Icon name="down" onClick={() => this.setState({ show: true })} />}
  37. </div>
  38. );
  39. }
  40. }