UNPKG

2.27 kBJavaScriptView Raw
1import classNames from 'classnames';
2import React, { PropTypes, Component } from 'react';
3
4const propTypes = {
5 componentClass: PropTypes.oneOfType([
6 React.PropTypes.element,
7 React.PropTypes.string
8 ]),
9
10 /**
11 * xs显示列数
12 */
13 xs: PropTypes.number,
14 /**
15 * sm显示列数
16 */
17 sm: PropTypes.number,
18 /**
19 * md显示列数
20 */
21 md: PropTypes.number,
22 /**
23 * lg显示列数
24 */
25 lg: PropTypes.number,
26 /**
27 * xs偏移列数
28 */
29 xsOffset: PropTypes.number,
30 /**
31 * sm偏移列数
32 */
33 smOffset: PropTypes.number,
34 /**
35 * md偏移列数
36 */
37 mdOffset: PropTypes.number,
38 /**
39 * lg偏移列数
40 */
41 lgOffset: PropTypes.number,
42 /**
43 * xs右偏移列数
44 */
45 xsPush: PropTypes.number,
46 /**
47 * sm右偏移列数
48 */
49 smPush: PropTypes.number,
50 /**
51 * md右偏移列数
52 */
53 mdPush: PropTypes.number,
54 /**
55 * lg右偏移列数
56 */
57 lgPush: PropTypes.number,
58 /**
59 * xs左偏移列数
60 */
61 xsPull: PropTypes.number,
62 /**
63 * sm左偏移列数
64 */
65 smPull: PropTypes.number,
66 /**
67 * md左偏移列数
68 */
69 mdPull: PropTypes.number,
70 /**
71 * lg左偏移列数
72 */
73 lgPull: PropTypes.number,
74};
75
76const defaultProps = {
77 componentClass: 'div',
78 clsPrefix: 'u-col'
79};
80
81
82const DEVICE_SIZES = ['lg', 'md', 'sm', 'xs'];
83
84class Col extends Component {
85 render() {
86 const { componentClass: Component, className, clsPrefix, ...others } = this.props;
87
88 const tbClass = [];
89/**
90 * 对传入props做样式转化
91 * @type {[type]}
92 */
93 DEVICE_SIZES.forEach(size => {
94 function popProp(propSuffix, modifier) {
95 const propName = `${size}${propSuffix}`;
96 const propValue = others[propName];
97
98 if (propValue != undefined && propValue != null) {
99 tbClass.push( `${clsPrefix}-${size}${modifier}-${propValue}`);
100 }
101
102 delete others[propName];
103 }
104
105 popProp('', '');
106 popProp('Offset', '-offset');
107 popProp('Push', '-push');
108 popProp('Pull', '-pull');
109
110 });
111
112 return (
113 <Component
114 className={classNames(tbClass, className)}
115 { ...others }>
116 { this.props.children }
117 </Component>
118 );
119 }
120}
121
122Col.propTypes = propTypes;
123Col.defaultProps = defaultProps;
124
125export default Col;