Switch组件封装loading记录

6 篇文章 0 订阅

背景:

已经有一个具有基本功能的switch组件,但是缺少loading功能。需求需有实现组件的loading状态

基本思路:

  1. 理解Switch组件的基本构成:trigger + children
    在这里插入图片描述
  2. 确定loading放置位置: 因为最后loading是放置于switch上trigger位置,所以基于trigger来写
    在这里插入图片描述
  3. 在不修改html(包括render)中dom的情况下, 可以使用伪元素:after 和 :before来实现想要在的位置插入元素
// style.scss 文件, react 配置了scss和 css module
// $scn  === switch-component-name  用scn来表示原来Switch组件的类名
:global {
    .#{$scn}-loading {
        line-height: 1;
    }
    //  核心代码 : 1. 通过absolution定位(已知switch组件有positon 属性)获取位置    2. 通过css3 中animation属性实现旋转  3. 通过font-family改变子体,映射成loading样子 
    // 
    &:before {
           position: absolute;
            top: 0;
            bottom: 0;
            z-index: 2;

            display: block;
            width: 16px;
            height: 16px;
            line-height: 16px;
            margin: auto;


            content: '\e900';
            font-family: NextIcon;
            font-size: 16px;

            color: red;
            animation: loadingCircle 1s infinite linear;
    }
    // 核心代码结束

}

  1. 封装switch并且暴露接口

//  注意scn变量对应字符串要和scss文件中scn变量对应字符串全等

const prefix = `${scn}`;

export default class ExtSwitch extends React.Component {
    render() {
        const { className, loading, disabled, ...restProps } = this.props;
        return (
            <Switch
		        // 核心代码 在类名中增加loading状态下的类名, 并且修改disabled当loading时候disabled也为true
                className={classnames(className, { [`${prefix}-loading`]: loading })}
                disabled={disabled || loading}
                {...restProps}
            />
        );
    }
}
  1. 完善建议: 可以增加switch-on 和 switch-off的样式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值