index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Icon } from '../Icon';
  4. export default class Open 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. const { height } = this.props;
  13. if (this.Text != null) {
  14. if (this.Text.offsetHeight > height) {
  15. this.setState({ more: true });
  16. }
  17. } else {
  18. setTimeout(() => {
  19. this.checkHeight();
  20. }, 1);
  21. }
  22. }
  23. render() {
  24. const { show, more } = this.state;
  25. const { up, down, height, className = '', children } = this.props;
  26. return (
  27. <div className={`super-open ${className} ${more ? 'more' : ''} ${!show ? 'hide' : ''}`}>
  28. <div
  29. className="hide-content"
  30. ref={ref => {
  31. this.Text = ref;
  32. }}
  33. style={{ height: more && !show ? height : null }}
  34. >
  35. {children}
  36. </div>
  37. {more && show && <span onClick={() => this.setState({ show: false })}>{up}</span>}
  38. {more && !show && <span onClick={() => this.setState({ show: true })}>{down}</span>}
  39. </div>
  40. );
  41. }
  42. }
  43. export class OpenText extends Component {
  44. render() {
  45. const { children } = this.props;
  46. return (
  47. <Open className="super-open-text" height={60} up={<Icon name="small-up" />} down={<Icon name="small-down" />}>
  48. {children}
  49. </Open>
  50. );
  51. }
  52. }