index.jsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, {Component} from 'react';
  2. import style from './index.less'
  3. import showImg from "@common/images/show.png";
  4. import hideImg from "@common/images/close.png";
  5. class RecommendInspect extends Component {
  6. constructor(props) {
  7. super(props)
  8. this.state = {
  9. showAll: false,
  10. }
  11. this.renderItem = this.renderItem.bind(this)
  12. }
  13. changeShowFlag() {
  14. this.setState({
  15. showAll: !this.state.showAll
  16. })
  17. }
  18. renderItem(item) {
  19. const { changeFlag } = this.props
  20. return <span>
  21. <input
  22. id={item.id + item.name}
  23. onChange={() =>changeFlag(item)}
  24. type="checkbox"
  25. checked={item.checked}
  26. />
  27. <label for={item.id + item.name}>{item.name}</label>
  28. </span>
  29. }
  30. render() {
  31. const { title,list, changeFlag, border } = this.props
  32. const { showAll } = this.state
  33. const listAll = list.map(item => {
  34. return this.renderItem(item)
  35. });
  36. let firstLineNum = 0; //第一行字数
  37. let secondLineNum = 0; //第二行字数
  38. const listHide = list.map((item, index) => {
  39. firstLineNum = firstLineNum + item.name.length + 2;
  40. if (firstLineNum > 26) {
  41. secondLineNum = secondLineNum + item.name.length + 2;
  42. if(secondLineNum > 20) {
  43. return;
  44. } else {
  45. return this.renderItem(item)
  46. }
  47. } else {
  48. return this.renderItem(item)
  49. }
  50. });
  51. return (
  52. <li className={style["inspectItem"]} style={border ? {"borderBottom": "1px solid #DFDFDF"} : ''}>
  53. <div className={style["title"]}>{title}:</div>
  54. <div className={style["content"]}>
  55. {list.length === 0
  56. ?<span >无</span>
  57. : showAll
  58. ? listAll
  59. : listHide}
  60. {secondLineNum > 21 ? (
  61. <span
  62. style={{ display: "inline-block" }}
  63. className={style["show"]}
  64. onClick={this.changeShowFlag.bind(this)}
  65. >
  66. {showAll ? '收起' : '更多'}
  67. <img src={showAll ? hideImg : showImg} />
  68. </span>
  69. ) : (
  70. ""
  71. )}
  72. </div>
  73. </li>
  74. )
  75. }
  76. }
  77. export default RecommendInspect;