index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Module from '../Module';
  4. import Tabs from '../Tabs';
  5. import Icon from '../Icon';
  6. class QAItem extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.Text = null;
  10. this.state = { show: false, more: false };
  11. this.checkHeight();
  12. }
  13. checkHeight() {
  14. if (this.Text != null) {
  15. console.log(this.Text.offsetHeight);
  16. if (this.Text.offsetHeight > 80) {
  17. this.setState({ more: true });
  18. }
  19. } else {
  20. setTimeout(() => {
  21. this.checkHeight();
  22. }, 1);
  23. }
  24. }
  25. render() {
  26. const { data } = this.props;
  27. const { show, more } = this.state;
  28. return (
  29. <div className={`qa-item ${more ? 'more' : ''} ${!show ? 'hide' : ''}`}>
  30. <div className="title">Q: {data.content}</div>
  31. <div
  32. ref={ref => {
  33. this.Text = ref;
  34. }}
  35. className="desc"
  36. >
  37. A: {data.answer}
  38. </div>
  39. {more && show && <Icon name="up" onClick={() => this.setState({ show: false })} />}
  40. {more && !show && <Icon name="down" onClick={() => this.setState({ show: true })} />}
  41. </div>
  42. );
  43. }
  44. }
  45. function QAList(props) {
  46. const { style, data = [], tabs = [], active } = props;
  47. return (
  48. <Module style={style} className="qa-list">
  49. {tabs.length > 0 && <Tabs
  50. type="division"
  51. theme="theme"
  52. active={active}
  53. space={2.5}
  54. width={100}
  55. tabs={tabs}
  56. />}
  57. {data.map(item => {
  58. return <QAItem data={item} />;
  59. })}
  60. </Module>
  61. );
  62. }
  63. QAList.propTypes = {};
  64. export default QAList;