react-components-docs/react编码规范.md

124 lines
3.8 KiB
Markdown
Raw Normal View History

2016-09-30 12:52:53 +08:00
# react组件开发规范
2016-10-08 14:15:57 +08:00
## 文件命名
- 每一个文件只包含一个组件,每一个基本组件只包含单一功能
- src目录下如果文件返回是一个类文件名首字母大写
- 文件js模块统一使用js后缀名
- 测试用例文件名使用.spec.js后缀
- 每一个组件使用一个单独的测试用例文件
## js规范
2016-10-14 15:38:12 +08:00
- 使用es6开发尽量使用常用的ES6语法(ES6语法参考)[http://es6.ruanyifeng.com/]
2016-09-30 12:52:53 +08:00
- 使用jsx语法
2016-10-17 15:55:35 +08:00
- 组件仓库命名为小写和“-”连接如button、button-group
- 组件文件命名使用大驼峰, ComponentDemo
2016-09-30 12:52:53 +08:00
- 带命名空间的组件如果一个组件包含只有自身使用的子组件以该组件为命名空间编写组件例如TableTable.Head
- 不使用displayName命名
- 自定义属性使用data-
- 使用propTypes进行props类型校验
- 使用defaultProps定义默认参数
2016-10-14 15:38:12 +08:00
- 定义props避开react关键字及保留字常用的props及state定义可参考下表
2016-09-30 12:52:53 +08:00
- 尽量少或者不使用ref获取和操作dom节点使用state和prop进行控制dom
- 事件调用使用在元素上onClick调用
- 注意react和html的表单元素的差异
- 使用es6后不支持mixin使用decorator进行扩展babel需要增加解析器和高阶组件方式扩展。
2016-10-08 14:15:57 +08:00
- 尽量不使用比较大的第三方js库
2016-10-14 15:40:57 +08:00
- 组件方法定义顺序 constructor --> 声明周期方法(componentWillMount,componentDidMount,
componentWillUpdate,componentDidUpdate,componentWillUnmount)
- 尽量多而有用的代码注释,方法用块级注释,结构如下例。
- 有必要需要些组件的销毁方法,比如 定时器,需要用销毁方法销毁定时器
- ...others 没有必要 勿用
2016-10-14 15:42:38 +08:00
- 自身定义的props属性应避免与react的关键字相同
2016-10-14 15:40:57 +08:00
2016-10-08 14:15:57 +08:00
2016-09-30 12:52:53 +08:00
- 代码规范使用 [airbnb规范](https://github.com/airbnb/javascript/tree/master/react)
2016-09-30 13:49:30 +08:00
2016-10-08 14:15:57 +08:00
## 样式规范
2016-10-10 16:45:37 +08:00
- 组件样式使用sass编写公用样式使用tinper-bee-core包请阅读[tinper-bee-core文档](https://github.com/tinper-bee/tinper-bee-core)
2016-10-08 14:15:57 +08:00
- 组件样式调用使用classnames模块进行样式处理使用className调用
- 组件内部使用clsPrefix为样式前缀
```javascript
const clsPrefix = 'u-select';
const class1 = {
[`${clsPrefix}-item`]: true,
[`${clsPrefix}-item-last`]: stepLast,
[`${clsPrefix}-status-${status}`]: true,
2016-10-17 15:14:49 +08:00
[`${clsPrefix}-custom`]: icon
2016-10-08 14:15:57 +08:00
};
const class2 = [`${clsPrefix}-submit`, `${clsPrefix}-item`];
const classString = classNames('hide', class1, class2);
```
2016-10-12 09:56:00 +08:00
## 组件接口规范
|参数|说明|类型|默认值|
|---|----|---|------|
|size|尺寸|string|medium|
|color|颜色|string|''|
|shape|形状|string|''|
|disabled|是否禁用(`disabled` 或 `true` `false`)|bool|false|
|className|增加额外的类名|string|''|
|htmlType|html dom 的 type 属性|string|''|
|style|内联样式|object|''|
|loadingText|loading时显示内容,默认值loading|string|loading|
|loadingTime|loading的时间|number|300ms|
2016-10-08 14:15:57 +08:00
2016-09-30 13:49:30 +08:00
## 基本代码结构
```javascript
//引入依赖
2016-10-11 13:30:13 +08:00
import React from 'react';
import ReactDOM from'react-dom';
import classnames from 'classnames';
2016-09-30 13:49:30 +08:00
//定义prop检验
const propTypes = {
2016-10-14 15:38:12 +08:00
//每一个props都要写注释
2016-09-30 13:49:30 +08:00
}
//定义默认参数
const defaultProps = {
}
2016-10-14 15:40:57 +08:00
/**
* 定义组件
*/
2016-09-30 13:49:30 +08:00
class Button extends React.Component {
constructor (props) {
super(props);
//定义state
this.state = {
2016-10-14 15:38:12 +08:00
//每一个state要写注释
2016-09-30 13:49:30 +08:00
}
2016-10-14 15:40:57 +08:00
// 事先声明方法绑定
this.MyEvent = this.MyEvent.bind(this);
2016-09-30 13:49:30 +08:00
}
2016-10-14 15:38:12 +08:00
//自定义函数方法,及注释
2016-10-17 15:27:47 +08:00
MyEvent () {
}
2016-10-14 15:38:12 +08:00
//组件生命周期方法
2016-09-30 13:49:30 +08:00
2016-10-17 15:27:47 +08:00
2016-09-30 13:49:30 +08:00
render () {
return (
2016-10-14 15:40:57 +08:00
// <div onClick={this.MyEvent}></div>
2016-09-30 13:49:30 +08:00
)
}
}
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
2016-10-11 13:30:13 +08:00
export default Button;
2016-09-30 13:49:30 +08:00
```
2016-10-08 14:15:57 +08:00
参考链接
https://github.com/react-component/react-component.github.io/blob/master/docs/zh-cn/code-style/js.md