React 组件封装之 Title 标题

一、Title 标题组件

组件说明:
实现两种 Title 标题,一种有主题填充色,没有返回图标,适用于一级页面的标题;一种没有主题填充色,有返回图标,适用于非一级页面的标题。

效果展示:
有主题填充色,一级页面标题效果
在这里插入图片描述

无主题填充色,非一级页面标题效果
在这里插入图片描述

二、使用案例

import {Title} from 'share';
import React from 'react';
export default class Example extends React.Component {
    constructor(props) {
        super(props);
    }
    render(){
      return  <Title isHome={true} title="商品发布"/>
    }
}

三、API 使用指南

属性说明类型默认值
title标题文字String
isHome是否是一级页面Booleanfalse
onLeftClick返回图标点击事件点击返回图标的回调函数(e: Object): void
style样式类名string

四、源代码

index.js

import React from 'react';
import './index.css';
import backBlock from '../images/title/back-block.png';
import {defaultProps} from './defaultProps';
export default class Title extends React.Component {
    static defaultProps = defaultProps;
    constructor(props) {
        super(props);
    }
    onLeftClick() {
        const {onLeftClick} =this.props;
        if (onLeftClick) {
            onLeftClick();
        }else {
            this.props.that.props.history.goBack()
        }
    }
    render() {
        const {title,prefixCls,isHome,style} =this.props;
        if (isHome) {
            return (
                <div className={prefixCls}>
                    <div className={prefixCls+"-warp-home"}>{title}</div>
                </div>
            )
        } else {
            return (
                <div className={prefixCls}>
                    <div className={prefixCls+"-warp"} style={style}>
                            <img src={backBlock} alt="" className={prefixCls+"-left-icon"} onClick={() => this.onLeftClick()}/>
                            <span>{title}</span>
                            <div className={prefixCls+"-right"}></div>
                    </div>
                </div>
            )
        }
    }
}

index.less

@import '../default';
@prefixCls: s-title;
.@{prefixCls} {
  height:@size-40;
  .@{prefixCls}-warp-home {
    position: fixed;
    background: @bc-main;
    color: @bc-white;
    left: 0;
    right: 0;
    height:@size-40;
    line-height:@size-40;
    text-align: center;
    font-size: @size-16;
    font-weight: bold;
    z-index: 999;
  }
  .@{prefixCls}-left-icon,.@{prefixCls}-right{
    width:@size-20;
  }
  .@{prefixCls}-warp {
    display: flex;
    background: #f5f5f9;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    left: 0;
    right: 0;
    height:@size-40;
    line-height:@size-40;
    padding: 0 (@size-10)-1;
    font-size: @size-16;
    z-index: 999;
    color: #333;
  }
}

defaultProps.js

function noop() {}
export const defaultProps = {
    prefixCls: 's-title',
    title:'',
    isHome:false,
    onLeftClick:noop,
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值