index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = [] } = props;
  46. return (
  47. <Module style={style} className="qa-list">
  48. <Tabs
  49. type="division"
  50. theme="theme"
  51. space={2.5}
  52. width={100}
  53. tabs={[{ key: 'qx', name: '解析详情' }, { key: 'chinese', name: '中文语意' }]}
  54. />
  55. {data.map(item => {
  56. return <QAItem data={item} />;
  57. })}
  58. </Module>
  59. );
  60. }
  61. QAList.propTypes = {};
  62. export default QAList;