#yyds干货盘点 React+antDesign封装一个tab组件(类组件)

简介: #yyds干货盘点 React+antDesign封装一个tab组件(类组件)

前言

我是歌谣 放弃很容易 但是坚持一定很酷 喜欢我就一键三连哈 微信公众号关注前端小歌谣 在我们的工作生活中 每次学习一个框架我们就不免要封装组件 而具备封装一个完美组件的能力 我称之为"优秀"


简单的父子组件

父组件

<Geyao/>

子组件

import React, { Component } from 'react';
class GeYao extends React.Component {
  render() {
    console.log(this, 'this');
    console.log(this.props, 'props');
    console.log(this.state, 'state');
    return <div>我是歌谣</div>;
  }
}
export default GeYao;

结果

image.png


需求介绍


需要做一个类似于这样的tab切换的组件

image.png


备注:一个简单的区分方法是,this.props 表示那些一旦定义,就不再改变的特性,而 this.state 是会随着用户互动而产生变化的特性。这时候 我们想着这样的数据格式必须给个数组 然后父组件绑定值


变化1

数据格式

const tabs = [
      { key: '', value: '全部' },
      { key: '1', value: '已审核' },
      { key: '2', value: '未审核' },
    ];

父组件

<GeYao tabs={tabs} />

子组件


import React, { Component } from 'react';
class GeYao extends React.Component {
  render() {
    const { tabs = [] } = this.props;
    console.log(this, 'this');
    console.log(this.props, 'props');
    console.log(this.state, 'state');
    return (
      <ul>
        {tabs.map((item, index) => {
          return <li key={item.key}>{item.name || item.value || ''}</li>;
        })}
      </ul>
    );
  }
}
export default GeYao;

运行结果


image.png

图解示意


image.png

变化2

样式处理的还是不够完美 接下来处理样式

样式处理的还是不够完美 接下来处理样式


变化1

数据格式

const tabs = [
      { key: '', value: '全部' },
      { key: '1', value: '已审核' },
      { key: '2', value: '未审核' },
    ];



父组件

<GeYao tabs={tabs} />


子组件

import React, { Component } from 'react';
class GeYao extends React.Component {
  render() {
    const { tabs = [], styles, itemStyles } = this.props;
    console.log(this, 'this');
    console.log(this.props, 'props');
    console.log(this.state, 'state');
    return (
      <ul
        style={{
          display: 'flex',
          background: '#FFFFFF',
          paddingLeft: '32px',
          width: '100%',
          ...styles,
        }}
      >
        {tabs.map((item, index) => {
          const marginStyle =
            index === 0
              ? {
                  marginLeft: 0,
                }
              : {};
          return (
            <li
              style={{
                color: '#1890FF',
                borderBottom: '2px solid #1890FF',
                padding: '0px 1.5% 8px',
                cursor: 'pointer',
                whiteSpace: 'nowrap',
                ...itemStyles,
                ...marginStyle,
              }}
              key={item.key}
            >
              {item.name || item.value || ''}
            </li>
          );
        })}
      </ul>
    );
  }
}
export default GeYao;



运行结果


image.png

图解

image.png


变化3

数据格式

const tabs = [
      { key: '', value: '全部' },
      { key: '1', value: '已审核' },
      { key: '2', value: '未审核' },
    ];



父组件

<GeYao tabs={tabs} />


子组件

import React, { Component } from 'react';
class GeYao extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectTab: props.selectTab || '',
    };
  }
  /**
   * 切换tab
   * @param {Boolean} doNot 是否不做任何事情,如果存在此值,不执行父组件切换方法,仅仅刷新样式
   */
  handleTabClick(key, doNot) {
    console.log(key, 'key');
    this.setState({
      selectTab: key,
    });
  }
  render() {
    const { selectTab } = this.state;
    const { tabs = [], styles, itemStyles } = this.props;
    return (
      <ul
        style={{
          display: 'flex',
          background: '#FFFFFF',
          paddingLeft: '32px',
          width: '100%',
          ...styles,
        }}
      >
        {tabs.map((item, index) => {
          const marginStyle =
            index === 0
              ? {
                  marginLeft: 0,
                }
              : {};
          return (
            <li
              onClick={() => this.handleTabClick(item.key)}
              style={{
                color: item.key === selectTab ? '#1890FF' : '#000000A6',
                borderBottom: item.key === selectTab ? '2px solid #1890FF' : '',
                padding: '0px 1.5% 8px',
                cursor: 'pointer',
                whiteSpace: 'nowrap',
                ...itemStyles,
                ...marginStyle,
              }}
              key={item.key}
            >
              {item.name || item.value || ''}
            </li>
          );
        })}
      </ul>
    );
  }
}
export default GeYao;



