antd5源码分析之classnames库

classnames地址

代码仓库
https://github.com/JedWatson/classnames

源码

可以找到rec/index.js文件

var hasOwn = {}.hasOwnProperty;

function classNames() {
  var classes = [];

  for (var i = 0; i < arguments.length; i++) {
    var arg = arguments[i];
    if (!arg) continue;

    var argType = typeof arg;

    if (argType === 'string' || argType === 'number') {
      classes.push(arg);
    } else if (Array.isArray(arg)) {
      if (arg.length) {
        var inner = classNames.apply(null, arg);
        if (inner) {
          classes.push(inner);
        }
      }
    } else if (argType === 'object') {
      if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
        classes.push(arg.toString());
        continue;
      }

      for (var key in arg) {
        if (hasOwn.call(arg, key) && arg[key]) {
          classes.push(key);
        }
      }
    }
  }
// 通过join将classes数组转换为字符串,返回;
  return classes.join(' ');
}

分析

片段1

通过arguments获取classnames函数接受的参数
利用for语句循环argument,当参数不存在时,continue继续执行。

  for (var i = 0; i < arguments.length; i++) {
    var arg = arguments[i];
    if (!arg) continue;
 }

片段2

通过typeof来判断参数的类型,只有三种分支结果:

var argType = typeof arg;

string或者number

直接push到classes数组中;

if (argType === 'string' || argType === 'number') {
  classes.push(arg);
}

array

这里是递归调用classNames函数,将数组中的每一项作为参数传入;

if (Array.isArray(arg)) {
      if (arg.length) {
        var inner = classNames.apply(null, arg);
        if (inner) {
          classes.push(inner);
      }
  }
}

object

  • 这里是遍历对象的每一项,如果值为true,则将key作为类名pushclasses数组中;
  • 但是首先这里的处理是先判断argtoString方法是否被重写,如果被重写了,则直接将arg的toString方法的返回值push到classes数组中;
  • -第一个判断是判断argtoString方法是否被重写;
  • 第二个判断是判断Object.prototype.toString方法是否被重写,如果被重写了,则arg的toString方法的返回值一定不会包含[native code]
if (argType === 'object') {
      if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
        classes.push(arg.toString());
        continue;
      }

      for (var key in arg) {
        if (hasOwn.call(arg, key) && arg[key]) {
          classes.push(key);
        }
      }
}
Function.prototype.toString()  toString()// 方法返回一个表示当前函数源代码的字符串。
console.log(Math.abs.toString());
// Expected output: "function abs() { [native code] }"
console.log({}.toString);//ƒ toString() { [native code] }
console.log({}.toString.toString());// 'function toString() { [native code] }'
console.log(Object.toString);//ƒ toString() { [native code] }
console.log(Object.toString?.toString());//'function toString() { [native code] }'
console.log(Object.prototype.toString);//ƒ toString() { [native code] }
console.log(Object.prototype.toString());//'[object Object]'
console.log(Object.prototype.toString.call('SS'));//'[object String]'
console.log(Object.prototype.toString.call(null));//'[object Null]'

测试

  const class1 = classNames('select-menu') // 'select-menu'
  const class2 = classNames('select-menu', 2) // 'select-menu 2'
  const class3 = classNames({
    "select-box": true,
    "select-class-box": false,
    "ul-box": 'ul-box-w'
  })// 'select-box ul-box'
  // 重写toString方法
  function fun(as) {
    fun.prototype.toString = function () {
      return as
    }
  }

  const class4 = classNames(new fun('oi'))//'oi'

兼容性

CommonJS

CommonJS是Node.js的模块规范,Node.js中使用require来引入模块,使用module.exports来导出模块;

所以这里通过判断module是否存在来判断是否是CommonJS环境,如果是的话,就通过module.exports来导出模块。

AMD

AMD是RequireJS在推广过程中对模块定义的规范化产出,AMD也是一种模块规范,AMD中使用define来定义模块,使用require来引入模块;

所以这里通过判断define是否存在来判断是否是AMD环境,如果是的话,就通过define来定义模块。

window 浏览器环境

window是浏览器中的全局对象,这里并没有判断,直接使用else兜底,因为这个库最终只会在浏览器中使用,所以这里直接使用window来定义模块。

  // CMD模块 支持module
	if (typeof module !== 'undefined' && module.exports) {
		classNames.default = classNames;
		module.exports = classNames;
	} else if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
    // AMD模块
		// register as 'classnames', consistent with npm package name
		define('classnames', [], function () {
			return classNames;
		});
	} else {
		window.classNames = classNames;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antd的Tabs组件中,当标签页数量过多时,会自动出现滚动条,使用户可以通过滚动来查看所有标签页。下面我们就来分析一下Antd是如何实现这个滚动效果的。 ## 实现原理 Antd中Tabs的滚动效果是通过设置`overflow-x: auto`来实现的,当标签页数量超过一定数量时,Tabs容器的宽度会缩小,出现水平滚动条。 具体实现代码如下: ```css .ant-tabs-bar { position: relative; margin: 0; padding: 0; white-space: nowrap; overflow-x: auto; overflow-y: hidden; user-select: none; -ms-touch-action: pan-y; touch-action: pan-y; -webkit-overflow-scrolling: touch; .ant-tabs-nav-container { height: 100%; display: inline-flex; align-items: stretch; } } ``` 从上面的代码中可以看出,Antd将Tabs容器的`overflow-x`属性设置为auto,这样当标签页数量过多时,会自动出现水平滚动条;同时还设置了一些其他属性,比如`white-space: nowrap`来防止标签页换行,`-webkit-overflow-scrolling: touch`来优化iOS设备的滚动体验等。 ## 注意事项 使用Antd的Tabs组件时,需要注意以下几点: 1. 需要设置Tabs容器的宽度,否则滚动条不会出现; 2. 如果标签页数量较少,Tabs容器的宽度会很大,导致页面布局不美观,需要进行调整; 3. 当标签页数量很多时,滚动体验可能会受到影响,需要根据实际情况进行优化。 ## 总结 Antd的Tabs组件通过设置`overflow-x: auto`来实现标签页滚动效果,同时还设置了一些其他属性来优化用户体验。在使用该组件时,需要注意设置Tabs容器的宽度,并根据实际情况进行优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值