一个展开和收起的业务组件(React)

index.tsx

import classNames from 'classnames';
import { useBoolean } from 'ahooks';
import { useMemo } from 'react';
import styles from './index.module.less';

type IProps = {
  foldHeight?: string | number;
} & React.HTMLAttributes<HTMLDivElement>;

// 默认折叠高度
const DEFAULT_FOLD_HEIGHT = 200;

/**
 * 用于折叠展开的组件
 * @param props
 * @returns
 */
const FoldBlock = (props: IProps) => {
  const [open, { toggle }] = useBoolean(false);

  const height = useMemo(() => {
    return typeof props?.foldHeight === 'number'
      ? `${props?.foldHeight ?? DEFAULT_FOLD_HEIGHT}px`
      : props?.foldHeight || `${DEFAULT_FOLD_HEIGHT}px`;
  }, [props?.foldHeight]);

  return (
    <div
      className={classNames(styles.foldBlockSwapper, props?.className)}
      style={{ height: open ? 'auto' : height, ...(props?.style || {}) }}
    >
      {props?.children}
      <div className={styles.toggleBox}>
        <div className={styles.splitLine} />
        <div className={styles.toggleBtn} onClick={toggle}>
          {open ? '收起' : '展开'}
          <span className={classNames(styles.toggleTriangle, open && styles.toggleTriangleOpen)} />
        </div>
        <div className={styles.splitLine} />
      </div>
    </div>
  );
};

export default FoldBlock;

index.module.less

.foldBlockSwapper {
  position: relative;
  padding-bottom: 44px;
  overflow: hidden;

  .toggleBox {
    position: absolute;
    bottom: 0;
    left: 0;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 44px;
    padding: 0 24px;
    padding-bottom: 16px;
    background-color: #fff;

    .splitLine {
      flex: 1;
      height: 1px;
      background-color: #e0e0e0;
      transform: scaleY(0.5);
    }

    .toggleBtn {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 5px 26px;
      color: #0068ff;
      cursor: pointer;

      .toggleTriangle {
        display: inline-block;
        width: 6px;
        height: 6px;
        margin-left: 8px;
        border-top: 1px solid #0068ff;
        border-right: 1px solid #0068ff;
        transform: rotate(135deg);
      }

      .toggleTriangleOpen {
        transform: rotate(315deg);
      }
    }
  }
}

相关推荐

  1. 一个展开业务组件(React)

    2024-03-29 05:46:03       19 阅读
  2. React函数组件组件区别

    2024-03-29 05:46:03       11 阅读
  3. ReactReact懒加载组件lazySuspense

    2024-03-29 05:46:03       11 阅读
  4. react组件元素类型总结

    2024-03-29 05:46:03       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 05:46:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 05:46:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 05:46:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 05:46:03       20 阅读

热门阅读

  1. 基于单片机和Wi-Fi 技术的家电远程控制系统设计

    2024-03-29 05:46:03       17 阅读
  2. day 5|中间件

    2024-03-29 05:46:03       16 阅读
  3. 【深度学习】YOLO检测器的发展历程

    2024-03-29 05:46:03       22 阅读
  4. Spring Boot集成itext实现html生成PDF功能

    2024-03-29 05:46:03       19 阅读
  5. 显示器刷新率

    2024-03-29 05:46:03       25 阅读
  6. DDPG control two-joint arm

    2024-03-29 05:46:03       23 阅读