运行结果

image.png


图解


image.png

变化4

数据格式

const tabs = [
      { key: '', value: '全部' },
      { key: '1', value: '已审核' },
      { key: '2', value: '未审核' },
    ];



父组件

<GeYao tabs={tabs} onTabSearch={this.handleTabChange} />


子组件

import React, { Component } from 'react';
class GeYao extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectTab: props.selectTab || '',
      mouseOverTable: '',
    };
  }
  /**
   * 切换tab
   * @param {Boolean} doNot 是否不做任何事情,如果存在此值,不执行父组件切换方法,仅仅刷新样式
   */
  handleTabClick(key, doNot) {
    console.log(key, 'key');
    this.setState({
      selectTab: key,
    });
    if (!doNot) {
      try {
        this.props.onTabSearch(key);
      } catch (error) {}
    }
  }
  render() {
    const { selectTab, mouseOverTable } = this.state;
    const { tabs = [], styles, itemStyles } = this.props;
    return (
      <ul
        style={{
          display: 'flex',
          background: '#FFFFFF',
          paddingLeft: '32px',
          width: '100%',
          ...styles,
        }}
      >
        {tabs.map((item, index) => {
          const marginStyle =
            index === 0
              ? {
                  marginLeft: 0,
                }
              : {};
          return (
            <li
              onMouseOver={() => this.setState({ mouseOverTable: item.key })}
              onMouseLeave={() => this.setState({ mouseOverTable: 'mouseLeave' })}
              onClick={() => this.handleTabClick(item.key)}
              style={{
                color:
                  item.key === selectTab || item.key === mouseOverTable ? '#1890FF' : '#000000A6',
                borderBottom: item.key === selectTab ? '2px solid #1890FF' : '',
                padding: '0px 1.5% 8px',
                cursor: 'pointer',
                whiteSpace: 'nowrap',
                ...itemStyles,
                ...marginStyle,
              }}
              key={item.key}
            >
              {item.name || item.value || ''}
            </li>
          );
        })}
      </ul>
    );
  }
}
export default GeYao;



运行结果

image.png


图解

image.png


总结

我是歌谣 放弃很容易 但是坚持一定很酷 谢谢你的鼓励 微信公众号前端小歌谣 加入前端巅峰交



相关文章
|
2月前
|
前端开发
React查询、搜索类功能的实现
React查询、搜索类功能的实现
17 0
|
3月前
|
资源调度 前端开发 JavaScript
React 的antd-mobile 组件库,嵌套路由
React 的antd-mobile 组件库,嵌套路由
40 0
|
2月前
|
存储 前端开发 中间件
React组件间的通信
React组件间的通信
15 1
|
2月前
|
前端开发 应用服务中间件 数据库
react服务端组件
react服务端组件
21 0
|
2月前
|
前端开发 JavaScript
快速上手React:从概述到组件与事件处理
快速上手React:从概述到组件与事件处理
|
3月前
|
前端开发 JavaScript API
React组件生命周期
React组件生命周期
74 1
|
3月前
|
设计模式 前端开发 数据可视化
【第4期】一文了解React UI 组件库
【第4期】一文了解React UI 组件库
98 0
|
3月前
|
存储 前端开发 JavaScript
【第34期】一文学会React组件传值
【第34期】一文学会React组件传值
31 0
|
3月前
|
前端开发
【第31期】一文学会用React Hooks组件编写组件
【第31期】一文学会用React Hooks组件编写组件
34 0
|
3月前
|
存储 前端开发 JavaScript
【第29期】一文学会用React类组件编写组件
【第29期】一文学会用React类组件编写组件
30 0