UNPKG

3.04 kBJavaScriptView Raw
1import React, { Component } from 'react';
2import ReactDOM from 'react-dom';
3import PropTypes from 'prop-types';
4import classnames from 'classnames';
5
6const propTypes = {
7 /**
8 * @title 尺寸
9 */
10 size: PropTypes.oneOf(['sm', 'md', 'xg', 'lg']),
11 /**
12 * @title 样式
13 */
14 style: PropTypes.object,
15 /**
16 * @title 形状
17 */
18 shape: PropTypes.oneOf(['block', 'round','border', 'squared', 'floating', 'pillRight', 'pillLeft', 'icon']),
19
20 bordered: PropTypes.bool,
21 /**
22 * @title 类型
23 */
24 colors: PropTypes.oneOf(['primary', 'secondary', 'accent', 'success', 'info', 'warning', 'danger', 'dark', 'light' , 'default']),
25 /**
26 * @title 是否禁用
27 * @veIgnore
28 */
29 disabled: PropTypes.bool,
30 /**
31 * @title 类名
32 * @veIgnore
33 */
34 className: PropTypes.string,
35
36 /**
37 * @title <button> 的 type
38 * @veIgnore
39 */
40 htmlType: PropTypes.oneOf(['submit', 'button', 'reset']),
41 isSubmit:PropTypes.bool//是否作为form的提交按钮
42}
43
44const defaultProps = {
45 disabled: false,
46 htmlType: 'button',
47 clsPrefix: 'u-button',
48 bordered: false,
49 isSubmit:false
50}
51
52const sizeMap = {
53 sm: 'sm',
54 md: 'md',
55 xg: 'xg',
56 lg: 'lg'
57 },
58 colorsMap = {
59 primary: 'primary',
60 secondary: 'secondary',
61 accent: 'accent',
62 success: 'success',
63 info: 'info',
64 warning: 'warning',
65 danger: 'danger',
66 dark: 'dark',
67 light: 'light'
68 },
69 shapeMap = {
70 block: 'block',
71 round: 'round',
72 border: 'border',
73 squared: 'squared',
74 floating: 'floating',
75 pillRight: 'pill-right',
76 pillLeft: 'pill-left',
77 icon: 'icon'
78 };
79
80
81class Button extends Component {
82 constructor(props) {
83 super(props);
84 }
85 render() {
86 let {colors,
87 shape,
88 disabled,
89 className,
90 size,
91 bordered,
92 children,
93 htmlType,
94 clsPrefix,
95 isSubmit,
96 ...others} = this.props;
97 let clsObj = {};
98 if (className) {
99 clsObj[className] = true;
100 }
101 if (sizeMap[size]) {
102 clsObj[`${clsPrefix}-${sizeMap[size]}`] = true;
103 }
104
105 if (shapeMap[shape]) {
106 clsObj[`${clsPrefix}-${shapeMap[shape]}`] = true;
107 }
108 if (colorsMap[colors]) {
109 clsObj[`${clsPrefix}-${colorsMap[colors]}`] = true;
110 }
111 //clsObj[`${clsPrefix}-border`] = bordered;
112 let classes = classnames(clsPrefix, clsObj);
113 return (
114 <button
115 type={htmlType}
116 className={classes}
117 disabled={disabled}
118 {...others}>
119 {this.props.children}
120 </button>
121 );
122 }
123}
124
125Button.propTypes = propTypes;
126Button.defaultProps = defaultProps;
127
128export default Button;