index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. if (this.Text.offsetHeight > 80) {
  16. this.setState({ more: true });
  17. }
  18. } else {
  19. setTimeout(() => {
  20. this.checkHeight();
  21. }, 1);
  22. }
  23. }
  24. render() {
  25. const { data } = this.props;
  26. const { show, more } = this.state;
  27. return (
  28. <div className={`qa-item ${more ? 'more' : ''} ${!show ? 'hide' : ''}`}>
  29. <div className="title">Q: {data.content}</div>
  30. <div
  31. ref={ref => {
  32. this.Text = ref;
  33. }}
  34. className="desc"
  35. >
  36. A: {data.answer}
  37. </div>
  38. {more && show && <Icon name="up" onClick={() => this.setState({ show: false })} />}
  39. {more && !show && <Icon name="down" onClick={() => this.setState({ show: true })} />}
  40. </div>
  41. );
  42. }
  43. }
  44. function QAList(props) {
  45. const { style, data = [], tabs = [], active } = props;
  46. return (
  47. <Module style={style} className="qa-list">
  48. <Tabs
  49. type="division"
  50. theme="theme"
  51. active={active}
  52. space={2.5}
  53. width={100}
  54. tabs={tabs}
  55. />
  56. {data.map(item => {
  57. return <QAItem data={item} />;
  58. })}
  59. </Module>
  60. );
  61. }
  62. QAList.propTypes = {};
  63. export default QAList;