第一次手寫react組件

也算是有紀(jì)念意義的,盡管現(xiàn)在感覺有些朦朧。比如,寫一個(gè)button的樣式加功能實(shí)現(xiàn)?,F(xiàn)在還看不到UI的樣子,等本書后面的內(nèi)容吧。

1, share/index.js

export { default as Select } from './select/index';
export { default as Title } from './title/index';
export { default as List } from './list/index';
export { default as Input } from './input/index';
export { default as Search } from './search/index';
export { default as Type } from './type/index';
export { default as Button } from './button/index';
export { default as TabBottom } from './tab-bottom/index';
export { default as Card } from './card/index';

2, share/default.less

@hd: 1px; //基本單位

//字體顏色
@c-main: #cc0000;
@c-gray: #ddd;
@c-white: #fff;
@c-ccc: #ccc;
@c-eee: #eee;
@c-999: #999;
@c-333: #333;

//背景色
@bc-main: #cc0000;
@bc-black: #333;
@bc-gray: #ddd;
@bc-body: #f5f5f9;
@bc-white: #fff;

//圖片尺寸
@size-10: 10 * @hd;
@size-12: 12 * @hd;
@size-14: 14 * @hd;
@size-16: 16 * @hd;
@size-20: 20 * @hd;
@size-30: 30 * @hd;
@size-40: 40 * @hd;
@size-50: 50 * @hd;

//邊框色
@border-c-base: #ddd;

3, share/button/index.less

@import '../default';
@prefixCls: s-button;
.@{prefixCls} {
    display: flex;
    justify-content: center;
    &-btn {
        text-align: center;
        height: (@size-40) - 5;
        line-height: (@size-40) - 5;
        border: 1px solid @c-main;
        border-radius: @size-10;
        color: @c-main;
        width: 30%;
        margin-left: auto;
        margin-right: auto;
    }
    &-active {
        background-color: #eee;
    }
}
.@{prefixCls}-btn-fill {
    height: @size-40;
    line-height: @size-40;
    background: @c-main;
    color: @c-white;
    border-radius: @size-10;
    text-align: center;
    font-size: @size-16;
    font-weight: bold;
}
.@{prefixCls}-btn-half {
    height: @size-40;
    line-height: @size-40;
    background: @c-main;
    color: @c-white;
    border-radius: @size-10;
    text-align: center;
    font-size: @size-16;
    font-weight: bold;
    width: 48%;
}

4, share/button/defaultProps.js

function noop() {}
export const defaultProps = {
    prefixCls: 's-button',
    onClick: noop,
    type: '',
    children: [],
    className: ''
}

5, share/button/index.js

import React from 'react';
import classnames from 'classnames';
import '../index.css';
import {defaultProps} from './defaultProps';
import TouchFeedback from 'rmc-feedback';

//兩個(gè)中文字符的正則表達(dá)式
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
//判斷是否是兩個(gè)字符
cont isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
//判斷是否是字符類型
function isString(str) {
    return typeof str === 'string';
}
//判斷如果是兩個(gè)中文字符則插入空格
function insertSpace(child) {
    if (isString(child.type) && isTwoCNChar(child.props.children)) {
        return React.cloneElement(
            child,
            {},
            child.props.children.split('').join(' '),
        );
    }
    if (isString(child)) {
        if (isTwoCNChar(child)) {
            child = child.split('').join(' ');
        }
    }
    return child;
}

export default class Button extends React.Component {
    static defaultProps = defaultProps;
    handleClick() {
        const { onClick } = this.props;
        if (onClick) {
            onClick();
        }
    };
    render() {
        const { prefixCls, type, children, className } = this.props;
        //通過classname方法將所有class整合
        const wrapCls = classnames(prefixCls, className, {
            ['${prefixCls}-btn']: (type === 'primary'||!type),
            ['${prefixCls}-btn-fill']: type === 'fill',
            ['${prefixCls}-btn-half']: type === 'half',
        });
        //調(diào)用insertSpace方法判斷,如果是兩個(gè)中文字符則插入空格
        const kids = React.Children.map(children, insertSpace);
        return(
            <TouchFeedback activeClassName={prefixCls+'-active'}>
                <div className={wrapCls} onClick{()=>this.handleClick()}>
                    {kids}
                </div>
            </TouchFeedback>
        )
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容