checkbox封装代码

手写checkbox

  • 1、IconMys 是封装的字体组件(下面就是svg 的图片)
    'input-check': `<svg viewBox="0 0 29 21">
          <polygon points="12.41,20.74 0,10.82 4.37,5.35 11.96,11.42 23.75,0 28.62,5.03 "/>
          </svg>`,
    
  • 2、Color, Font, 是公共变量 自己可以随意定义
  • 3、styled 在js 中写样式工具
import React from 'react';
import styled from 'styled-components';
import {
    IconMys,
} from '../../core/index';
import {
    Color,
    Font,
} from '../../../../style/Const';

const Wrap = styled.div`
position: relative;
box-sizing: border-box;
padding-top: 10px;
padding-bottom: 10px;
min-height: 44px;
padding-left: 15px;
padding-right: 15px;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: center;
.checkbox-item label{
    display: flex;
    justify-content: center;
    align-items: center;
}
.checkbox-content{
    flex: 1;
    font-size:${Font.Size.base}px;
    line-height: ${Font.LineHeight.md};
}
input:checked ~ .checkbox_icon{
    background-color: #2697ac;
    border: none;
    .checkbox_icon_mark{
        visibility: visible;
    }
}
`;
const CheckboxInput = styled.input`
position: absolute;
top: 0;
left: 0;
opacity: 0;
width: 100%;
height: 100%;
z-index: 2;
border: 0 none;
appearance: none;
box-sizing: border-box;
padding: 0;
`;
const CheckboxIcon = styled.span`
border-radius: 4px;
display: inline-flex;
flex-direction: column;
align-items: center;
justify-content: center;
box-sizing: border-box;
width: 22px;
height: 22px;
background-color: #fff;
border: solid 1px ${Color.Border.base};
vertical-align: middle;
margin-right: 10px;
.checkbox_icon_mark{
    width: 15px;
    visibility: hidden;
    svg{
        color: #fff;
        width: 100%;
        height: auto;
        margin-top: 2px;
    }
}
`;

class Checkbox extends React.Component {
    render() {
        const { checked, value, name, onChange, width, ...rest } = this.props;
        return (
            <Wrap {...rest}>
                <div className="checkbox-item">
                    <label>
                        <CheckboxInput type="checkbox" name={name} className="checkbox-input" style={{width}} value={value} onChange={onChange} checked={checked} />
                        <CheckboxIcon className='checkbox_icon'>
                            <div className='checkbox_icon_mark'>
                                <IconMys type='#input-check' />
                            </div>
                        </CheckboxIcon>
                        <span className="checkbox-content">
                            {this.props.children}
                        </span>
                    </label>
                </div>
            </Wrap>
        );
    }
}
const noop = () => {};
Checkbox.defaultProps = {
    onChange: noop,
};
export default Checkbox;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值