-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathCLabel.js
120 lines (99 loc) · 2.95 KB
/
CLabel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {tagPropType, mapToCssModules, deprecated} from './Shared/helper.js';
import isobject from 'lodash.isobject';
const colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
const stringOrNumberProp = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
const columnProps = PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.shape({
size: stringOrNumberProp,
push: deprecated(stringOrNumberProp, 'Please use the prop "order"'),
pull: deprecated(stringOrNumberProp, 'Please use the prop "order"'),
order: stringOrNumberProp,
offset: stringOrNumberProp,
}),
]);
const getColumnSizeClass = (isXs, colWidth, colSize)=>{
if (colSize === true || colSize === '') {
return isXs ? 'col' : `col-${colWidth}`;
} else if (colSize === 'auto') {
return isXs ? 'col-auto' : `col-${colWidth}-auto`;
}
return isXs ? `col-${colSize}` : `col-${colWidth}-${colSize}`;
};
//component - CoreUI / CLabel
const CLabel = props=>{
const {
tag: Tag,
className,
cssModule,
//
innerRef,
hidden,
widths,
check,
size,
for: htmlFor,
...attributes
} = props;
// render
const colClasses = [];
widths.forEach((colWidth, i) => {
let columnProp = props[colWidth];
delete attributes[colWidth];
if (!columnProp && columnProp !== '') {
return;
}
const isXs = !i;
let colClass;
if (isobject(columnProp)) {
const colSizeInterfix = isXs ? '-' : `-${colWidth}-`;
colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
colClasses.push(mapToCssModules(classNames({
[colClass]: columnProp.size || columnProp.size === '',
[`order${colSizeInterfix}${columnProp.order}`]: columnProp.order || columnProp.order === 0,
[`offset${colSizeInterfix}${columnProp.offset}`]: columnProp.offset || columnProp.offset === 0
})), cssModule);
} else {
colClass = getColumnSizeClass(isXs, colWidth, columnProp);
colClasses.push(colClass);
}
});
const classes = mapToCssModules(classNames(
className,
hidden ? 'sr-only' : false,
check ? 'form-check-label' : false,
size ? `col-form-label-${size}` : false,
colClasses,
colClasses.length ? 'col-form-label' : false
), cssModule);
return (
<Tag htmlFor={htmlFor} {...attributes} className={classes} ref={innerRef} />
);
}
CLabel.propTypes = {
tag: tagPropType,
children: PropTypes.node,
className: PropTypes.string,
cssModule: PropTypes.object,
//
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
hidden: PropTypes.bool,
check: PropTypes.bool,
size: PropTypes.string,
for: PropTypes.string,
xs: columnProps,
sm: columnProps,
md: columnProps,
lg: columnProps,
xl: columnProps,
widths: PropTypes.array
};
CLabel.defaultProps = {
tag: 'label',
widths: colWidths
};
export default CLabel;