1
0

index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. open() {
  24. const { onOpen } = this.props;
  25. this.setState({ show: true });
  26. if (onOpen) onOpen();
  27. }
  28. close() {
  29. const { onClose } = this.props;
  30. this.setState({ show: false });
  31. if (onClose) onClose();
  32. }
  33. render() {
  34. const { show, more } = this.state;
  35. const { up, down, height, className = '', children } = this.props;
  36. return (
  37. <div className={`super-open ${className} ${more ? 'more' : ''} ${!show ? 'hide' : ''}`}>
  38. <div
  39. className="hide-content"
  40. ref={ref => {
  41. this.Text = ref;
  42. }}
  43. style={{ height: more && !show ? height : null }}
  44. >
  45. {children}
  46. </div>
  47. {more && show && <span onClick={() => this.close()}>{up}</span>}
  48. {more && !show && <span onClick={() => this.open()}>{down}</span>}
  49. </div>
  50. );
  51. }
  52. }
  53. export class OpenText extends Component {
  54. render() {
  55. const { children, onOpen, onClose } = this.props;
  56. return (
  57. <Open className="super-open-text" height={60} up={<Icon name="small-up" />} down={<Icon name="small-down" />} onOpen={onOpen} onClose={onClose}>
  58. {children}
  59. </Open>
  60. );
  61. }
  62. }