内容简介:技术栈:React + Webpack + TypeScript + Docz先上脚手架目录结构
技术栈:React + Webpack + TypeScript + Docz
先上脚手架目录结构
docz example lib scripts src
注意:本脚手架只是基本配置,内部代码比较简洁,主要是起到一个学习引导作用。
组件开发过程中没什么技术难点,主要是样式处理。 全局 default.less 样式处理借鉴了 antd ,将常用的样式用变量存储,方便后续主题设置
@wui-prefix: wui; @primary-color: red; @info-color: #3bb4f2; @success-color: #5eb95e; @danger-color: #dd514c; @warning-color: #f37b1d; @white: #fff; @black: #000; @alert-info-bg-color: @info-color; @alert-success-bg-color: @success-color; @alert-warning-bg-color: @warning-color; @alert-danger-bg-color: @danger-color; 复制代码
mixins/alert.less 混合用函数注入变量
.var-alert(@background-color, @border-corlor, @text-color) {
border: 1px solid @border-corlor;
background-color: @background-color;
color: @text-color;
}
复制代码
然后组件 components/alert/index.less 中直接引入即可
@import '../style/default.less';
@import '../style/mixins/alert.less';
@alert-prefix-cls: ~'@{wui-prefix}-alert';
.@{alert-prefix-cls} {
position: relative;
padding: 8px 16px;
margin-bottom: 10px;
&-info {
.var-alert(@alert-info-bg-color, @alert-info-bg-color, @white);
}
&-success {
.var-alert(@alert-success-bg-color, @alert-success-bg-color, @white);
}
&-warning {
.var-alert(@alert-warning-bg-color, @alert-warning-bg-color, @white);
}
&-danger {
.var-alert(@alert-danger-bg-color, @alert-danger-bg-color, @white);
}
}
复制代码
构建过程中, less 文件无需转换为 css 文件,只需通过脚本遍历文件夹同步到对应目录下即可, build-less.js 代码:
const readFiles = async (filePath, name, callback) => {
const files = fs.readdirSync(filePath)
files.forEach(file => {
const filedir = path.join(filePath, file)
fs.stat(filedir, (error, stats) => {
if (error) {
return console.error('stats error:', error)
}
if (stats.isFile() && filedir.indexOf(name) > -1) {
callback && callback(filedir)
} else if (stats.isDirectory()) {
readFiles(filedir, name, callback)
}
})
})
}
const componentsPath = 'src/components'
readFiles(
path.join(__dirname, '../', componentsPath),
'.less',
(file, error) => {
if (error) {
return console.error('read files error:', error)
}
fs.outputFileSync(
file.replace(componentsPath, 'lib'),
fs.readFileSync(file)
)
}
)
复制代码
文档界面:
贴一段 Alert 组件简易代码
import React, { Component } from 'react'
import classNames from 'classnames'
import { getPrefix } from '../util/method'
import './index.less'
interface AlertProps {
type?: 'success' | 'error' | 'warn' | 'info'
message: React.ReactNode
className?: string
}
export default class Alert extends Component<AlertProps> {
render() {
const { type = 'info' } = this.props
const prefix = getPrefix('alert')
const className = classNames(prefix, `${prefix}-${type}`)
return <div className={className}>{this.props.message}</div>
}
}
复制代码
在本地开发及测试完组件后需要发布到仓库测试,不建议发布到npm官方仓库中,因为毕竟是半成品,可以使用 verdaccio 在本地搭建一个私有仓库进行测试
搭建私有参考可以参考 使用verdaccio搭建私有npm仓库
以上所述就是小编给大家介绍的《如何开发React UI组件库》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
电商产品经理宝典:电商后台系统产品逻辑全解析
刘志远 / 电子工业出版社 / 2017-10-1 / 49.00元
时至今日,对于产品经理的要求趋向业务型、平台型,甚至产生了细分领域专家。纯粹的前端产品经理(页面、交互)逐渐失去竞争力。而当后台产品经理的视野开始从功能延伸到模块,再延伸到子系统,最后关注整体系统时,就有了把控平台型产品的能力。 《电商产品经理宝典:电商后台系统产品逻辑全解析》围绕“电商后台产品”,从电商的整体产品架构入手,逐步剖析各支撑子系统。通过学习电商产品后台的架构和逻辑,可以让读者从......一起来看看 《电商产品经理宝典:电商后台系统产品逻辑全解析》 这本书的介绍吧!